blob: 0bb9fb4547d51f0728da6b822010f522d24df046 [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);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002945 // Consume the device reset notification generated when adding a new device.
2946 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 }
2948
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002949 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002950 SetUp(DEVICE_CLASSES);
2951 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952
Chris Yea52ade12020-08-27 16:49:20 -07002953 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002954 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 }
2957
2958 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002959 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 }
2961
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002962 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002963 if (!changes ||
2964 (changes &
2965 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2966 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 mReader->requestRefreshConfiguration(changes);
2968 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002969 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002970 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002971 // Loop the reader to flush the input listener queue.
2972 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002973 }
2974
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2976 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002977 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 InputDeviceIdentifier identifier;
2979 identifier.name = name;
2980 identifier.location = location;
2981 std::shared_ptr<InputDevice> device =
2982 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2983 identifier);
2984 mReader->pushNextDevice(device);
2985 mFakeEventHub->addDevice(eventHubId, name, classes);
2986 mReader->loopOnce();
2987 return device;
2988 }
2989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 template <class T, typename... Args>
2991 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002992 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002993 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002995 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002996 // Loop the reader to flush the input listener queue.
2997 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002998 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 }
3000
3001 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003002 int32_t orientation, const std::string& uniqueId,
3003 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003004 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3005 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003006 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3007 }
3008
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003009 void clearViewports() {
3010 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 }
3012
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003013 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3014 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 RawEvent event;
3016 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003018 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 event.type = type;
3020 event.code = code;
3021 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003023 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003024 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 }
3026
3027 static void assertMotionRange(const InputDeviceInfo& info,
3028 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3029 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003030 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3032 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3033 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3034 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3035 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3036 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3037 }
3038
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003039 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3040 float size, float touchMajor, float touchMinor, float toolMajor,
3041 float toolMinor, float orientation, float distance,
3042 float scaledAxisEpsilon = 1.f) {
3043 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3044 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3046 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003047 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3048 scaledAxisEpsilon);
3049 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3050 scaledAxisEpsilon);
3051 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3052 scaledAxisEpsilon);
3053 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3054 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3056 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3057 }
3058
Michael Wright17db18e2020-06-26 20:51:44 +01003059 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003061 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062 ASSERT_NEAR(x, actualX, 1);
3063 ASSERT_NEAR(y, actualY, 1);
3064 }
3065};
3066
3067const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003068const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003069const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3071const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003072const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3073 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003074const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075
3076// --- SwitchInputMapperTest ---
3077
3078class SwitchInputMapperTest : public InputMapperTest {
3079protected:
3080};
3081
3082TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086}
3087
3088TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003091 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003094 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096}
3097
3098TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 NotifySwitchArgs args;
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3108 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003109 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3110 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111 args.switchMask);
3112 ASSERT_EQ(uint32_t(0), args.policyFlags);
3113}
3114
Chris Ye87143712020-11-10 05:05:58 +00003115// --- VibratorInputMapperTest ---
3116class VibratorInputMapperTest : public InputMapperTest {
3117protected:
3118 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3119};
3120
3121TEST_F(VibratorInputMapperTest, GetSources) {
3122 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3123
3124 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3125}
3126
3127TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3128 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3129
3130 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3131}
3132
3133TEST_F(VibratorInputMapperTest, Vibrate) {
3134 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003135 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003136 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3137
3138 VibrationElement pattern(2);
3139 VibrationSequence sequence(2);
3140 pattern.duration = std::chrono::milliseconds(200);
3141 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3142 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3143 sequence.addElement(pattern);
3144 pattern.duration = std::chrono::milliseconds(500);
3145 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3146 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3147 sequence.addElement(pattern);
3148
3149 std::vector<int64_t> timings = {0, 1};
3150 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3151
3152 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003153 // Start vibrating
3154 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003155 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003156 // Verify vibrator state listener was notified.
3157 mReader->loopOnce();
3158 NotifyVibratorStateArgs args;
3159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_TRUE(args.isOn);
3162 // Stop vibrating
3163 mapper.cancelVibrate(VIBRATION_TOKEN);
3164 ASSERT_FALSE(mapper.isVibrating());
3165 // Verify vibrator state listener was notified.
3166 mReader->loopOnce();
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3168 ASSERT_EQ(DEVICE_ID, args.deviceId);
3169 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003170}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171
Chris Yef59a2f42020-10-16 12:55:26 -07003172// --- SensorInputMapperTest ---
3173
3174class SensorInputMapperTest : public InputMapperTest {
3175protected:
3176 static const int32_t ACCEL_RAW_MIN;
3177 static const int32_t ACCEL_RAW_MAX;
3178 static const int32_t ACCEL_RAW_FUZZ;
3179 static const int32_t ACCEL_RAW_FLAT;
3180 static const int32_t ACCEL_RAW_RESOLUTION;
3181
3182 static const int32_t GYRO_RAW_MIN;
3183 static const int32_t GYRO_RAW_MAX;
3184 static const int32_t GYRO_RAW_FUZZ;
3185 static const int32_t GYRO_RAW_FLAT;
3186 static const int32_t GYRO_RAW_RESOLUTION;
3187
3188 static const float GRAVITY_MS2_UNIT;
3189 static const float DEGREE_RADIAN_UNIT;
3190
3191 void prepareAccelAxes();
3192 void prepareGyroAxes();
3193 void setAccelProperties();
3194 void setGyroProperties();
3195 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3196};
3197
3198const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3199const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3200const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3201const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3202const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3203
3204const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3205const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3206const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3207const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3208const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3209
3210const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3211const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3212
3213void SensorInputMapperTest::prepareAccelAxes() {
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3215 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3217 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3219 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3220}
3221
3222void SensorInputMapperTest::prepareGyroAxes() {
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3224 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3226 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3228 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3229}
3230
3231void SensorInputMapperTest::setAccelProperties() {
3232 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3233 /* sensorDataIndex */ 0);
3234 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3235 /* sensorDataIndex */ 1);
3236 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3237 /* sensorDataIndex */ 2);
3238 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3239 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3240 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3241 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3242 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3243}
3244
3245void SensorInputMapperTest::setGyroProperties() {
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3247 /* sensorDataIndex */ 0);
3248 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3249 /* sensorDataIndex */ 1);
3250 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3251 /* sensorDataIndex */ 2);
3252 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3253 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3254 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3255 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3256 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3257}
3258
3259TEST_F(SensorInputMapperTest, GetSources) {
3260 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3261
3262 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3263}
3264
3265TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3266 setAccelProperties();
3267 prepareAccelAxes();
3268 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3269
3270 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3271 std::chrono::microseconds(10000),
3272 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003273 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003279
3280 NotifySensorArgs args;
3281 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3282 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3283 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3284
3285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3286 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3287 ASSERT_EQ(args.deviceId, DEVICE_ID);
3288 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3289 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3290 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3291 ASSERT_EQ(args.values, values);
3292 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3293}
3294
3295TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3296 setGyroProperties();
3297 prepareGyroAxes();
3298 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3299
3300 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3301 std::chrono::microseconds(10000),
3302 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003303 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003309
3310 NotifySensorArgs args;
3311 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3312 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3313 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3314
3315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3316 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3317 ASSERT_EQ(args.deviceId, DEVICE_ID);
3318 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3319 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3320 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3321 ASSERT_EQ(args.values, values);
3322 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3323}
3324
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325// --- KeyboardInputMapperTest ---
3326
3327class KeyboardInputMapperTest : public InputMapperTest {
3328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003329 const std::string UNIQUE_ID = "local:0";
3330
3331 void prepareDisplay(int32_t orientation);
3332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003334 int32_t originalKeyCode, int32_t rotatedKeyCode,
3335 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336};
3337
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003338/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3339 * orientation.
3340 */
3341void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003342 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3343 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344}
3345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003347 int32_t originalScanCode, int32_t originalKeyCode,
3348 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 NotifyKeyArgs args;
3350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3360 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3361 ASSERT_EQ(originalScanCode, args.scanCode);
3362 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003363 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 KeyboardInputMapper& mapper =
3368 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3369 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372}
3373
3374TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3375 const int32_t USAGE_A = 0x070004;
3376 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003377 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3378 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003379 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3380 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3381 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003383 KeyboardInputMapper& mapper =
3384 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3385 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003386 // Initial metastate is AMETA_NONE.
3387 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 NotifyKeyArgs args;
3392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3393 ASSERT_EQ(DEVICE_ID, args.deviceId);
3394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3395 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3397 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3398 ASSERT_EQ(KEY_HOME, args.scanCode);
3399 ASSERT_EQ(AMETA_NONE, args.metaState);
3400 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3401 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3402 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3403
3404 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3407 ASSERT_EQ(DEVICE_ID, args.deviceId);
3408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3409 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3410 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3411 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3412 ASSERT_EQ(KEY_HOME, args.scanCode);
3413 ASSERT_EQ(AMETA_NONE, args.metaState);
3414 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3415 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3416 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3417
3418 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(DEVICE_ID, args.deviceId);
3423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3424 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3426 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3427 ASSERT_EQ(0, args.scanCode);
3428 ASSERT_EQ(AMETA_NONE, args.metaState);
3429 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3430 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3431 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3432
3433 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3435 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3437 ASSERT_EQ(DEVICE_ID, args.deviceId);
3438 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3439 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3440 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3441 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3442 ASSERT_EQ(0, args.scanCode);
3443 ASSERT_EQ(AMETA_NONE, args.metaState);
3444 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3445 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3446 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3447
3448 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(DEVICE_ID, args.deviceId);
3453 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3454 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3456 ASSERT_EQ(0, args.keyCode);
3457 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3458 ASSERT_EQ(AMETA_NONE, args.metaState);
3459 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3460 ASSERT_EQ(0U, args.policyFlags);
3461 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3462
3463 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3465 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3467 ASSERT_EQ(DEVICE_ID, args.deviceId);
3468 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3469 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3471 ASSERT_EQ(0, args.keyCode);
3472 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3473 ASSERT_EQ(AMETA_NONE, args.metaState);
3474 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3475 ASSERT_EQ(0U, args.policyFlags);
3476 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3477}
3478
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479/**
3480 * Ensure that the readTime is set to the time when the EV_KEY is received.
3481 */
3482TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3484
3485 KeyboardInputMapper& mapper =
3486 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3487 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3488 NotifyKeyArgs args;
3489
3490 // Key down
3491 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3493 ASSERT_EQ(12, args.readTime);
3494
3495 // Key up
3496 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3498 ASSERT_EQ(15, args.readTime);
3499}
3500
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3503 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003504 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3505 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 KeyboardInputMapper& mapper =
3509 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3510 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
Arthur Hung95f68612022-04-07 14:08:22 +08003512 // Initial metastate is AMETA_NONE.
3513 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 NotifyKeyArgs args;
3518 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());
arthurhungdcef2dc2020-08-11 14:47:50 +08003521 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
3529 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534
3535 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003536 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3538 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003540 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541}
3542
3543TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3546 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 KeyboardInputMapper& mapper =
3550 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3551 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003553 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3557 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3558 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3559 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3560 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3561 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3562}
3563
3564TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 KeyboardInputMapper& mapper =
3572 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3573 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003575 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576 ASSERT_NO_FATAL_FAILURE(
3577 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3579 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3581 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3583 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 clearViewports();
3586 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 ASSERT_NO_FATAL_FAILURE(
3588 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3590 AKEYCODE_DPAD_UP, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3592 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3594 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003596 clearViewports();
3597 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_UP, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 clearViewports();
3608 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 ASSERT_NO_FATAL_FAILURE(
3610 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3612 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3614 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3615 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3616 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
3618 // Special case: if orientation changes while key is down, we still emit the same keycode
3619 // in the key up as we did in the key down.
3620 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003621 clearViewports();
3622 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3626 ASSERT_EQ(KEY_UP, args.scanCode);
3627 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3628
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003629 clearViewports();
3630 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3633 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3634 ASSERT_EQ(KEY_UP, args.scanCode);
3635 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3636}
3637
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3639 // If the keyboard is not orientation aware,
3640 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003643 KeyboardInputMapper& mapper =
3644 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 NotifyKeyArgs args;
3647
3648 // Display id should be ADISPLAY_ID_NONE without any display configuration.
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
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003655 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3660 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3661}
3662
3663TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3664 // If the keyboard is orientation aware,
3665 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
3670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003672 NotifyKeyArgs args;
3673
3674 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3675 // ^--- already checked by the previous test
3676
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003677 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003678 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3683 ASSERT_EQ(DISPLAY_ID, args.displayId);
3684
3685 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003686 clearViewports();
3687 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003688 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(newDisplayId, args.displayId);
3694}
3695
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 KeyboardInputMapper& mapper =
3698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3699 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706}
3707
Philip Junker4af3b3d2021-12-14 10:36:55 +01003708TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3709 KeyboardInputMapper& mapper =
3710 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3711 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3712
3713 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3714 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3715 << "If a mapping is available, the result is equal to the mapping";
3716
3717 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3718 << "If no mapping is available, the result is the key location";
3719}
3720
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 KeyboardInputMapper& mapper =
3723 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3724 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731}
3732
3733TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 KeyboardInputMapper& mapper =
3735 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3736 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003741 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 ASSERT_TRUE(flags[0]);
3743 ASSERT_FALSE(flags[1]);
3744}
3745
3746TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003747 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3748 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3749 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3750 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 KeyboardInputMapper& mapper =
3755 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3756 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003757 // Initial metastate is AMETA_NONE.
3758 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
3789 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3794 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
3805 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3809 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812}
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3817 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3819
3820 KeyboardInputMapper& mapper =
3821 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3822 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3823
Chris Yea52ade12020-08-27 16:49:20 -07003824 // Meta state should be AMETA_NONE after reset
3825 mapper.reset(ARBITRARY_TIME);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3828 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3829 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3830
3831 NotifyKeyArgs args;
3832 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(AMETA_NONE, args.metaState);
3836 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3837 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3838 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3839
3840 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(AMETA_NONE, args.metaState);
3844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3845 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3846 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3847}
3848
Arthur Hung2c9a3342019-07-23 14:18:59 +08003849TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3850 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855
3856 // keyboard 2.
3857 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003858 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003861 std::shared_ptr<InputDevice> device2 =
3862 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003863 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3866 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3867 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3868 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 KeyboardInputMapper& mapper =
3871 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3872 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003875 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003877 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3878 device2->reset(ARBITRARY_TIME);
3879
3880 // Prepared displays and associated info.
3881 constexpr uint8_t hdmi1 = 0;
3882 constexpr uint8_t hdmi2 = 1;
3883 const std::string SECONDARY_UNIQUE_ID = "local:1";
3884
3885 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3886 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3887
3888 // No associated display viewport found, should disable the device.
3889 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3891 ASSERT_FALSE(device2->isEnabled());
3892
3893 // Prepare second display.
3894 constexpr int32_t newDisplayId = 2;
3895 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003896 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003897 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003898 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003899 // Default device will reconfigure above, need additional reconfiguration for another device.
3900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3902
3903 // Device should be enabled after the associated display is found.
3904 ASSERT_TRUE(mDevice->isEnabled());
3905 ASSERT_TRUE(device2->isEnabled());
3906
3907 // Test pad key events
3908 ASSERT_NO_FATAL_FAILURE(
3909 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3911 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3912 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3913 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3915 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3916
3917 ASSERT_NO_FATAL_FAILURE(
3918 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3920 AKEYCODE_DPAD_RIGHT, newDisplayId));
3921 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3922 AKEYCODE_DPAD_DOWN, newDisplayId));
3923 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3924 AKEYCODE_DPAD_LEFT, newDisplayId));
3925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
arthurhungc903df12020-08-11 15:08:42 +08003927TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3928 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3929 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3930 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3932 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3934
3935 KeyboardInputMapper& mapper =
3936 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3937 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003938 // Initial metastate is AMETA_NONE.
3939 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003940
3941 // Initialization should have turned all of the lights off.
3942 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3943 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3945
3946 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003949 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3950 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3951
3952 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003955 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3956 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3957
3958 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003961 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3962 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3963
3964 mFakeEventHub->removeDevice(EVENTHUB_ID);
3965 mReader->loopOnce();
3966
3967 // keyboard 2 should default toggle keys.
3968 const std::string USB2 = "USB2";
3969 const std::string DEVICE_NAME2 = "KEYBOARD2";
3970 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3971 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3972 std::shared_ptr<InputDevice> device2 =
3973 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003974 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003975 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3976 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3977 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3978 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3979 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3980 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3981
arthurhung6fe95782020-10-05 22:41:16 +08003982 KeyboardInputMapper& mapper2 =
3983 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3984 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003985 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3986 device2->reset(ARBITRARY_TIME);
3987
3988 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3989 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3990 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003991 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3992 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003993}
3994
Arthur Hungcb40a002021-08-03 14:31:01 +00003995TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3998 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3999
4000 // Suppose we have two mappers. (DPAD + KEYBOARD)
4001 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4002 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4003 KeyboardInputMapper& mapper =
4004 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4005 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004006 // Initial metastate is AMETA_NONE.
4007 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004008
4009 mReader->toggleCapsLockState(DEVICE_ID);
4010 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4011}
4012
Arthur Hungfb3cc112022-04-13 07:39:50 +00004013TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4014 // keyboard 1.
4015 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4016 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4017 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4021
4022 KeyboardInputMapper& mapper1 =
4023 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4024 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4025
4026 // keyboard 2.
4027 const std::string USB2 = "USB2";
4028 const std::string DEVICE_NAME2 = "KEYBOARD2";
4029 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4030 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4031 std::shared_ptr<InputDevice> device2 =
4032 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4033 ftl::Flags<InputDeviceClass>(0));
4034 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4035 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4036 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4037 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4038 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4039 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4040
4041 KeyboardInputMapper& mapper2 =
4042 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4043 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4044 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4045 device2->reset(ARBITRARY_TIME);
4046
Arthur Hung95f68612022-04-07 14:08:22 +08004047 // Initial metastate is AMETA_NONE.
4048 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4049 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4050
4051 // Toggle num lock on and off.
4052 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4053 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004054 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4055 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4056 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4057
4058 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4059 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4060 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4061 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4062 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4063
4064 // Toggle caps lock on and off.
4065 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4066 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4067 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4068 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4069 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4070
4071 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4072 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4073 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4074 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4075 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4076
4077 // Toggle scroll lock on and off.
4078 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4079 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4080 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4081 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4083
4084 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4086 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4087 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4088 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4089}
4090
Arthur Hung2141d542022-08-23 07:45:21 +00004091TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4092 const int32_t USAGE_A = 0x070004;
4093 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4094 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4095
4096 KeyboardInputMapper& mapper =
4097 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4098 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4099 // Key down by scan code.
4100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4101 NotifyKeyArgs args;
4102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4103 ASSERT_EQ(DEVICE_ID, args.deviceId);
4104 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4105 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4106 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4107 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4108 ASSERT_EQ(KEY_HOME, args.scanCode);
4109 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4110
4111 // Disable device, it should synthesize cancellation events for down events.
4112 mFakePolicy->addDisabledDevice(DEVICE_ID);
4113 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4114
4115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4117 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4118 ASSERT_EQ(KEY_HOME, args.scanCode);
4119 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4120}
4121
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004122// --- KeyboardInputMapperTest_ExternalDevice ---
4123
4124class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4125protected:
Chris Yea52ade12020-08-27 16:49:20 -07004126 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127};
4128
4129TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004130 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4131 // marked as WAKE in the keylayout file to trigger wake.
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, 0);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4136 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004137
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(uint32_t(0), args.policyFlags);
4150
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 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_PLAY, 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_PLAYPAUSE, 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_PLAYPAUSE, 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
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004168TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004169 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004170
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174
Powei Fengd041c5d2019-05-03 17:11:33 -07004175 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 KeyboardInputMapper& mapper =
4177 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4178 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004179
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004181 NotifyKeyArgs args;
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4183 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4184
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4187 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4188
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4191 ASSERT_EQ(uint32_t(0), args.policyFlags);
4192
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4195 ASSERT_EQ(uint32_t(0), args.policyFlags);
4196
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4199 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4200
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4203 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4204}
4205
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206// --- CursorInputMapperTest ---
4207
4208class CursorInputMapperTest : public InputMapperTest {
4209protected:
4210 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4211
Michael Wright17db18e2020-06-26 20:51:44 +01004212 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
Chris Yea52ade12020-08-27 16:49:20 -07004214 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 InputMapperTest::SetUp();
4216
Michael Wright17db18e2020-06-26 20:51:44 +01004217 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004218 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 }
4220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4222 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004223
4224 void prepareDisplay(int32_t orientation) {
4225 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004226 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004227 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4228 orientation, uniqueId, NO_PORT, viewportType);
4229 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230
4231 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4232 float pressure) {
4233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4234 0.0f, 0.0f, 0.0f, EPSILON));
4235 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236};
4237
4238const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4241 int32_t originalY, int32_t rotatedX,
4242 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 NotifyMotionArgs args;
4244
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(args.pointerCoords[0],
4252 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4253 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254}
4255
4256TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004258 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004260 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261}
4262
4263TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004265 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268}
4269
4270TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
4274 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004275 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
4277 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004278 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4279 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4281 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4282
4283 // When the bounds are set, then there should be a valid motion range.
4284 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4285
4286 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004287 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
4289 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4290 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4291 1, 800 - 1, 0.0f, 0.0f));
4292 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4293 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4294 2, 480 - 1, 0.0f, 0.0f));
4295 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4296 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4297 0.0f, 1.0f, 0.0f, 0.0f));
4298}
4299
4300TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004305 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
4307 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4308 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4309 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4310 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4311 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4312 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4313 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4314 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4315 0.0f, 1.0f, 0.0f, 0.0f));
4316}
4317
4318TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
arthurhungdcef2dc2020-08-11 14:47:50 +08004322 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323
4324 NotifyMotionArgs args;
4325
4326 // Button press.
4327 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4331 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4332 ASSERT_EQ(DEVICE_ID, args.deviceId);
4333 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4334 ASSERT_EQ(uint32_t(0), args.policyFlags);
4335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4336 ASSERT_EQ(0, args.flags);
4337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4338 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4339 ASSERT_EQ(0, args.edgeFlags);
4340 ASSERT_EQ(uint32_t(1), args.pointerCount);
4341 ASSERT_EQ(0, args.pointerProperties[0].id);
4342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4345 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4346 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4347
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4349 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4350 ASSERT_EQ(DEVICE_ID, args.deviceId);
4351 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4352 ASSERT_EQ(uint32_t(0), args.policyFlags);
4353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4354 ASSERT_EQ(0, args.flags);
4355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4356 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4357 ASSERT_EQ(0, args.edgeFlags);
4358 ASSERT_EQ(uint32_t(1), args.pointerCount);
4359 ASSERT_EQ(0, args.pointerProperties[0].id);
4360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4363 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4364 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4365
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4368 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4370 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4371 ASSERT_EQ(DEVICE_ID, args.deviceId);
4372 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4373 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4375 ASSERT_EQ(0, args.flags);
4376 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4377 ASSERT_EQ(0, args.buttonState);
4378 ASSERT_EQ(0, args.edgeFlags);
4379 ASSERT_EQ(uint32_t(1), args.pointerCount);
4380 ASSERT_EQ(0, args.pointerProperties[0].id);
4381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004383 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4384 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4385 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4386
4387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4388 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4389 ASSERT_EQ(DEVICE_ID, args.deviceId);
4390 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4391 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4393 ASSERT_EQ(0, args.flags);
4394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4395 ASSERT_EQ(0, args.buttonState);
4396 ASSERT_EQ(0, args.edgeFlags);
4397 ASSERT_EQ(uint32_t(1), args.pointerCount);
4398 ASSERT_EQ(0, args.pointerProperties[0].id);
4399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4402 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4403 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4404}
4405
4406TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004408 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409
4410 NotifyMotionArgs args;
4411
4412 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4418 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4419 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420
4421 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4427 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428}
4429
4430TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004432 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
4434 NotifyMotionArgs args;
4435
4436 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
4454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457}
4458
4459TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004461 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462
4463 NotifyMotionArgs args;
4464
4465 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4471 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4473 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4474 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4479 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4480 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004481
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004488 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4489 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4490 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
4492 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004497 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004498
4499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004501 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502}
4503
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004504TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004506 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4507 // need to be rotated.
4508 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004509 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004511 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4520}
4521
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004522TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004524 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4525 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004526 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004528 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4537
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004538 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4542 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4543 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4544 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004547
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004548 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4553 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4554 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4555 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4557
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004558 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4564 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4565 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4566 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567}
4568
4569TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004571 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572
4573 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4574 mFakePointerController->setPosition(100, 200);
4575 mFakePointerController->setButtonState(0);
4576
4577 NotifyMotionArgs motionArgs;
4578 NotifyKeyArgs keyArgs;
4579
4580 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4584 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4585 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4586 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004587 ASSERT_NO_FATAL_FAILURE(
4588 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4592 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4593 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004594 ASSERT_NO_FATAL_FAILURE(
4595 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004596
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_EQ(0, motionArgs.buttonState);
4602 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(
4604 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_EQ(0, motionArgs.buttonState);
4609 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004610 ASSERT_NO_FATAL_FAILURE(
4611 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(0, motionArgs.buttonState);
4616 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(
4618 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619
4620 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4626 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4627 motionArgs.buttonState);
4628 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4629 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004630 ASSERT_NO_FATAL_FAILURE(
4631 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4635 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4636 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4637 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004638 ASSERT_NO_FATAL_FAILURE(
4639 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4643 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4644 motionArgs.buttonState);
4645 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4646 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004647 ASSERT_NO_FATAL_FAILURE(
4648 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004649
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004653 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4655 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004656 ASSERT_NO_FATAL_FAILURE(
4657 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4662 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004663 ASSERT_NO_FATAL_FAILURE(
4664 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4670 ASSERT_EQ(0, motionArgs.buttonState);
4671 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004672 ASSERT_NO_FATAL_FAILURE(
4673 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_EQ(0, motionArgs.buttonState);
4679 ASSERT_EQ(0, mFakePointerController->getButtonState());
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004683
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4685 ASSERT_EQ(0, motionArgs.buttonState);
4686 ASSERT_EQ(0, mFakePointerController->getButtonState());
4687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
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
4691 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4701 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004704
4705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4706 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4708 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004709 ASSERT_NO_FATAL_FAILURE(
4710 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_EQ(0, motionArgs.buttonState);
4717 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004718 ASSERT_NO_FATAL_FAILURE(
4719 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, mFakePointerController->getButtonState());
4725
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004726 ASSERT_NO_FATAL_FAILURE(
4727 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4731
4732 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4737 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4742 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004743 ASSERT_NO_FATAL_FAILURE(
4744 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745
4746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4749 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004750 ASSERT_NO_FATAL_FAILURE(
4751 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004759 ASSERT_NO_FATAL_FAILURE(
4760 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004761
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4763 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4764 ASSERT_EQ(0, motionArgs.buttonState);
4765 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004766 ASSERT_NO_FATAL_FAILURE(
4767 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4770 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4771 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4772
4773 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4778 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004779
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4783 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004784 ASSERT_NO_FATAL_FAILURE(
4785 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4790 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004791 ASSERT_NO_FATAL_FAILURE(
4792 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_EQ(0, motionArgs.buttonState);
4799 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004800 ASSERT_NO_FATAL_FAILURE(
4801 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004802
4803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4805 ASSERT_EQ(0, motionArgs.buttonState);
4806 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(
4808 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004809
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4812 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4813
4814 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4818 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4819 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004820
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4824 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004825 ASSERT_NO_FATAL_FAILURE(
4826 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004827
4828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4830 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4831 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004832 ASSERT_NO_FATAL_FAILURE(
4833 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_EQ(0, motionArgs.buttonState);
4840 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004841 ASSERT_NO_FATAL_FAILURE(
4842 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4846 ASSERT_EQ(0, motionArgs.buttonState);
4847 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004848 ASSERT_NO_FATAL_FAILURE(
4849 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004850
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4852 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4853 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4854}
4855
4856TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004858 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859
4860 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4861 mFakePointerController->setPosition(100, 200);
4862 mFakePointerController->setButtonState(0);
4863
4864 NotifyMotionArgs args;
4865
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004870 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4873 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 +01004874 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004875}
4876
4877TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004878 addConfigurationProperty("cursor.mode", "pointer");
4879 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004880 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004881
4882 NotifyDeviceResetArgs resetArgs;
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4884 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4885 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4886
4887 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4888 mFakePointerController->setPosition(100, 200);
4889 mFakePointerController->setButtonState(0);
4890
4891 NotifyMotionArgs args;
4892
4893 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4898 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4901 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 +01004902 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004903
4904 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4908 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4909 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4911 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4913 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4916 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4917
4918 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004919 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4920 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4922 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4925 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4927 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4928 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4930 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4931
4932 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4937 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4940 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 +01004941 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004942
4943 // Disable pointer capture and check that the device generation got bumped
4944 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004945 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004946 mFakePolicy->setPointerCapture(false);
4947 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004948 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004951 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4952
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4957 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4960 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 +01004961 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962}
4963
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004964/**
4965 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4966 * pointer acceleration or speed processing should not be applied.
4967 */
4968TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4969 addConfigurationProperty("cursor.mode", "pointer");
4970 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4971 100.f /*high threshold*/, 10.f /*acceleration*/);
4972 mFakePolicy->setVelocityControlParams(testParams);
4973 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4974
4975 NotifyDeviceResetArgs resetArgs;
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4977 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4978 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4979
4980 NotifyMotionArgs args;
4981
4982 // Move and verify scale is applied.
4983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4987 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4989 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4990 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4991 ASSERT_GT(relX, 10);
4992 ASSERT_GT(relY, 20);
4993
4994 // Enable Pointer Capture
4995 mFakePolicy->setPointerCapture(true);
4996 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4997 NotifyPointerCaptureChangedArgs captureArgs;
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4999 ASSERT_TRUE(captureArgs.request.enable);
5000
5001 // Move and verify scale is not applied.
5002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5006 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5008 ASSERT_EQ(10, args.pointerCoords[0].getX());
5009 ASSERT_EQ(20, args.pointerCoords[0].getY());
5010}
5011
Prabir Pradhan208360b2022-06-24 18:37:04 +00005012TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5013 addConfigurationProperty("cursor.mode", "pointer");
5014 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5015
5016 NotifyDeviceResetArgs resetArgs;
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5018 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5019 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5020
5021 // Ensure the display is rotated.
5022 prepareDisplay(DISPLAY_ORIENTATION_90);
5023
5024 NotifyMotionArgs args;
5025
5026 // Verify that the coordinates are rotated.
5027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5031 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5033 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5034 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5035
5036 // Enable Pointer Capture.
5037 mFakePolicy->setPointerCapture(true);
5038 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5039 NotifyPointerCaptureChangedArgs captureArgs;
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5041 ASSERT_TRUE(captureArgs.request.enable);
5042
5043 // Move and verify rotation is not applied.
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5048 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5050 ASSERT_EQ(10, args.pointerCoords[0].getX());
5051 ASSERT_EQ(20, args.pointerCoords[0].getY());
5052}
5053
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005054TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005055 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005056
Garfield Tan888a6a42020-01-09 11:39:16 -08005057 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005058 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005059 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5060 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005061 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5062 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005063 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5064 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5065
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005066 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5067 mFakePointerController->setPosition(100, 200);
5068 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005069
5070 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5075 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5078 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 +01005079 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005080 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5081}
5082
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083// --- TouchInputMapperTest ---
5084
5085class TouchInputMapperTest : public InputMapperTest {
5086protected:
5087 static const int32_t RAW_X_MIN;
5088 static const int32_t RAW_X_MAX;
5089 static const int32_t RAW_Y_MIN;
5090 static const int32_t RAW_Y_MAX;
5091 static const int32_t RAW_TOUCH_MIN;
5092 static const int32_t RAW_TOUCH_MAX;
5093 static const int32_t RAW_TOOL_MIN;
5094 static const int32_t RAW_TOOL_MAX;
5095 static const int32_t RAW_PRESSURE_MIN;
5096 static const int32_t RAW_PRESSURE_MAX;
5097 static const int32_t RAW_ORIENTATION_MIN;
5098 static const int32_t RAW_ORIENTATION_MAX;
5099 static const int32_t RAW_DISTANCE_MIN;
5100 static const int32_t RAW_DISTANCE_MAX;
5101 static const int32_t RAW_TILT_MIN;
5102 static const int32_t RAW_TILT_MAX;
5103 static const int32_t RAW_ID_MIN;
5104 static const int32_t RAW_ID_MAX;
5105 static const int32_t RAW_SLOT_MIN;
5106 static const int32_t RAW_SLOT_MAX;
5107 static const float X_PRECISION;
5108 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005109 static const float X_PRECISION_VIRTUAL;
5110 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005113 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
5115 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5116
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005117 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005118 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005119
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120 enum Axes {
5121 POSITION = 1 << 0,
5122 TOUCH = 1 << 1,
5123 TOOL = 1 << 2,
5124 PRESSURE = 1 << 3,
5125 ORIENTATION = 1 << 4,
5126 MINOR = 1 << 5,
5127 ID = 1 << 6,
5128 DISTANCE = 1 << 7,
5129 TILT = 1 << 8,
5130 SLOT = 1 << 9,
5131 TOOL_TYPE = 1 << 10,
5132 };
5133
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005134 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5135 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005136 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005138 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 int32_t toRawX(float displayX);
5140 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005141 int32_t toRotatedRawX(float displayX);
5142 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005143 float toCookedX(float rawX, float rawY);
5144 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005146 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005148 float toDisplayY(int32_t rawY, int32_t displayHeight);
5149
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150};
5151
5152const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5153const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5154const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5155const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5156const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5157const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5158const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5159const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005160const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5161const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5163const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5164const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5165const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5166const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5167const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5168const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5169const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5170const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5171const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5172const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5173const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005174const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5175 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5176const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5177 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005178const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5179 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180
5181const float TouchInputMapperTest::GEOMETRIC_SCALE =
5182 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5183 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5184
5185const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5186 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5187 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5188};
5189
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005190void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005191 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5192 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005193}
5194
5195void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5196 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5197 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198}
5199
Santos Cordonfa5cf462017-04-05 10:37:00 -07005200void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005201 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5202 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5203 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005204}
5205
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005207 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5208 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5210 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211}
5212
Jason Gerecke489fda82012-09-07 17:19:40 -07005213void TouchInputMapperTest::prepareLocationCalibration() {
5214 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5215}
5216
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217int32_t TouchInputMapperTest::toRawX(float displayX) {
5218 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5219}
5220
5221int32_t TouchInputMapperTest::toRawY(float displayY) {
5222 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5223}
5224
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005225int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5226 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5227}
5228
5229int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5230 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5231}
5232
Jason Gerecke489fda82012-09-07 17:19:40 -07005233float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5234 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5235 return rawX;
5236}
5237
5238float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5239 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5240 return rawY;
5241}
5242
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005244 return toDisplayX(rawX, DISPLAY_WIDTH);
5245}
5246
5247float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5248 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249}
5250
5251float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005252 return toDisplayY(rawY, DISPLAY_HEIGHT);
5253}
5254
5255float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5256 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257}
5258
5259
5260// --- SingleTouchInputMapperTest ---
5261
5262class SingleTouchInputMapperTest : public TouchInputMapperTest {
5263protected:
5264 void prepareButtons();
5265 void prepareAxes(int axes);
5266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005267 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5268 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5269 void processUp(SingleTouchInputMapper& mappery);
5270 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5271 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5272 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5273 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5274 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5275 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276};
5277
5278void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005279 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
5282void SingleTouchInputMapperTest::prepareAxes(int axes) {
5283 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005284 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286 }
5287 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005288 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5289 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290 }
5291 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005292 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5293 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 }
5295 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5297 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298 }
5299 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005300 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 }
5303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309}
5310
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005311void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318}
5319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322}
5323
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5325 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327}
5328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331}
5332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5334 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337}
5338
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005339void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5340 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342}
5343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346}
5347
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349 prepareButtons();
5350 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354}
5355
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 prepareButtons();
5358 prepareAxes(POSITION);
5359 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005360 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363}
5364
5365TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366 addConfigurationProperty("touch.deviceType", "touchScreen");
5367 prepareDisplay(DISPLAY_ORIENTATION_0);
5368 prepareButtons();
5369 prepareAxes(POSITION);
5370 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372
5373 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
5376 // Virtual key is down.
5377 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5378 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5379 processDown(mapper, x, y);
5380 processSync(mapper);
5381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384
5385 // Virtual key is up.
5386 processUp(mapper);
5387 processSync(mapper);
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391}
5392
5393TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 addConfigurationProperty("touch.deviceType", "touchScreen");
5395 prepareDisplay(DISPLAY_ORIENTATION_0);
5396 prepareButtons();
5397 prepareAxes(POSITION);
5398 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400
5401 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
5404 // Virtual key is down.
5405 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5406 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5407 processDown(mapper, x, y);
5408 processSync(mapper);
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005411 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
5413 // Virtual key is up.
5414 processUp(mapper);
5415 processSync(mapper);
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5417
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419}
5420
5421TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005430 ASSERT_TRUE(
5431 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432 ASSERT_TRUE(flags[0]);
5433 ASSERT_FALSE(flags[1]);
5434}
5435
5436TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 addConfigurationProperty("touch.deviceType", "touchScreen");
5438 prepareDisplay(DISPLAY_ORIENTATION_0);
5439 prepareButtons();
5440 prepareAxes(POSITION);
5441 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005442 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
arthurhungdcef2dc2020-08-11 14:47:50 +08005444 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445
5446 NotifyKeyArgs args;
5447
5448 // Press virtual key.
5449 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5450 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5451 processDown(mapper, x, y);
5452 processSync(mapper);
5453
5454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5455 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5456 ASSERT_EQ(DEVICE_ID, args.deviceId);
5457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5458 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5459 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5460 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5461 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5462 ASSERT_EQ(KEY_HOME, args.scanCode);
5463 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5464 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5465
5466 // Release virtual key.
5467 processUp(mapper);
5468 processSync(mapper);
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5471 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5472 ASSERT_EQ(DEVICE_ID, args.deviceId);
5473 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5474 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5475 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5476 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5477 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5478 ASSERT_EQ(KEY_HOME, args.scanCode);
5479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5480 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5481
5482 // Should not have sent any motions.
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5484}
5485
5486TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 addConfigurationProperty("touch.deviceType", "touchScreen");
5488 prepareDisplay(DISPLAY_ORIENTATION_0);
5489 prepareButtons();
5490 prepareAxes(POSITION);
5491 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
arthurhungdcef2dc2020-08-11 14:47:50 +08005494 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 NotifyKeyArgs keyArgs;
5497
5498 // Press virtual key.
5499 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5500 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5501 processDown(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_DOWN, keyArgs.action);
5510 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5511 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5512 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5514 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5515
5516 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5517 // into the display area.
5518 y -= 100;
5519 processMove(mapper, x, y);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5523 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5524 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5525 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5526 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5528 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5529 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5530 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5531 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5533 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5534
5535 NotifyMotionArgs motionArgs;
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5538 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5539 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5540 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5541 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5542 ASSERT_EQ(0, motionArgs.flags);
5543 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5544 ASSERT_EQ(0, motionArgs.buttonState);
5545 ASSERT_EQ(0, motionArgs.edgeFlags);
5546 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5550 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5551 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5552 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5554
5555 // Keep moving out of bounds. Should generate a pointer move.
5556 y -= 50;
5557 processMove(mapper, x, y);
5558 processSync(mapper);
5559
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5562 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5563 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5564 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5566 ASSERT_EQ(0, motionArgs.flags);
5567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5568 ASSERT_EQ(0, motionArgs.buttonState);
5569 ASSERT_EQ(0, motionArgs.edgeFlags);
5570 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5574 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5575 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5576 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5578
5579 // Release out of bounds. Should generate a pointer up.
5580 processUp(mapper);
5581 processSync(mapper);
5582
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5589 ASSERT_EQ(0, motionArgs.flags);
5590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5591 ASSERT_EQ(0, motionArgs.buttonState);
5592 ASSERT_EQ(0, motionArgs.edgeFlags);
5593 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5597 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5598 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5599 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5601
5602 // Should not have sent any more keys or motions.
5603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5605}
5606
5607TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608 addConfigurationProperty("touch.deviceType", "touchScreen");
5609 prepareDisplay(DISPLAY_ORIENTATION_0);
5610 prepareButtons();
5611 prepareAxes(POSITION);
5612 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005613 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614
arthurhungdcef2dc2020-08-11 14:47:50 +08005615 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616
5617 NotifyMotionArgs motionArgs;
5618
5619 // Initially go down out of bounds.
5620 int32_t x = -10;
5621 int32_t y = -10;
5622 processDown(mapper, x, y);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5626
5627 // Move into the display area. Should generate a pointer down.
5628 x = 50;
5629 y = 75;
5630 processMove(mapper, x, y);
5631 processSync(mapper);
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5635 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5636 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5637 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5639 ASSERT_EQ(0, motionArgs.flags);
5640 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5641 ASSERT_EQ(0, motionArgs.buttonState);
5642 ASSERT_EQ(0, motionArgs.edgeFlags);
5643 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5644 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5647 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5648 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5649 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5651
5652 // Release. Should generate a pointer up.
5653 processUp(mapper);
5654 processSync(mapper);
5655
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5658 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5659 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5660 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5661 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5662 ASSERT_EQ(0, motionArgs.flags);
5663 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5664 ASSERT_EQ(0, motionArgs.buttonState);
5665 ASSERT_EQ(0, motionArgs.edgeFlags);
5666 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5670 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5671 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5672 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5674
5675 // Should not have sent any more keys or motions.
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5678}
5679
Santos Cordonfa5cf462017-04-05 10:37:00 -07005680TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005681 addConfigurationProperty("touch.deviceType", "touchScreen");
5682 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5683
5684 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5685 prepareButtons();
5686 prepareAxes(POSITION);
5687 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005688 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005689
arthurhungdcef2dc2020-08-11 14:47:50 +08005690 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005691
5692 NotifyMotionArgs motionArgs;
5693
5694 // Down.
5695 int32_t x = 100;
5696 int32_t y = 125;
5697 processDown(mapper, x, y);
5698 processSync(mapper);
5699
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5701 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5702 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5703 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5704 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5705 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5706 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5707 ASSERT_EQ(0, motionArgs.flags);
5708 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5709 ASSERT_EQ(0, motionArgs.buttonState);
5710 ASSERT_EQ(0, motionArgs.edgeFlags);
5711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5715 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5716 1, 0, 0, 0, 0, 0, 0, 0));
5717 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5718 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5720
5721 // Move.
5722 x += 50;
5723 y += 75;
5724 processMove(mapper, x, y);
5725 processSync(mapper);
5726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5728 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5729 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5730 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5731 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5732 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5734 ASSERT_EQ(0, motionArgs.flags);
5735 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5736 ASSERT_EQ(0, motionArgs.buttonState);
5737 ASSERT_EQ(0, motionArgs.edgeFlags);
5738 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5739 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5742 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5743 1, 0, 0, 0, 0, 0, 0, 0));
5744 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5745 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5747
5748 // Up.
5749 processUp(mapper);
5750 processSync(mapper);
5751
5752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5753 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5754 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5755 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5756 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5757 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5759 ASSERT_EQ(0, motionArgs.flags);
5760 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5761 ASSERT_EQ(0, motionArgs.buttonState);
5762 ASSERT_EQ(0, motionArgs.edgeFlags);
5763 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5764 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5767 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5768 1, 0, 0, 0, 0, 0, 0, 0));
5769 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5770 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5772
5773 // Should not have sent any more keys or motions.
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5776}
5777
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779 addConfigurationProperty("touch.deviceType", "touchScreen");
5780 prepareDisplay(DISPLAY_ORIENTATION_0);
5781 prepareButtons();
5782 prepareAxes(POSITION);
5783 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005784 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785
arthurhungdcef2dc2020-08-11 14:47:50 +08005786 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787
5788 NotifyMotionArgs motionArgs;
5789
5790 // Down.
5791 int32_t x = 100;
5792 int32_t y = 125;
5793 processDown(mapper, x, y);
5794 processSync(mapper);
5795
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5798 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5799 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5800 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5802 ASSERT_EQ(0, motionArgs.flags);
5803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5804 ASSERT_EQ(0, motionArgs.buttonState);
5805 ASSERT_EQ(0, motionArgs.edgeFlags);
5806 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5810 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5811 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5812 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5814
5815 // Move.
5816 x += 50;
5817 y += 75;
5818 processMove(mapper, x, y);
5819 processSync(mapper);
5820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5823 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5824 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5825 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5827 ASSERT_EQ(0, motionArgs.flags);
5828 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5829 ASSERT_EQ(0, motionArgs.buttonState);
5830 ASSERT_EQ(0, motionArgs.edgeFlags);
5831 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5832 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5835 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5836 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5837 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5839
5840 // Up.
5841 processUp(mapper);
5842 processSync(mapper);
5843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5846 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5847 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5848 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5850 ASSERT_EQ(0, motionArgs.flags);
5851 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5852 ASSERT_EQ(0, motionArgs.buttonState);
5853 ASSERT_EQ(0, motionArgs.edgeFlags);
5854 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5858 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5859 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5860 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5862
5863 // Should not have sent any more keys or motions.
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5866}
5867
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005868TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 addConfigurationProperty("touch.deviceType", "touchScreen");
5870 prepareButtons();
5871 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005872 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5873 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005874 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875
5876 NotifyMotionArgs args;
5877
5878 // Rotation 90.
5879 prepareDisplay(DISPLAY_ORIENTATION_90);
5880 processDown(mapper, toRawX(50), toRawY(75));
5881 processSync(mapper);
5882
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5884 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5885 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5886
5887 processUp(mapper);
5888 processSync(mapper);
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5890}
5891
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005892TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 addConfigurationProperty("touch.deviceType", "touchScreen");
5894 prepareButtons();
5895 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005896 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5897 // orientation-aware are affected by display rotation.
5898 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005899 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900
5901 NotifyMotionArgs args;
5902
5903 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005904 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 prepareDisplay(DISPLAY_ORIENTATION_0);
5906 processDown(mapper, toRawX(50), toRawY(75));
5907 processSync(mapper);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5910 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5911 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5912
5913 processUp(mapper);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5916
5917 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005918 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005919 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005920 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921 processSync(mapper);
5922
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5924 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5925 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5926
5927 processUp(mapper);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5930
5931 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005932 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 prepareDisplay(DISPLAY_ORIENTATION_180);
5934 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5935 processSync(mapper);
5936
5937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5938 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5939 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5940
5941 processUp(mapper);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5944
5945 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005946 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005948 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949 processSync(mapper);
5950
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5952 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5953 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5954
5955 processUp(mapper);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5958}
5959
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005960TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5961 addConfigurationProperty("touch.deviceType", "touchScreen");
5962 prepareButtons();
5963 prepareAxes(POSITION);
5964 addConfigurationProperty("touch.orientationAware", "1");
5965 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5966 clearViewports();
5967 prepareDisplay(DISPLAY_ORIENTATION_0);
5968 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5969 NotifyMotionArgs args;
5970
5971 // Orientation 0.
5972 processDown(mapper, toRawX(50), toRawY(75));
5973 processSync(mapper);
5974
5975 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5976 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5977 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5978
5979 processUp(mapper);
5980 processSync(mapper);
5981 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5982}
5983
5984TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5985 addConfigurationProperty("touch.deviceType", "touchScreen");
5986 prepareButtons();
5987 prepareAxes(POSITION);
5988 addConfigurationProperty("touch.orientationAware", "1");
5989 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5990 clearViewports();
5991 prepareDisplay(DISPLAY_ORIENTATION_0);
5992 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5993 NotifyMotionArgs args;
5994
5995 // Orientation 90.
5996 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5997 processSync(mapper);
5998
5999 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6000 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6001 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6002
6003 processUp(mapper);
6004 processSync(mapper);
6005 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6006}
6007
6008TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6009 addConfigurationProperty("touch.deviceType", "touchScreen");
6010 prepareButtons();
6011 prepareAxes(POSITION);
6012 addConfigurationProperty("touch.orientationAware", "1");
6013 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6014 clearViewports();
6015 prepareDisplay(DISPLAY_ORIENTATION_0);
6016 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6017 NotifyMotionArgs args;
6018
6019 // Orientation 180.
6020 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6021 processSync(mapper);
6022
6023 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6024 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6025 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6026
6027 processUp(mapper);
6028 processSync(mapper);
6029 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6030}
6031
6032TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6033 addConfigurationProperty("touch.deviceType", "touchScreen");
6034 prepareButtons();
6035 prepareAxes(POSITION);
6036 addConfigurationProperty("touch.orientationAware", "1");
6037 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6038 clearViewports();
6039 prepareDisplay(DISPLAY_ORIENTATION_0);
6040 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6041 NotifyMotionArgs args;
6042
6043 // Orientation 270.
6044 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6045 processSync(mapper);
6046
6047 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6048 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6049 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6050
6051 processUp(mapper);
6052 processSync(mapper);
6053 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6054}
6055
6056TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6057 addConfigurationProperty("touch.deviceType", "touchScreen");
6058 prepareButtons();
6059 prepareAxes(POSITION);
6060 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6061 // orientation-aware are affected by display rotation.
6062 addConfigurationProperty("touch.orientationAware", "0");
6063 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6064 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6065
6066 NotifyMotionArgs args;
6067
6068 // Orientation 90, Rotation 0.
6069 clearViewports();
6070 prepareDisplay(DISPLAY_ORIENTATION_0);
6071 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6072 processSync(mapper);
6073
6074 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6075 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6076 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6077
6078 processUp(mapper);
6079 processSync(mapper);
6080 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6081
6082 // Orientation 90, Rotation 90.
6083 clearViewports();
6084 prepareDisplay(DISPLAY_ORIENTATION_90);
6085 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6086 processSync(mapper);
6087
6088 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6089 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6090 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6091
6092 processUp(mapper);
6093 processSync(mapper);
6094 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6095
6096 // Orientation 90, Rotation 180.
6097 clearViewports();
6098 prepareDisplay(DISPLAY_ORIENTATION_180);
6099 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6100 processSync(mapper);
6101
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6103 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6104 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6105
6106 processUp(mapper);
6107 processSync(mapper);
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6109
6110 // Orientation 90, Rotation 270.
6111 clearViewports();
6112 prepareDisplay(DISPLAY_ORIENTATION_270);
6113 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6114 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6115 processSync(mapper);
6116
6117 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6118 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6119 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6120
6121 processUp(mapper);
6122 processSync(mapper);
6123 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6124}
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 addConfigurationProperty("touch.deviceType", "touchScreen");
6128 prepareDisplay(DISPLAY_ORIENTATION_0);
6129 prepareButtons();
6130 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 // These calculations are based on the input device calibration documentation.
6134 int32_t rawX = 100;
6135 int32_t rawY = 200;
6136 int32_t rawPressure = 10;
6137 int32_t rawToolMajor = 12;
6138 int32_t rawDistance = 2;
6139 int32_t rawTiltX = 30;
6140 int32_t rawTiltY = 110;
6141
6142 float x = toDisplayX(rawX);
6143 float y = toDisplayY(rawY);
6144 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6145 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6146 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6147 float distance = float(rawDistance);
6148
6149 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6150 float tiltScale = M_PI / 180;
6151 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6152 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6153 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6154 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6155
6156 processDown(mapper, rawX, rawY);
6157 processPressure(mapper, rawPressure);
6158 processToolMajor(mapper, rawToolMajor);
6159 processDistance(mapper, rawDistance);
6160 processTilt(mapper, rawTiltX, rawTiltY);
6161 processSync(mapper);
6162
6163 NotifyMotionArgs args;
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6165 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6166 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6167 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6168}
6169
Jason Gerecke489fda82012-09-07 17:19:40 -07006170TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006171 addConfigurationProperty("touch.deviceType", "touchScreen");
6172 prepareDisplay(DISPLAY_ORIENTATION_0);
6173 prepareLocationCalibration();
6174 prepareButtons();
6175 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006176 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006177
6178 int32_t rawX = 100;
6179 int32_t rawY = 200;
6180
6181 float x = toDisplayX(toCookedX(rawX, rawY));
6182 float y = toDisplayY(toCookedY(rawX, rawY));
6183
6184 processDown(mapper, rawX, rawY);
6185 processSync(mapper);
6186
6187 NotifyMotionArgs args;
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6190 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6191}
6192
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 addConfigurationProperty("touch.deviceType", "touchScreen");
6195 prepareDisplay(DISPLAY_ORIENTATION_0);
6196 prepareButtons();
6197 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006198 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 NotifyMotionArgs motionArgs;
6201 NotifyKeyArgs keyArgs;
6202
6203 processDown(mapper, 100, 200);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6207 ASSERT_EQ(0, motionArgs.buttonState);
6208
6209 // press BTN_LEFT, release BTN_LEFT
6210 processKey(mapper, BTN_LEFT, 1);
6211 processSync(mapper);
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6215
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6218 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6219
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 processKey(mapper, BTN_LEFT, 0);
6221 processSync(mapper);
6222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229
6230 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6231 processKey(mapper, BTN_RIGHT, 1);
6232 processKey(mapper, BTN_MIDDLE, 1);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6237 motionArgs.buttonState);
6238
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6242
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6245 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6246 motionArgs.buttonState);
6247
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248 processKey(mapper, BTN_RIGHT, 0);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006253
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257
6258 processKey(mapper, BTN_MIDDLE, 0);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 // press BTN_BACK, release BTN_BACK
6269 processKey(mapper, BTN_BACK, 1);
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6272 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6273 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6281 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282
6283 processKey(mapper, BTN_BACK, 0);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(0, motionArgs.buttonState);
6292
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6294 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6295 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6296
6297 // press BTN_SIDE, release BTN_SIDE
6298 processKey(mapper, BTN_SIDE, 1);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6301 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6302 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311
6312 processKey(mapper, BTN_SIDE, 0);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(0, motionArgs.buttonState);
6321
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6323 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6324 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6325
6326 // press BTN_FORWARD, release BTN_FORWARD
6327 processKey(mapper, BTN_FORWARD, 1);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6331 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006332
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6339 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340
6341 processKey(mapper, BTN_FORWARD, 0);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(0, motionArgs.buttonState);
6350
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6353 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6354
6355 // press BTN_EXTRA, release BTN_EXTRA
6356 processKey(mapper, BTN_EXTRA, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006361
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6365
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6368 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006369
6370 processKey(mapper, BTN_EXTRA, 0);
6371 processSync(mapper);
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375
6376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006378 ASSERT_EQ(0, motionArgs.buttonState);
6379
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6381 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6382 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6383
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6385
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 // press BTN_STYLUS, release BTN_STYLUS
6387 processKey(mapper, BTN_STYLUS, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6395 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 processKey(mapper, BTN_STYLUS, 0);
6398 processSync(mapper);
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 // press BTN_STYLUS2, release BTN_STYLUS2
6408 processKey(mapper, BTN_STYLUS2, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6413
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6416 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417
6418 processKey(mapper, BTN_STYLUS2, 0);
6419 processSync(mapper);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006423
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006426 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427
6428 // release touch
6429 processUp(mapper);
6430 processSync(mapper);
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6433 ASSERT_EQ(0, motionArgs.buttonState);
6434}
6435
6436TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 addConfigurationProperty("touch.deviceType", "touchScreen");
6438 prepareDisplay(DISPLAY_ORIENTATION_0);
6439 prepareButtons();
6440 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006441 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442
6443 NotifyMotionArgs motionArgs;
6444
6445 // default tool type is finger
6446 processDown(mapper, 100, 200);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6451
6452 // eraser
6453 processKey(mapper, BTN_TOOL_RUBBER, 1);
6454 processSync(mapper);
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6458
6459 // stylus
6460 processKey(mapper, BTN_TOOL_RUBBER, 0);
6461 processKey(mapper, BTN_TOOL_PEN, 1);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6464 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6466
6467 // brush
6468 processKey(mapper, BTN_TOOL_PEN, 0);
6469 processKey(mapper, BTN_TOOL_BRUSH, 1);
6470 processSync(mapper);
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6474
6475 // pencil
6476 processKey(mapper, BTN_TOOL_BRUSH, 0);
6477 processKey(mapper, BTN_TOOL_PENCIL, 1);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6482
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006483 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006484 processKey(mapper, BTN_TOOL_PENCIL, 0);
6485 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6490
6491 // mouse
6492 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6493 processKey(mapper, BTN_TOOL_MOUSE, 1);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6498
6499 // lens
6500 processKey(mapper, BTN_TOOL_MOUSE, 0);
6501 processKey(mapper, BTN_TOOL_LENS, 1);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6505 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6506
6507 // double-tap
6508 processKey(mapper, BTN_TOOL_LENS, 0);
6509 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6514
6515 // triple-tap
6516 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6517 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6518 processSync(mapper);
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6522
6523 // quad-tap
6524 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6525 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6530
6531 // finger
6532 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6533 processKey(mapper, BTN_TOOL_FINGER, 1);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6538
6539 // stylus trumps finger
6540 processKey(mapper, BTN_TOOL_PEN, 1);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6545
6546 // eraser trumps stylus
6547 processKey(mapper, BTN_TOOL_RUBBER, 1);
6548 processSync(mapper);
6549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6552
6553 // mouse trumps eraser
6554 processKey(mapper, BTN_TOOL_MOUSE, 1);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6559
6560 // back to default tool type
6561 processKey(mapper, BTN_TOOL_MOUSE, 0);
6562 processKey(mapper, BTN_TOOL_RUBBER, 0);
6563 processKey(mapper, BTN_TOOL_PEN, 0);
6564 processKey(mapper, BTN_TOOL_FINGER, 0);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6569}
6570
6571TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006572 addConfigurationProperty("touch.deviceType", "touchScreen");
6573 prepareDisplay(DISPLAY_ORIENTATION_0);
6574 prepareButtons();
6575 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006576 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006577 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578
6579 NotifyMotionArgs motionArgs;
6580
6581 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6582 processKey(mapper, BTN_TOOL_FINGER, 1);
6583 processMove(mapper, 100, 200);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6593 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6594
6595 // move a little
6596 processMove(mapper, 150, 250);
6597 processSync(mapper);
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6602
6603 // down when BTN_TOUCH is pressed, pressure defaults to 1
6604 processKey(mapper, BTN_TOUCH, 1);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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_DOWN, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6615
6616 // up when BTN_TOUCH is released, hover restored
6617 processKey(mapper, BTN_TOUCH, 0);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6620 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6622 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6623
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6627 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6628
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6633
6634 // exit hover when pointer goes away
6635 processKey(mapper, BTN_TOOL_FINGER, 0);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6641}
6642
6643TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006644 addConfigurationProperty("touch.deviceType", "touchScreen");
6645 prepareDisplay(DISPLAY_ORIENTATION_0);
6646 prepareButtons();
6647 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006648 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006649
6650 NotifyMotionArgs motionArgs;
6651
6652 // initially hovering because pressure is 0
6653 processDown(mapper, 100, 200);
6654 processPressure(mapper, 0);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6659 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6660
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6664 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6665
6666 // move a little
6667 processMove(mapper, 150, 250);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6672 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6673
6674 // down when pressure is non-zero
6675 processPressure(mapper, RAW_PRESSURE_MAX);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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_DOWN, motionArgs.action);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6686
6687 // up when pressure becomes 0, hover restored
6688 processPressure(mapper, 0);
6689 processSync(mapper);
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6691 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6693 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6694
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6698 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6699
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6703 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6704
6705 // exit hover when pointer goes away
6706 processUp(mapper);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6711 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6712}
6713
lilinnan687e58f2022-07-19 16:00:50 +08006714TEST_F(SingleTouchInputMapperTest,
6715 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6716 addConfigurationProperty("touch.deviceType", "touchScreen");
6717 prepareDisplay(DISPLAY_ORIENTATION_0);
6718 prepareButtons();
6719 prepareAxes(POSITION);
6720 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6721 NotifyMotionArgs motionArgs;
6722
6723 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006724 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006725 processSync(mapper);
6726
6727 // We should receive a down event
6728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6729 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6730
6731 // Change display id
6732 clearViewports();
6733 prepareSecondaryDisplay(ViewportType::INTERNAL);
6734
6735 // We should receive a cancel event
6736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6737 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6738 // Then receive reset called
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6740}
6741
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006742TEST_F(SingleTouchInputMapperTest,
6743 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6744 addConfigurationProperty("touch.deviceType", "touchScreen");
6745 prepareDisplay(DISPLAY_ORIENTATION_0);
6746 prepareButtons();
6747 prepareAxes(POSITION);
6748 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6750 NotifyMotionArgs motionArgs;
6751
6752 // Start a new gesture.
6753 processDown(mapper, 100, 200);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6757
6758 // Make the viewport inactive. This will put the device in disabled mode.
6759 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6760 viewport->isActive = false;
6761 mFakePolicy->updateViewport(*viewport);
6762 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6763
6764 // We should receive a cancel event for the ongoing gesture.
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6767 // Then we should be notified that the device was reset.
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6769
6770 // No events are generated while the viewport is inactive.
6771 processMove(mapper, 101, 201);
6772 processSync(mapper);
6773 processDown(mapper, 102, 202);
6774 processSync(mapper);
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6776
6777 // Make the viewport active again. The device should resume processing events.
6778 viewport->isActive = true;
6779 mFakePolicy->updateViewport(*viewport);
6780 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6781
6782 // The device is reset because it changes back to direct mode, without generating any events.
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6785
6786 // Start a new gesture.
6787 processDown(mapper, 100, 200);
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6791
6792 // No more events.
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6795}
6796
Prabir Pradhan5632d622021-09-06 07:57:20 -07006797// --- TouchDisplayProjectionTest ---
6798
6799class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6800public:
6801 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6802 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6803 // rotated equivalent of the given un-rotated physical display bounds.
6804 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6805 uint32_t inverseRotationFlags;
6806 auto width = DISPLAY_WIDTH;
6807 auto height = DISPLAY_HEIGHT;
6808 switch (orientation) {
6809 case DISPLAY_ORIENTATION_90:
6810 inverseRotationFlags = ui::Transform::ROT_270;
6811 std::swap(width, height);
6812 break;
6813 case DISPLAY_ORIENTATION_180:
6814 inverseRotationFlags = ui::Transform::ROT_180;
6815 break;
6816 case DISPLAY_ORIENTATION_270:
6817 inverseRotationFlags = ui::Transform::ROT_90;
6818 std::swap(width, height);
6819 break;
6820 case DISPLAY_ORIENTATION_0:
6821 inverseRotationFlags = ui::Transform::ROT_0;
6822 break;
6823 default:
6824 FAIL() << "Invalid orientation: " << orientation;
6825 }
6826
6827 const ui::Transform rotation(inverseRotationFlags, width, height);
6828 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6829
6830 std::optional<DisplayViewport> internalViewport =
6831 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6832 DisplayViewport& v = *internalViewport;
6833 v.displayId = DISPLAY_ID;
6834 v.orientation = orientation;
6835
6836 v.logicalLeft = 0;
6837 v.logicalTop = 0;
6838 v.logicalRight = 100;
6839 v.logicalBottom = 100;
6840
6841 v.physicalLeft = rotatedPhysicalDisplay.left;
6842 v.physicalTop = rotatedPhysicalDisplay.top;
6843 v.physicalRight = rotatedPhysicalDisplay.right;
6844 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6845
6846 v.deviceWidth = width;
6847 v.deviceHeight = height;
6848
6849 v.isActive = true;
6850 v.uniqueId = UNIQUE_ID;
6851 v.type = ViewportType::INTERNAL;
6852 mFakePolicy->updateViewport(v);
6853 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6854 }
6855
6856 void assertReceivedMove(const Point& point) {
6857 NotifyMotionArgs motionArgs;
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6860 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6862 1, 0, 0, 0, 0, 0, 0, 0));
6863 }
6864};
6865
6866TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6867 addConfigurationProperty("touch.deviceType", "touchScreen");
6868 prepareDisplay(DISPLAY_ORIENTATION_0);
6869
6870 prepareButtons();
6871 prepareAxes(POSITION);
6872 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6873
6874 NotifyMotionArgs motionArgs;
6875
6876 // Configure the DisplayViewport such that the logical display maps to a subsection of
6877 // the display panel called the physical display. Here, the physical display is bounded by the
6878 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6879 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6880 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6881 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6882
6883 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6884 DISPLAY_ORIENTATION_270}) {
6885 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6886
6887 // Touches outside the physical display should be ignored, and should not generate any
6888 // events. Ensure touches at the following points that lie outside of the physical display
6889 // area do not generate any events.
6890 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6891 processDown(mapper, toRawX(point.x), toRawY(point.y));
6892 processSync(mapper);
6893 processUp(mapper);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6896 << "Unexpected event generated for touch outside physical display at point: "
6897 << point.x << ", " << point.y;
6898 }
6899 }
6900}
6901
6902TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6903 addConfigurationProperty("touch.deviceType", "touchScreen");
6904 prepareDisplay(DISPLAY_ORIENTATION_0);
6905
6906 prepareButtons();
6907 prepareAxes(POSITION);
6908 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6909
6910 NotifyMotionArgs motionArgs;
6911
6912 // Configure the DisplayViewport such that the logical display maps to a subsection of
6913 // the display panel called the physical display. Here, the physical display is bounded by the
6914 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6915 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6916
6917 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6918 DISPLAY_ORIENTATION_270}) {
6919 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6920
6921 // Touches that start outside the physical display should be ignored until it enters the
6922 // physical display bounds, at which point it should generate a down event. Start a touch at
6923 // the point (5, 100), which is outside the physical display bounds.
6924 static const Point kOutsidePoint{5, 100};
6925 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6926 processSync(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6928
6929 // Move the touch into the physical display area. This should generate a pointer down.
6930 processMove(mapper, toRawX(11), toRawY(21));
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6934 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6935 ASSERT_NO_FATAL_FAILURE(
6936 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6937
6938 // Move the touch inside the physical display area. This should generate a pointer move.
6939 processMove(mapper, toRawX(69), toRawY(159));
6940 processSync(mapper);
6941 assertReceivedMove({69, 159});
6942
6943 // Move outside the physical display area. Since the pointer is already down, this should
6944 // now continue generating events.
6945 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6946 processSync(mapper);
6947 assertReceivedMove(kOutsidePoint);
6948
6949 // Release. This should generate a pointer up.
6950 processUp(mapper);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6955 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6956
6957 // Ensure no more events were generated.
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6960 }
6961}
6962
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963// --- MultiTouchInputMapperTest ---
6964
6965class MultiTouchInputMapperTest : public TouchInputMapperTest {
6966protected:
6967 void prepareAxes(int axes);
6968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6970 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6971 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6972 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6973 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6974 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6975 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6976 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6977 void processId(MultiTouchInputMapper& mapper, int32_t id);
6978 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6979 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6980 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6981 void processMTSync(MultiTouchInputMapper& mapper);
6982 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983};
6984
6985void MultiTouchInputMapperTest::prepareAxes(int axes) {
6986 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989 }
6990 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6992 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6995 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996 }
6997 }
6998 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006999 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7000 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007003 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004 }
7005 }
7006 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007007 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7008 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009 }
7010 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007011 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7012 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013 }
7014 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007015 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7016 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017 }
7018 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7020 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 }
7022 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007023 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7024 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025 }
7026 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007027 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028 }
7029}
7030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007031void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7032 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035}
7036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007037void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7038 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040}
7041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7043 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045}
7046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007047void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049}
7050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007051void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053}
7054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007055void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7056 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058}
7059
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007060void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062}
7063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007064void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066}
7067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007068void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070}
7071
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007072void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074}
7075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007076void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078}
7079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007080void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7081 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083}
7084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007085void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087}
7088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007089void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091}
7092
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 addConfigurationProperty("touch.deviceType", "touchScreen");
7095 prepareDisplay(DISPLAY_ORIENTATION_0);
7096 prepareAxes(POSITION);
7097 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099
arthurhungdcef2dc2020-08-11 14:47:50 +08007100 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101
7102 NotifyMotionArgs motionArgs;
7103
7104 // Two fingers down at once.
7105 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7106 processPosition(mapper, x1, y1);
7107 processMTSync(mapper);
7108 processPosition(mapper, x2, y2);
7109 processMTSync(mapper);
7110 processSync(mapper);
7111
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7114 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7115 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7116 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7117 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7118 ASSERT_EQ(0, motionArgs.flags);
7119 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7120 ASSERT_EQ(0, motionArgs.buttonState);
7121 ASSERT_EQ(0, motionArgs.edgeFlags);
7122 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7123 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7126 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7127 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7128 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7130
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007136 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 ASSERT_EQ(0, motionArgs.flags);
7138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7139 ASSERT_EQ(0, motionArgs.buttonState);
7140 ASSERT_EQ(0, motionArgs.edgeFlags);
7141 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7144 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7147 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7153
7154 // Move.
7155 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7156 processPosition(mapper, x1, y1);
7157 processMTSync(mapper);
7158 processPosition(mapper, x2, y2);
7159 processMTSync(mapper);
7160 processSync(mapper);
7161
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7165 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7166 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7168 ASSERT_EQ(0, motionArgs.flags);
7169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7170 ASSERT_EQ(0, motionArgs.buttonState);
7171 ASSERT_EQ(0, motionArgs.edgeFlags);
7172 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7175 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7178 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
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 // First finger up.
7186 x2 += 15; y2 -= 20;
7187 processPosition(mapper, x2, y2);
7188 processMTSync(mapper);
7189 processSync(mapper);
7190
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007196 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_EQ(0, motionArgs.flags);
7198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7199 ASSERT_EQ(0, motionArgs.buttonState);
7200 ASSERT_EQ(0, motionArgs.edgeFlags);
7201 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7204 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7207 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7213
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7215 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7216 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7218 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7220 ASSERT_EQ(0, motionArgs.flags);
7221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7222 ASSERT_EQ(0, motionArgs.buttonState);
7223 ASSERT_EQ(0, motionArgs.edgeFlags);
7224 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7225 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7228 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7229 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7230 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7232
7233 // Move.
7234 x2 += 20; y2 -= 25;
7235 processPosition(mapper, x2, y2);
7236 processMTSync(mapper);
7237 processSync(mapper);
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7245 ASSERT_EQ(0, motionArgs.flags);
7246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7247 ASSERT_EQ(0, motionArgs.buttonState);
7248 ASSERT_EQ(0, motionArgs.edgeFlags);
7249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7250 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7253 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7257
7258 // New finger down.
7259 int32_t x3 = 700, y3 = 300;
7260 processPosition(mapper, x2, y2);
7261 processMTSync(mapper);
7262 processPosition(mapper, x3, y3);
7263 processMTSync(mapper);
7264 processSync(mapper);
7265
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7268 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7270 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007271 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(0, motionArgs.flags);
7273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7274 ASSERT_EQ(0, motionArgs.buttonState);
7275 ASSERT_EQ(0, motionArgs.edgeFlags);
7276 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7279 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7284 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7288
7289 // Second finger up.
7290 x3 += 30; y3 -= 20;
7291 processPosition(mapper, x3, y3);
7292 processMTSync(mapper);
7293 processSync(mapper);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007300 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301 ASSERT_EQ(0, motionArgs.flags);
7302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7303 ASSERT_EQ(0, motionArgs.buttonState);
7304 ASSERT_EQ(0, motionArgs.edgeFlags);
7305 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7308 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7311 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7317
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7321 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7322 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7324 ASSERT_EQ(0, motionArgs.flags);
7325 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7326 ASSERT_EQ(0, motionArgs.buttonState);
7327 ASSERT_EQ(0, motionArgs.edgeFlags);
7328 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7329 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7330 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7332 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7333 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7334 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7336
7337 // Last finger up.
7338 processMTSync(mapper);
7339 processSync(mapper);
7340
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7343 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7344 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7345 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7347 ASSERT_EQ(0, motionArgs.flags);
7348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7349 ASSERT_EQ(0, motionArgs.buttonState);
7350 ASSERT_EQ(0, motionArgs.edgeFlags);
7351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7355 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7359
7360 // Should not have sent any more keys or motions.
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7363}
7364
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007365TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7366 addConfigurationProperty("touch.deviceType", "touchScreen");
7367 prepareDisplay(DISPLAY_ORIENTATION_0);
7368
7369 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7370 /*fuzz*/ 0, /*resolution*/ 10);
7371 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7372 /*fuzz*/ 0, /*resolution*/ 11);
7373 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7374 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7375 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7376 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7377 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7378 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7379 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7380 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7381
7382 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7383
7384 // X and Y axes
7385 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7386 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7387 // Touch major and minor
7388 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7389 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7390 // Tool major and minor
7391 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7392 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7393}
7394
7395TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7396 addConfigurationProperty("touch.deviceType", "touchScreen");
7397 prepareDisplay(DISPLAY_ORIENTATION_0);
7398
7399 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7400 /*fuzz*/ 0, /*resolution*/ 10);
7401 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7402 /*fuzz*/ 0, /*resolution*/ 11);
7403
7404 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7405
7406 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7407
7408 // Touch major and minor
7409 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7410 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7411 // Tool major and minor
7412 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7413 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7414}
7415
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417 addConfigurationProperty("touch.deviceType", "touchScreen");
7418 prepareDisplay(DISPLAY_ORIENTATION_0);
7419 prepareAxes(POSITION | ID);
7420 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007422
arthurhungdcef2dc2020-08-11 14:47:50 +08007423 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007424
7425 NotifyMotionArgs motionArgs;
7426
7427 // Two fingers down at once.
7428 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7429 processPosition(mapper, x1, y1);
7430 processId(mapper, 1);
7431 processMTSync(mapper);
7432 processPosition(mapper, x2, y2);
7433 processId(mapper, 2);
7434 processMTSync(mapper);
7435 processSync(mapper);
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7443 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7444
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007446 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7450 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7453 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7455 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7456
7457 // Move.
7458 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7459 processPosition(mapper, x1, y1);
7460 processId(mapper, 1);
7461 processMTSync(mapper);
7462 processPosition(mapper, x2, y2);
7463 processId(mapper, 2);
7464 processMTSync(mapper);
7465 processSync(mapper);
7466
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7475 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7478
7479 // First finger up.
7480 x2 += 15; y2 -= 20;
7481 processPosition(mapper, x2, y2);
7482 processId(mapper, 2);
7483 processMTSync(mapper);
7484 processSync(mapper);
7485
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007487 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7491 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7494 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7496 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7497
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7500 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7501 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7504 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7505
7506 // Move.
7507 x2 += 20; y2 -= 25;
7508 processPosition(mapper, x2, y2);
7509 processId(mapper, 2);
7510 processMTSync(mapper);
7511 processSync(mapper);
7512
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7516 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7520
7521 // New finger down.
7522 int32_t x3 = 700, y3 = 300;
7523 processPosition(mapper, x2, y2);
7524 processId(mapper, 2);
7525 processMTSync(mapper);
7526 processPosition(mapper, x3, y3);
7527 processId(mapper, 3);
7528 processMTSync(mapper);
7529 processSync(mapper);
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007532 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7539 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7541 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7542
7543 // Second finger up.
7544 x3 += 30; y3 -= 20;
7545 processPosition(mapper, x3, y3);
7546 processId(mapper, 3);
7547 processMTSync(mapper);
7548 processSync(mapper);
7549
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007551 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7555 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7560 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7561
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7564 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7568 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7569
7570 // Last finger up.
7571 processMTSync(mapper);
7572 processSync(mapper);
7573
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7576 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7577 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7580 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7581
7582 // Should not have sent any more keys or motions.
7583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7585}
7586
7587TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588 addConfigurationProperty("touch.deviceType", "touchScreen");
7589 prepareDisplay(DISPLAY_ORIENTATION_0);
7590 prepareAxes(POSITION | ID | SLOT);
7591 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007593
arthurhungdcef2dc2020-08-11 14:47:50 +08007594 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595
7596 NotifyMotionArgs motionArgs;
7597
7598 // Two fingers down at once.
7599 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7600 processPosition(mapper, x1, y1);
7601 processId(mapper, 1);
7602 processSlot(mapper, 1);
7603 processPosition(mapper, x2, y2);
7604 processId(mapper, 2);
7605 processSync(mapper);
7606
7607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7609 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7613 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007616 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7620 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7621 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7623 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7625 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7626
7627 // Move.
7628 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7629 processSlot(mapper, 0);
7630 processPosition(mapper, x1, y1);
7631 processSlot(mapper, 1);
7632 processPosition(mapper, x2, y2);
7633 processSync(mapper);
7634
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7637 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7638 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7640 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7643 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7645 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7646
7647 // First finger up.
7648 x2 += 15; y2 -= 20;
7649 processSlot(mapper, 0);
7650 processId(mapper, -1);
7651 processSlot(mapper, 1);
7652 processPosition(mapper, x2, y2);
7653 processSync(mapper);
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007656 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7663 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7665 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7666
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7669 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7670 ASSERT_EQ(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7674
7675 // Move.
7676 x2 += 20; y2 -= 25;
7677 processPosition(mapper, x2, y2);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7683 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7687
7688 // New finger down.
7689 int32_t x3 = 700, y3 = 300;
7690 processPosition(mapper, x2, y2);
7691 processSlot(mapper, 0);
7692 processId(mapper, 3);
7693 processPosition(mapper, x3, y3);
7694 processSync(mapper);
7695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007697 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7701 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7704 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // Second finger up.
7709 x3 += 30; y3 -= 20;
7710 processSlot(mapper, 1);
7711 processId(mapper, -1);
7712 processSlot(mapper, 0);
7713 processPosition(mapper, x3, y3);
7714 processSync(mapper);
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007717 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7721 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7724 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7726 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7727
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7730 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7734 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7735
7736 // Last finger up.
7737 processId(mapper, -1);
7738 processSync(mapper);
7739
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7741 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7742 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7746 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7747
7748 // Should not have sent any more keys or motions.
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7751}
7752
7753TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 addConfigurationProperty("touch.deviceType", "touchScreen");
7755 prepareDisplay(DISPLAY_ORIENTATION_0);
7756 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758
7759 // These calculations are based on the input device calibration documentation.
7760 int32_t rawX = 100;
7761 int32_t rawY = 200;
7762 int32_t rawTouchMajor = 7;
7763 int32_t rawTouchMinor = 6;
7764 int32_t rawToolMajor = 9;
7765 int32_t rawToolMinor = 8;
7766 int32_t rawPressure = 11;
7767 int32_t rawDistance = 0;
7768 int32_t rawOrientation = 3;
7769 int32_t id = 5;
7770
7771 float x = toDisplayX(rawX);
7772 float y = toDisplayY(rawY);
7773 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7774 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7775 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7776 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7777 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7778 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7779 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7780 float distance = float(rawDistance);
7781
7782 processPosition(mapper, rawX, rawY);
7783 processTouchMajor(mapper, rawTouchMajor);
7784 processTouchMinor(mapper, rawTouchMinor);
7785 processToolMajor(mapper, rawToolMajor);
7786 processToolMinor(mapper, rawToolMinor);
7787 processPressure(mapper, rawPressure);
7788 processOrientation(mapper, rawOrientation);
7789 processDistance(mapper, rawDistance);
7790 processId(mapper, id);
7791 processMTSync(mapper);
7792 processSync(mapper);
7793
7794 NotifyMotionArgs args;
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7796 ASSERT_EQ(0, args.pointerProperties[0].id);
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7798 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7799 orientation, distance));
7800}
7801
7802TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 addConfigurationProperty("touch.deviceType", "touchScreen");
7804 prepareDisplay(DISPLAY_ORIENTATION_0);
7805 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7806 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007807 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808
7809 // These calculations are based on the input device calibration documentation.
7810 int32_t rawX = 100;
7811 int32_t rawY = 200;
7812 int32_t rawTouchMajor = 140;
7813 int32_t rawTouchMinor = 120;
7814 int32_t rawToolMajor = 180;
7815 int32_t rawToolMinor = 160;
7816
7817 float x = toDisplayX(rawX);
7818 float y = toDisplayY(rawY);
7819 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7820 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7821 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7822 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7823 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7824
7825 processPosition(mapper, rawX, rawY);
7826 processTouchMajor(mapper, rawTouchMajor);
7827 processTouchMinor(mapper, rawTouchMinor);
7828 processToolMajor(mapper, rawToolMajor);
7829 processToolMinor(mapper, rawToolMinor);
7830 processMTSync(mapper);
7831 processSync(mapper);
7832
7833 NotifyMotionArgs args;
7834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7836 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7837}
7838
7839TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 addConfigurationProperty("touch.deviceType", "touchScreen");
7841 prepareDisplay(DISPLAY_ORIENTATION_0);
7842 prepareAxes(POSITION | TOUCH | TOOL);
7843 addConfigurationProperty("touch.size.calibration", "diameter");
7844 addConfigurationProperty("touch.size.scale", "10");
7845 addConfigurationProperty("touch.size.bias", "160");
7846 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848
7849 // These calculations are based on the input device calibration documentation.
7850 // Note: We only provide a single common touch/tool value because the device is assumed
7851 // not to emit separate values for each pointer (isSummed = 1).
7852 int32_t rawX = 100;
7853 int32_t rawY = 200;
7854 int32_t rawX2 = 150;
7855 int32_t rawY2 = 250;
7856 int32_t rawTouchMajor = 5;
7857 int32_t rawToolMajor = 8;
7858
7859 float x = toDisplayX(rawX);
7860 float y = toDisplayY(rawY);
7861 float x2 = toDisplayX(rawX2);
7862 float y2 = toDisplayY(rawY2);
7863 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7864 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7865 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7866
7867 processPosition(mapper, rawX, rawY);
7868 processTouchMajor(mapper, rawTouchMajor);
7869 processToolMajor(mapper, rawToolMajor);
7870 processMTSync(mapper);
7871 processPosition(mapper, rawX2, rawY2);
7872 processTouchMajor(mapper, rawTouchMajor);
7873 processToolMajor(mapper, rawToolMajor);
7874 processMTSync(mapper);
7875 processSync(mapper);
7876
7877 NotifyMotionArgs args;
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007882 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 ASSERT_EQ(size_t(2), args.pointerCount);
7884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7885 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7887 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7888}
7889
7890TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 addConfigurationProperty("touch.deviceType", "touchScreen");
7892 prepareDisplay(DISPLAY_ORIENTATION_0);
7893 prepareAxes(POSITION | TOUCH | TOOL);
7894 addConfigurationProperty("touch.size.calibration", "area");
7895 addConfigurationProperty("touch.size.scale", "43");
7896 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898
7899 // These calculations are based on the input device calibration documentation.
7900 int32_t rawX = 100;
7901 int32_t rawY = 200;
7902 int32_t rawTouchMajor = 5;
7903 int32_t rawToolMajor = 8;
7904
7905 float x = toDisplayX(rawX);
7906 float y = toDisplayY(rawY);
7907 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7908 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7909 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7910
7911 processPosition(mapper, rawX, rawY);
7912 processTouchMajor(mapper, rawTouchMajor);
7913 processToolMajor(mapper, rawToolMajor);
7914 processMTSync(mapper);
7915 processSync(mapper);
7916
7917 NotifyMotionArgs args;
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7920 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7921}
7922
7923TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 addConfigurationProperty("touch.deviceType", "touchScreen");
7925 prepareDisplay(DISPLAY_ORIENTATION_0);
7926 prepareAxes(POSITION | PRESSURE);
7927 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7928 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007929 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007930
Michael Wrightaa449c92017-12-13 21:21:43 +00007931 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007932 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007933 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7934 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7935 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7936
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 // These calculations are based on the input device calibration documentation.
7938 int32_t rawX = 100;
7939 int32_t rawY = 200;
7940 int32_t rawPressure = 60;
7941
7942 float x = toDisplayX(rawX);
7943 float y = toDisplayY(rawY);
7944 float pressure = float(rawPressure) * 0.01f;
7945
7946 processPosition(mapper, rawX, rawY);
7947 processPressure(mapper, rawPressure);
7948 processMTSync(mapper);
7949 processSync(mapper);
7950
7951 NotifyMotionArgs args;
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7954 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7955}
7956
7957TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 addConfigurationProperty("touch.deviceType", "touchScreen");
7959 prepareDisplay(DISPLAY_ORIENTATION_0);
7960 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007961 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962
7963 NotifyMotionArgs motionArgs;
7964 NotifyKeyArgs keyArgs;
7965
7966 processId(mapper, 1);
7967 processPosition(mapper, 100, 200);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7971 ASSERT_EQ(0, motionArgs.buttonState);
7972
7973 // press BTN_LEFT, release BTN_LEFT
7974 processKey(mapper, BTN_LEFT, 1);
7975 processSync(mapper);
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7978 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7979
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7982 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7983
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 processKey(mapper, BTN_LEFT, 0);
7985 processSync(mapper);
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007989
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007992 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993
7994 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7995 processKey(mapper, BTN_RIGHT, 1);
7996 processKey(mapper, BTN_MIDDLE, 1);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8000 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8001 motionArgs.buttonState);
8002
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8005 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8006
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8009 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8010 motionArgs.buttonState);
8011
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 processKey(mapper, BTN_RIGHT, 0);
8013 processSync(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008017
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008020 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021
8022 processKey(mapper, BTN_MIDDLE, 0);
8023 processSync(mapper);
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008025 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008030 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031
8032 // press BTN_BACK, release BTN_BACK
8033 processKey(mapper, BTN_BACK, 1);
8034 processSync(mapper);
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8036 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8037 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008038
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8042
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8045 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046
8047 processKey(mapper, BTN_BACK, 0);
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008052
8053 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(0, motionArgs.buttonState);
8056
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8058 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8059 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8060
8061 // press BTN_SIDE, release BTN_SIDE
8062 processKey(mapper, BTN_SIDE, 1);
8063 processSync(mapper);
8064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8065 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8066 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8074 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075
8076 processKey(mapper, BTN_SIDE, 0);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
8085
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8088 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8089
8090 // press BTN_FORWARD, release BTN_FORWARD
8091 processKey(mapper, BTN_FORWARD, 1);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8094 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8095 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8100
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8103 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104
8105 processKey(mapper, BTN_FORWARD, 0);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113 ASSERT_EQ(0, motionArgs.buttonState);
8114
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8116 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8117 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8118
8119 // press BTN_EXTRA, release BTN_EXTRA
8120 processKey(mapper, BTN_EXTRA, 1);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8123 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8124 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008125
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008128 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8129
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8132 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133
8134 processKey(mapper, BTN_EXTRA, 0);
8135 processSync(mapper);
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008142 ASSERT_EQ(0, motionArgs.buttonState);
8143
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8146 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8147
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8149
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 // press BTN_STYLUS, release BTN_STYLUS
8151 processKey(mapper, BTN_STYLUS, 1);
8152 processSync(mapper);
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8156
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160
8161 processKey(mapper, BTN_STYLUS, 0);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170
8171 // press BTN_STYLUS2, release BTN_STYLUS2
8172 processKey(mapper, BTN_STYLUS2, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8177
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181
8182 processKey(mapper, BTN_STYLUS2, 0);
8183 processSync(mapper);
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191
8192 // release touch
8193 processId(mapper, -1);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8197 ASSERT_EQ(0, motionArgs.buttonState);
8198}
8199
8200TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 addConfigurationProperty("touch.deviceType", "touchScreen");
8202 prepareDisplay(DISPLAY_ORIENTATION_0);
8203 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205
8206 NotifyMotionArgs motionArgs;
8207
8208 // default tool type is finger
8209 processId(mapper, 1);
8210 processPosition(mapper, 100, 200);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8215
8216 // eraser
8217 processKey(mapper, BTN_TOOL_RUBBER, 1);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8222
8223 // stylus
8224 processKey(mapper, BTN_TOOL_RUBBER, 0);
8225 processKey(mapper, BTN_TOOL_PEN, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8230
8231 // brush
8232 processKey(mapper, BTN_TOOL_PEN, 0);
8233 processKey(mapper, BTN_TOOL_BRUSH, 1);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8238
8239 // pencil
8240 processKey(mapper, BTN_TOOL_BRUSH, 0);
8241 processKey(mapper, BTN_TOOL_PENCIL, 1);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8246
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008247 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 processKey(mapper, BTN_TOOL_PENCIL, 0);
8249 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8254
8255 // mouse
8256 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8257 processKey(mapper, BTN_TOOL_MOUSE, 1);
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8262
8263 // lens
8264 processKey(mapper, BTN_TOOL_MOUSE, 0);
8265 processKey(mapper, BTN_TOOL_LENS, 1);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8270
8271 // double-tap
8272 processKey(mapper, BTN_TOOL_LENS, 0);
8273 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8278
8279 // triple-tap
8280 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8281 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8286
8287 // quad-tap
8288 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8289 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8294
8295 // finger
8296 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8297 processKey(mapper, BTN_TOOL_FINGER, 1);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8302
8303 // stylus trumps finger
8304 processKey(mapper, BTN_TOOL_PEN, 1);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8309
8310 // eraser trumps stylus
8311 processKey(mapper, BTN_TOOL_RUBBER, 1);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8316
8317 // mouse trumps eraser
8318 processKey(mapper, BTN_TOOL_MOUSE, 1);
8319 processSync(mapper);
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8323
8324 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8325 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8330
8331 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8332 processToolType(mapper, MT_TOOL_PEN);
8333 processSync(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8336 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8337
8338 // back to default tool type
8339 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8340 processKey(mapper, BTN_TOOL_MOUSE, 0);
8341 processKey(mapper, BTN_TOOL_RUBBER, 0);
8342 processKey(mapper, BTN_TOOL_PEN, 0);
8343 processKey(mapper, BTN_TOOL_FINGER, 0);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8348}
8349
8350TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351 addConfigurationProperty("touch.deviceType", "touchScreen");
8352 prepareDisplay(DISPLAY_ORIENTATION_0);
8353 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008354 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008355 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356
8357 NotifyMotionArgs motionArgs;
8358
8359 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8360 processId(mapper, 1);
8361 processPosition(mapper, 100, 200);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8366 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8367
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8371 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8372
8373 // move a little
8374 processPosition(mapper, 150, 250);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8380
8381 // down when BTN_TOUCH is pressed, pressure defaults to 1
8382 processKey(mapper, BTN_TOUCH, 1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8387 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8388
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8392 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8393
8394 // up when BTN_TOUCH is released, hover restored
8395 processKey(mapper, BTN_TOUCH, 0);
8396 processSync(mapper);
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8400 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8401
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8405 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8406
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8410 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8411
8412 // exit hover when pointer goes away
8413 processId(mapper, -1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8418 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8419}
8420
8421TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 addConfigurationProperty("touch.deviceType", "touchScreen");
8423 prepareDisplay(DISPLAY_ORIENTATION_0);
8424 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426
8427 NotifyMotionArgs motionArgs;
8428
8429 // initially hovering because pressure is 0
8430 processId(mapper, 1);
8431 processPosition(mapper, 100, 200);
8432 processPressure(mapper, 0);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8438
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8442 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8443
8444 // move a little
8445 processPosition(mapper, 150, 250);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8450 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8451
8452 // down when pressure becomes non-zero
8453 processPressure(mapper, RAW_PRESSURE_MAX);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8458 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8459
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8461 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8463 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8464
8465 // up when pressure becomes 0, hover restored
8466 processPressure(mapper, 0);
8467 processSync(mapper);
8468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8471 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8472
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8476 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8477
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8481 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8482
8483 // exit hover when pointer goes away
8484 processId(mapper, -1);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8489 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8490}
8491
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008492/**
8493 * Set the input device port <--> display port associations, and check that the
8494 * events are routed to the display that matches the display port.
8495 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8496 */
8497TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008498 const std::string usb2 = "USB2";
8499 const uint8_t hdmi1 = 0;
8500 const uint8_t hdmi2 = 1;
8501 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008502 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008503
8504 addConfigurationProperty("touch.deviceType", "touchScreen");
8505 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008507
8508 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8509 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8510
8511 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8512 // for this input device is specified, and the matching viewport is not present,
8513 // the input device should be disabled (at the mapper level).
8514
8515 // Add viewport for display 2 on hdmi2
8516 prepareSecondaryDisplay(type, hdmi2);
8517 // Send a touch event
8518 processPosition(mapper, 100, 100);
8519 processSync(mapper);
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8521
8522 // Add viewport for display 1 on hdmi1
8523 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8524 // Send a touch event again
8525 processPosition(mapper, 100, 100);
8526 processSync(mapper);
8527
8528 NotifyMotionArgs args;
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8530 ASSERT_EQ(DISPLAY_ID, args.displayId);
8531}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008533TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8534 addConfigurationProperty("touch.deviceType", "touchScreen");
8535 prepareAxes(POSITION);
8536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8537
8538 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8539
8540 prepareDisplay(DISPLAY_ORIENTATION_0);
8541 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8542
8543 // Send a touch event
8544 processPosition(mapper, 100, 100);
8545 processSync(mapper);
8546
8547 NotifyMotionArgs args;
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8549 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8550}
8551
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008552TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008553 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008554 std::shared_ptr<FakePointerController> fakePointerController =
8555 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008556 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008557 fakePointerController->setPosition(100, 200);
8558 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008559 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008560
Garfield Tan888a6a42020-01-09 11:39:16 -08008561 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008562 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008563
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008564 prepareDisplay(DISPLAY_ORIENTATION_0);
8565 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008566 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008567
8568 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008569 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008570
8571 NotifyMotionArgs motionArgs;
8572 processPosition(mapper, 100, 100);
8573 processSync(mapper);
8574
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8577 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8578}
8579
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008580/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008581 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8582 */
8583TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8584 addConfigurationProperty("touch.deviceType", "touchScreen");
8585 prepareAxes(POSITION);
8586 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8587
8588 prepareDisplay(DISPLAY_ORIENTATION_0);
8589 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8590 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8591 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8592 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8593
8594 NotifyMotionArgs args;
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8596 ASSERT_EQ(26, args.readTime);
8597
8598 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8599 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8600 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8601
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8603 ASSERT_EQ(33, args.readTime);
8604}
8605
8606/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008607 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8608 * events should not be delivered to the listener.
8609 */
8610TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8611 addConfigurationProperty("touch.deviceType", "touchScreen");
8612 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8613 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8614 ViewportType::INTERNAL);
8615 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8616 prepareAxes(POSITION);
8617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8618
8619 NotifyMotionArgs motionArgs;
8620 processPosition(mapper, 100, 100);
8621 processSync(mapper);
8622
8623 mFakeListener->assertNotifyMotionWasNotCalled();
8624}
8625
Garfield Tanc734e4f2021-01-15 20:01:39 -08008626TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8627 addConfigurationProperty("touch.deviceType", "touchScreen");
8628 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8629 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8630 ViewportType::INTERNAL);
8631 std::optional<DisplayViewport> optionalDisplayViewport =
8632 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8633 ASSERT_TRUE(optionalDisplayViewport.has_value());
8634 DisplayViewport displayViewport = *optionalDisplayViewport;
8635
8636 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8637 prepareAxes(POSITION);
8638 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8639
8640 // Finger down
8641 int32_t x = 100, y = 100;
8642 processPosition(mapper, x, y);
8643 processSync(mapper);
8644
8645 NotifyMotionArgs motionArgs;
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8647 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8648
8649 // Deactivate display viewport
8650 displayViewport.isActive = false;
8651 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8652 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8653
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008654 // The ongoing touch should be canceled immediately
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8657
8658 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008659 x += 10, y += 10;
8660 processPosition(mapper, x, y);
8661 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008663
8664 // Reactivate display viewport
8665 displayViewport.isActive = true;
8666 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8667 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8668
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008669 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008670 x += 10, y += 10;
8671 processPosition(mapper, x, y);
8672 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008675}
8676
Arthur Hung7c645402019-01-25 17:45:42 +08008677TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8678 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008679 prepareAxes(POSITION | ID | SLOT);
8680 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008682
8683 // Create the second touch screen device, and enable multi fingers.
8684 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008685 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008686 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008687 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008688 std::shared_ptr<InputDevice> device2 =
8689 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008690 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008691
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008692 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8693 0 /*flat*/, 0 /*fuzz*/);
8694 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8695 0 /*flat*/, 0 /*fuzz*/);
8696 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8697 0 /*flat*/, 0 /*fuzz*/);
8698 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8699 0 /*flat*/, 0 /*fuzz*/);
8700 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8701 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8702 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008703
8704 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008705 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008706 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8707 device2->reset(ARBITRARY_TIME);
8708
8709 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008710 std::shared_ptr<FakePointerController> fakePointerController =
8711 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008712 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008713
8714 // Setup policy for associated displays and show touches.
8715 const uint8_t hdmi1 = 0;
8716 const uint8_t hdmi2 = 1;
8717 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8718 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8719 mFakePolicy->setShowTouches(true);
8720
8721 // Create displays.
8722 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008723 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008724
8725 // Default device will reconfigure above, need additional reconfiguration for another device.
8726 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008727 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8728 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008729
8730 // Two fingers down at default display.
8731 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8732 processPosition(mapper, x1, y1);
8733 processId(mapper, 1);
8734 processSlot(mapper, 1);
8735 processPosition(mapper, x2, y2);
8736 processId(mapper, 2);
8737 processSync(mapper);
8738
8739 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8740 fakePointerController->getSpots().find(DISPLAY_ID);
8741 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8742 ASSERT_EQ(size_t(2), iter->second.size());
8743
8744 // Two fingers down at second display.
8745 processPosition(mapper2, x1, y1);
8746 processId(mapper2, 1);
8747 processSlot(mapper2, 1);
8748 processPosition(mapper2, x2, y2);
8749 processId(mapper2, 2);
8750 processSync(mapper2);
8751
8752 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8753 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8754 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008755
8756 // Disable the show touches configuration and ensure the spots are cleared.
8757 mFakePolicy->setShowTouches(false);
8758 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8759 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8760
8761 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008762}
8763
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008764TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008765 prepareAxes(POSITION);
8766 addConfigurationProperty("touch.deviceType", "touchScreen");
8767 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008768 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008769
8770 NotifyMotionArgs motionArgs;
8771 // Unrotated video frame
8772 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8773 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008774 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008775 processPosition(mapper, 100, 200);
8776 processSync(mapper);
8777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8778 ASSERT_EQ(frames, motionArgs.videoFrames);
8779
8780 // Subsequent touch events should not have any videoframes
8781 // This is implemented separately in FakeEventHub,
8782 // but that should match the behaviour of TouchVideoDevice.
8783 processPosition(mapper, 200, 200);
8784 processSync(mapper);
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8786 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8787}
8788
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008789TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008790 prepareAxes(POSITION);
8791 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008793 // Unrotated video frame
8794 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8795 NotifyMotionArgs motionArgs;
8796
8797 // Test all 4 orientations
8798 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008799 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8800 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8801 clearViewports();
8802 prepareDisplay(orientation);
8803 std::vector<TouchVideoFrame> frames{frame};
8804 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8805 processPosition(mapper, 100, 200);
8806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 ASSERT_EQ(frames, motionArgs.videoFrames);
8809 }
8810}
8811
8812TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8813 prepareAxes(POSITION);
8814 addConfigurationProperty("touch.deviceType", "touchScreen");
8815 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8816 // orientation-aware are affected by display rotation.
8817 addConfigurationProperty("touch.orientationAware", "0");
8818 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8819 // Unrotated video frame
8820 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8821 NotifyMotionArgs motionArgs;
8822
8823 // Test all 4 orientations
8824 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008825 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8826 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8827 clearViewports();
8828 prepareDisplay(orientation);
8829 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008830 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008831 processPosition(mapper, 100, 200);
8832 processSync(mapper);
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008834 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8835 // compared to the display. This is so that when the window transform (which contains the
8836 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8837 // window's coordinate space.
8838 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008839 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008840
8841 // Release finger.
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008844 }
8845}
8846
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008847TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008848 prepareAxes(POSITION);
8849 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008850 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008851 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8852 // so mix these.
8853 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8854 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8855 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8856 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8857 NotifyMotionArgs motionArgs;
8858
8859 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008860 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008861 processPosition(mapper, 100, 200);
8862 processSync(mapper);
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008864 ASSERT_EQ(frames, motionArgs.videoFrames);
8865}
8866
8867TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8868 prepareAxes(POSITION);
8869 addConfigurationProperty("touch.deviceType", "touchScreen");
8870 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8871 // orientation-aware are affected by display rotation.
8872 addConfigurationProperty("touch.orientationAware", "0");
8873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8874 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8875 // so mix these.
8876 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8877 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8878 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8879 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8880 NotifyMotionArgs motionArgs;
8881
8882 prepareDisplay(DISPLAY_ORIENTATION_90);
8883 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8884 processPosition(mapper, 100, 200);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8888 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8889 // compared to the display. This is so that when the window transform (which contains the
8890 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8891 // window's coordinate space.
8892 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8893 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008894 ASSERT_EQ(frames, motionArgs.videoFrames);
8895}
8896
Arthur Hung9da14732019-09-02 16:16:58 +08008897/**
8898 * If we had defined port associations, but the viewport is not ready, the touch device would be
8899 * expected to be disabled, and it should be enabled after the viewport has found.
8900 */
8901TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008902 constexpr uint8_t hdmi2 = 1;
8903 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008904 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008905
8906 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8907
8908 addConfigurationProperty("touch.deviceType", "touchScreen");
8909 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008911
8912 ASSERT_EQ(mDevice->isEnabled(), false);
8913
8914 // Add display on hdmi2, the device should be enabled and can receive touch event.
8915 prepareSecondaryDisplay(type, hdmi2);
8916 ASSERT_EQ(mDevice->isEnabled(), true);
8917
8918 // Send a touch event.
8919 processPosition(mapper, 100, 100);
8920 processSync(mapper);
8921
8922 NotifyMotionArgs args;
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8924 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8925}
8926
Arthur Hung421eb1c2020-01-16 00:09:42 +08008927TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008928 addConfigurationProperty("touch.deviceType", "touchScreen");
8929 prepareDisplay(DISPLAY_ORIENTATION_0);
8930 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008932
8933 NotifyMotionArgs motionArgs;
8934
8935 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8936 // finger down
8937 processId(mapper, 1);
8938 processPosition(mapper, x1, y1);
8939 processSync(mapper);
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8943
8944 // finger move
8945 processId(mapper, 1);
8946 processPosition(mapper, x2, y2);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8951
8952 // finger up.
8953 processId(mapper, -1);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8958
8959 // new finger down
8960 processId(mapper, 1);
8961 processPosition(mapper, x3, y3);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8966}
8967
8968/**
arthurhungcc7f9802020-04-30 17:55:40 +08008969 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8970 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008971 */
arthurhungcc7f9802020-04-30 17:55:40 +08008972TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008973 addConfigurationProperty("touch.deviceType", "touchScreen");
8974 prepareDisplay(DISPLAY_ORIENTATION_0);
8975 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008977
8978 NotifyMotionArgs motionArgs;
8979
8980 // default tool type is finger
8981 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008982 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008983 processPosition(mapper, x1, y1);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8988
8989 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8990 processToolType(mapper, MT_TOOL_PALM);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8994
8995 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008996 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008997 processPosition(mapper, x2, y2);
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9000
9001 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009002 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9005
9006 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009007 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009008 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009009 processPosition(mapper, x3, y3);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9014}
9015
arthurhungbf89a482020-04-17 17:37:55 +08009016/**
arthurhungcc7f9802020-04-30 17:55:40 +08009017 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9018 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009019 */
arthurhungcc7f9802020-04-30 17:55:40 +08009020TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009021 addConfigurationProperty("touch.deviceType", "touchScreen");
9022 prepareDisplay(DISPLAY_ORIENTATION_0);
9023 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9025
9026 NotifyMotionArgs motionArgs;
9027
9028 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009029 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9030 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009031 processPosition(mapper, x1, y1);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9036
9037 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009038 processSlot(mapper, SECOND_SLOT);
9039 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009040 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009041 processSync(mapper);
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009043 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9045
9046 // If the tool type of the first finger changes to MT_TOOL_PALM,
9047 // we expect to receive ACTION_POINTER_UP with cancel flag.
9048 processSlot(mapper, FIRST_SLOT);
9049 processId(mapper, FIRST_TRACKING_ID);
9050 processToolType(mapper, MT_TOOL_PALM);
9051 processSync(mapper);
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009053 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009054 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9055
9056 // The following MOVE events of second finger should be processed.
9057 processSlot(mapper, SECOND_SLOT);
9058 processId(mapper, SECOND_TRACKING_ID);
9059 processPosition(mapper, x2 + 1, y2 + 1);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9063 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9064
9065 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9066 // it. Second finger receive move.
9067 processSlot(mapper, FIRST_SLOT);
9068 processId(mapper, INVALID_TRACKING_ID);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9072 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9073
9074 // Second finger keeps moving.
9075 processSlot(mapper, SECOND_SLOT);
9076 processId(mapper, SECOND_TRACKING_ID);
9077 processPosition(mapper, x2 + 2, y2 + 2);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9081 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9082
9083 // Second finger up.
9084 processId(mapper, INVALID_TRACKING_ID);
9085 processSync(mapper);
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9088 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9089}
9090
9091/**
9092 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9093 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9094 */
9095TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9096 addConfigurationProperty("touch.deviceType", "touchScreen");
9097 prepareDisplay(DISPLAY_ORIENTATION_0);
9098 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9100
9101 NotifyMotionArgs motionArgs;
9102
9103 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9104 // First finger down.
9105 processId(mapper, FIRST_TRACKING_ID);
9106 processPosition(mapper, x1, y1);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9111
9112 // Second finger down.
9113 processSlot(mapper, SECOND_SLOT);
9114 processId(mapper, SECOND_TRACKING_ID);
9115 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009118 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9120
arthurhungcc7f9802020-04-30 17:55:40 +08009121 // If the tool type of the first finger changes to MT_TOOL_PALM,
9122 // we expect to receive ACTION_POINTER_UP with cancel flag.
9123 processSlot(mapper, FIRST_SLOT);
9124 processId(mapper, FIRST_TRACKING_ID);
9125 processToolType(mapper, MT_TOOL_PALM);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009128 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009129 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9130
9131 // Second finger keeps moving.
9132 processSlot(mapper, SECOND_SLOT);
9133 processId(mapper, SECOND_TRACKING_ID);
9134 processPosition(mapper, x2 + 1, y2 + 1);
9135 processSync(mapper);
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9138
9139 // second finger becomes palm, receive cancel due to only 1 finger is active.
9140 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009141 processToolType(mapper, MT_TOOL_PALM);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9145
arthurhungcc7f9802020-04-30 17:55:40 +08009146 // third finger down.
9147 processSlot(mapper, THIRD_SLOT);
9148 processId(mapper, THIRD_TRACKING_ID);
9149 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009150 processPosition(mapper, x3, y3);
9151 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009155 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9156
9157 // third finger move
9158 processId(mapper, THIRD_TRACKING_ID);
9159 processPosition(mapper, x3 + 1, y3 + 1);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9163
9164 // first finger up, third finger receive move.
9165 processSlot(mapper, FIRST_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 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9171
9172 // second finger up, third finger receive move.
9173 processSlot(mapper, SECOND_SLOT);
9174 processId(mapper, INVALID_TRACKING_ID);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9178 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9179
9180 // third finger up.
9181 processSlot(mapper, THIRD_SLOT);
9182 processId(mapper, INVALID_TRACKING_ID);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9186 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9187}
9188
9189/**
9190 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9191 * and the active finger could still be allowed to receive the events
9192 */
9193TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9194 addConfigurationProperty("touch.deviceType", "touchScreen");
9195 prepareDisplay(DISPLAY_ORIENTATION_0);
9196 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9197 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9198
9199 NotifyMotionArgs motionArgs;
9200
9201 // default tool type is finger
9202 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9203 processId(mapper, FIRST_TRACKING_ID);
9204 processPosition(mapper, x1, y1);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9209
9210 // Second finger down.
9211 processSlot(mapper, SECOND_SLOT);
9212 processId(mapper, SECOND_TRACKING_ID);
9213 processPosition(mapper, x2, y2);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009216 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9218
9219 // If the tool type of the second finger changes to MT_TOOL_PALM,
9220 // we expect to receive ACTION_POINTER_UP with cancel flag.
9221 processId(mapper, SECOND_TRACKING_ID);
9222 processToolType(mapper, MT_TOOL_PALM);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009225 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009226 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9227
9228 // The following MOVE event should be processed.
9229 processSlot(mapper, FIRST_SLOT);
9230 processId(mapper, FIRST_TRACKING_ID);
9231 processPosition(mapper, x1 + 1, y1 + 1);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9235 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9236
9237 // second finger up.
9238 processSlot(mapper, SECOND_SLOT);
9239 processId(mapper, INVALID_TRACKING_ID);
9240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9243
9244 // first finger keep moving
9245 processSlot(mapper, FIRST_SLOT);
9246 processId(mapper, FIRST_TRACKING_ID);
9247 processPosition(mapper, x1 + 2, y1 + 2);
9248 processSync(mapper);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9251
9252 // first finger up.
9253 processId(mapper, INVALID_TRACKING_ID);
9254 processSync(mapper);
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9257 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009258}
9259
Arthur Hung9ad18942021-06-19 02:04:46 +00009260/**
9261 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9262 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9263 * cause slot be valid again.
9264 */
9265TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9266 addConfigurationProperty("touch.deviceType", "touchScreen");
9267 prepareDisplay(DISPLAY_ORIENTATION_0);
9268 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9269 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9270
9271 NotifyMotionArgs motionArgs;
9272
9273 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9274 // First finger down.
9275 processId(mapper, FIRST_TRACKING_ID);
9276 processPosition(mapper, x1, y1);
9277 processPressure(mapper, RAW_PRESSURE_MAX);
9278 processSync(mapper);
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9281 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9282
9283 // First finger move.
9284 processId(mapper, FIRST_TRACKING_ID);
9285 processPosition(mapper, x1 + 1, y1 + 1);
9286 processPressure(mapper, RAW_PRESSURE_MAX);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9290 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9291
9292 // Second finger down.
9293 processSlot(mapper, SECOND_SLOT);
9294 processId(mapper, SECOND_TRACKING_ID);
9295 processPosition(mapper, x2, y2);
9296 processPressure(mapper, RAW_PRESSURE_MAX);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009299 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009300 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9301
9302 // second finger up with some unexpected data.
9303 processSlot(mapper, SECOND_SLOT);
9304 processId(mapper, INVALID_TRACKING_ID);
9305 processPosition(mapper, x2, y2);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009308 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009309 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9310
9311 // first finger up with some unexpected data.
9312 processSlot(mapper, FIRST_SLOT);
9313 processId(mapper, INVALID_TRACKING_ID);
9314 processPosition(mapper, x2, y2);
9315 processPressure(mapper, RAW_PRESSURE_MAX);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9319 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9320}
9321
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009322// --- MultiTouchInputMapperTest_ExternalDevice ---
9323
9324class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9325protected:
Chris Yea52ade12020-08-27 16:49:20 -07009326 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009327};
9328
9329/**
9330 * Expect fallback to internal viewport if device is external and external viewport is not present.
9331 */
9332TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9333 prepareAxes(POSITION);
9334 addConfigurationProperty("touch.deviceType", "touchScreen");
9335 prepareDisplay(DISPLAY_ORIENTATION_0);
9336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9337
9338 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9339
9340 NotifyMotionArgs motionArgs;
9341
9342 // Expect the event to be sent to the internal viewport,
9343 // because an external viewport is not present.
9344 processPosition(mapper, 100, 100);
9345 processSync(mapper);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9347 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9348
9349 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009350 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009351 processPosition(mapper, 100, 100);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9355}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009356
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009357TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9358 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9359 std::shared_ptr<FakePointerController> fakePointerController =
9360 std::make_shared<FakePointerController>();
9361 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9362 fakePointerController->setPosition(0, 0);
9363 fakePointerController->setButtonState(0);
9364
9365 // prepare device and capture
9366 prepareDisplay(DISPLAY_ORIENTATION_0);
9367 prepareAxes(POSITION | ID | SLOT);
9368 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9370 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009371 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9373
9374 // captured touchpad should be a touchpad source
9375 NotifyDeviceResetArgs resetArgs;
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9377 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9378
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009379 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009380
9381 const InputDeviceInfo::MotionRange* relRangeX =
9382 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9383 ASSERT_NE(relRangeX, nullptr);
9384 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9385 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9386 const InputDeviceInfo::MotionRange* relRangeY =
9387 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9388 ASSERT_NE(relRangeY, nullptr);
9389 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9390 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9391
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009392 // run captured pointer tests - note that this is unscaled, so input listener events should be
9393 // identical to what the hardware sends (accounting for any
9394 // calibration).
9395 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009396 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009397 processId(mapper, 1);
9398 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9399 processKey(mapper, BTN_TOUCH, 1);
9400 processSync(mapper);
9401
9402 // expect coord[0] to contain initial location of touch 0
9403 NotifyMotionArgs args;
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9406 ASSERT_EQ(1U, args.pointerCount);
9407 ASSERT_EQ(0, args.pointerProperties[0].id);
9408 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9409 ASSERT_NO_FATAL_FAILURE(
9410 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9411
9412 // FINGER 1 DOWN
9413 processSlot(mapper, 1);
9414 processId(mapper, 2);
9415 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9416 processSync(mapper);
9417
9418 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009420 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009421 ASSERT_EQ(2U, args.pointerCount);
9422 ASSERT_EQ(0, args.pointerProperties[0].id);
9423 ASSERT_EQ(1, args.pointerProperties[1].id);
9424 ASSERT_NO_FATAL_FAILURE(
9425 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9426 ASSERT_NO_FATAL_FAILURE(
9427 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9428
9429 // FINGER 1 MOVE
9430 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9431 processSync(mapper);
9432
9433 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9434 // from move
9435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9437 ASSERT_NO_FATAL_FAILURE(
9438 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9439 ASSERT_NO_FATAL_FAILURE(
9440 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9441
9442 // FINGER 0 MOVE
9443 processSlot(mapper, 0);
9444 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9445 processSync(mapper);
9446
9447 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9450 ASSERT_NO_FATAL_FAILURE(
9451 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9452 ASSERT_NO_FATAL_FAILURE(
9453 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9454
9455 // BUTTON DOWN
9456 processKey(mapper, BTN_LEFT, 1);
9457 processSync(mapper);
9458
9459 // touchinputmapper design sends a move before button press
9460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9464
9465 // BUTTON UP
9466 processKey(mapper, BTN_LEFT, 0);
9467 processSync(mapper);
9468
9469 // touchinputmapper design sends a move after button release
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9474
9475 // FINGER 0 UP
9476 processId(mapper, -1);
9477 processSync(mapper);
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9479 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9480
9481 // FINGER 1 MOVE
9482 processSlot(mapper, 1);
9483 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9484 processSync(mapper);
9485
9486 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9489 ASSERT_EQ(1U, args.pointerCount);
9490 ASSERT_EQ(1, args.pointerProperties[0].id);
9491 ASSERT_NO_FATAL_FAILURE(
9492 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9493
9494 // FINGER 1 UP
9495 processId(mapper, -1);
9496 processKey(mapper, BTN_TOUCH, 0);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9500
9501 // non captured touchpad should be a mouse source
9502 mFakePolicy->setPointerCapture(false);
9503 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9505 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9506}
9507
9508TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9509 std::shared_ptr<FakePointerController> fakePointerController =
9510 std::make_shared<FakePointerController>();
9511 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9512 fakePointerController->setPosition(0, 0);
9513 fakePointerController->setButtonState(0);
9514
9515 // prepare device and capture
9516 prepareDisplay(DISPLAY_ORIENTATION_0);
9517 prepareAxes(POSITION | ID | SLOT);
9518 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9519 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009520 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9522 // run uncaptured pointer tests - pushes out generic events
9523 // FINGER 0 DOWN
9524 processId(mapper, 3);
9525 processPosition(mapper, 100, 100);
9526 processKey(mapper, BTN_TOUCH, 1);
9527 processSync(mapper);
9528
9529 // start at (100,100), cursor should be at (0,0) * scale
9530 NotifyMotionArgs args;
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9533 ASSERT_NO_FATAL_FAILURE(
9534 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9535
9536 // FINGER 0 MOVE
9537 processPosition(mapper, 200, 200);
9538 processSync(mapper);
9539
9540 // compute scaling to help with touch position checking
9541 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9542 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9543 float scale =
9544 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9545
9546 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9548 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9550 0, 0, 0, 0, 0, 0, 0));
9551}
9552
9553TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9554 std::shared_ptr<FakePointerController> fakePointerController =
9555 std::make_shared<FakePointerController>();
9556
9557 prepareDisplay(DISPLAY_ORIENTATION_0);
9558 prepareAxes(POSITION | ID | SLOT);
9559 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009560 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009561 mFakePolicy->setPointerCapture(false);
9562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9563
9564 // uncaptured touchpad should be a pointer device
9565 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9566
9567 // captured touchpad should be a touchpad device
9568 mFakePolicy->setPointerCapture(true);
9569 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9570 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9571}
9572
HQ Liue6983c72022-04-19 22:14:56 +00009573class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9574protected:
9575 float mPointerMovementScale;
9576 float mPointerXZoomScale;
9577 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9578 addConfigurationProperty("touch.deviceType", "pointer");
9579 std::shared_ptr<FakePointerController> fakePointerController =
9580 std::make_shared<FakePointerController>();
9581 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9582 fakePointerController->setPosition(0, 0);
9583 fakePointerController->setButtonState(0);
9584 prepareDisplay(DISPLAY_ORIENTATION_0);
9585
9586 prepareAxes(POSITION);
9587 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9588 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9589 // needs to be disabled, and the pointer gesture needs to be enabled.
9590 mFakePolicy->setPointerCapture(false);
9591 mFakePolicy->setPointerGestureEnabled(true);
9592 mFakePolicy->setPointerController(fakePointerController);
9593
9594 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9595 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9596 mPointerMovementScale =
9597 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9598 mPointerXZoomScale =
9599 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9600 }
9601
9602 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9604 /*flat*/ 0,
9605 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9606 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9607 /*flat*/ 0,
9608 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9609 }
9610};
9611
9612/**
9613 * Two fingers down on a pointer mode touch pad. The width
9614 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9615 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9616 * be greater than the both value to be freeform gesture, so that after two
9617 * fingers start to move downwards, the gesture should be swipe.
9618 */
9619TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9620 // The min freeform gesture width is 25units/mm x 30mm = 750
9621 // which is greater than fraction of the diagnal length of the touchpad (349).
9622 // Thus, MaxSwipWidth is 750.
9623 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9624 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9625 NotifyMotionArgs motionArgs;
9626
9627 // Two fingers down at once.
9628 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9629 // Pointer's initial position is used the [0,0] coordinate.
9630 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9631
9632 processId(mapper, FIRST_TRACKING_ID);
9633 processPosition(mapper, x1, y1);
9634 processMTSync(mapper);
9635 processId(mapper, SECOND_TRACKING_ID);
9636 processPosition(mapper, x2, y2);
9637 processMTSync(mapper);
9638 processSync(mapper);
9639
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9641 ASSERT_EQ(1U, motionArgs.pointerCount);
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9644 ASSERT_NO_FATAL_FAILURE(
9645 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9646
9647 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9648 // that there should be 1 pointer.
9649 int32_t movingDistance = 200;
9650 y1 += movingDistance;
9651 y2 += movingDistance;
9652
9653 processId(mapper, FIRST_TRACKING_ID);
9654 processPosition(mapper, x1, y1);
9655 processMTSync(mapper);
9656 processId(mapper, SECOND_TRACKING_ID);
9657 processPosition(mapper, x2, y2);
9658 processMTSync(mapper);
9659 processSync(mapper);
9660
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(1U, motionArgs.pointerCount);
9663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9666 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9667 0, 0, 0, 0));
9668}
9669
9670/**
9671 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9672 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9673 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9674 * value to be freeform gesture, so that after two fingers start to move downwards,
9675 * the gesture should be swipe.
9676 */
9677TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9678 // The min freeform gesture width is 5units/mm x 30mm = 150
9679 // which is greater than fraction of the diagnal length of the touchpad (349).
9680 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9681 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9683 NotifyMotionArgs motionArgs;
9684
9685 // Two fingers down at once.
9686 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9687 // Pointer's initial position is used the [0,0] coordinate.
9688 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9689
9690 processId(mapper, FIRST_TRACKING_ID);
9691 processPosition(mapper, x1, y1);
9692 processMTSync(mapper);
9693 processId(mapper, SECOND_TRACKING_ID);
9694 processPosition(mapper, x2, y2);
9695 processMTSync(mapper);
9696 processSync(mapper);
9697
9698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9699 ASSERT_EQ(1U, motionArgs.pointerCount);
9700 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9702 ASSERT_NO_FATAL_FAILURE(
9703 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9704
9705 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9706 // and there should be 1 pointer.
9707 int32_t movingDistance = 200;
9708 y1 += movingDistance;
9709 y2 += movingDistance;
9710
9711 processId(mapper, FIRST_TRACKING_ID);
9712 processPosition(mapper, x1, y1);
9713 processMTSync(mapper);
9714 processId(mapper, SECOND_TRACKING_ID);
9715 processPosition(mapper, x2, y2);
9716 processMTSync(mapper);
9717 processSync(mapper);
9718
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9720 ASSERT_EQ(1U, motionArgs.pointerCount);
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9723 // New coordinate is the scaled relative coordinate from the initial coordinate.
9724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9725 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9726 0, 0, 0, 0));
9727}
9728
9729/**
9730 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9731 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9732 * freeform gestures after two fingers start to move downwards.
9733 */
9734TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9735 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9736 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9737
9738 NotifyMotionArgs motionArgs;
9739
9740 // Two fingers down at once. Wider than the max swipe width.
9741 // The gesture is expected to be PRESS, then transformed to FREEFORM
9742 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9743
9744 processId(mapper, FIRST_TRACKING_ID);
9745 processPosition(mapper, x1, y1);
9746 processMTSync(mapper);
9747 processId(mapper, SECOND_TRACKING_ID);
9748 processPosition(mapper, x2, y2);
9749 processMTSync(mapper);
9750 processSync(mapper);
9751
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(1U, motionArgs.pointerCount);
9754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9755 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9756 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9757 ASSERT_NO_FATAL_FAILURE(
9758 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9759
9760 int32_t movingDistance = 200;
9761
9762 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9763 // then two down events for two pointers.
9764 y1 += movingDistance;
9765 y2 += movingDistance;
9766
9767 processId(mapper, FIRST_TRACKING_ID);
9768 processPosition(mapper, x1, y1);
9769 processMTSync(mapper);
9770 processId(mapper, SECOND_TRACKING_ID);
9771 processPosition(mapper, x2, y2);
9772 processMTSync(mapper);
9773 processSync(mapper);
9774
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9776 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9777 ASSERT_EQ(1U, motionArgs.pointerCount);
9778 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9781 ASSERT_EQ(1U, motionArgs.pointerCount);
9782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9785 ASSERT_EQ(2U, motionArgs.pointerCount);
9786 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9787 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9788 // Two pointers' scaled relative coordinates from their initial centroid.
9789 // Initial y coordinates are 0 as y1 and y2 have the same value.
9790 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9791 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9792 // When pointers move, the new coordinates equal to the initial coordinates plus
9793 // scaled moving distance.
9794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9795 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9796 0, 0, 0, 0));
9797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9798 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9799 0, 0, 0, 0));
9800
9801 // Move two fingers down again, expect one MOVE motion event.
9802 y1 += movingDistance;
9803 y2 += movingDistance;
9804
9805 processId(mapper, FIRST_TRACKING_ID);
9806 processPosition(mapper, x1, y1);
9807 processMTSync(mapper);
9808 processId(mapper, SECOND_TRACKING_ID);
9809 processPosition(mapper, x2, y2);
9810 processMTSync(mapper);
9811 processSync(mapper);
9812
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(2U, motionArgs.pointerCount);
9815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9818 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9819 0, 0, 0, 0, 0));
9820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9821 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9822 0, 0, 0, 0, 0));
9823}
9824
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009825// --- JoystickInputMapperTest ---
9826
9827class JoystickInputMapperTest : public InputMapperTest {
9828protected:
9829 static const int32_t RAW_X_MIN;
9830 static const int32_t RAW_X_MAX;
9831 static const int32_t RAW_Y_MIN;
9832 static const int32_t RAW_Y_MAX;
9833
9834 void SetUp() override {
9835 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9836 }
9837 void prepareAxes() {
9838 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9839 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9840 }
9841
9842 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9844 }
9845
9846 void processSync(JoystickInputMapper& mapper) {
9847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9848 }
9849
9850 void prepareVirtualDisplay(int32_t orientation) {
9851 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9852 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9853 NO_PORT, ViewportType::VIRTUAL);
9854 }
9855};
9856
9857const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9858const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9859const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9860const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9861
9862TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9863 prepareAxes();
9864 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9865
9866 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9867
9868 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9869
9870 // Send an axis event
9871 processAxis(mapper, ABS_X, 100);
9872 processSync(mapper);
9873
9874 NotifyMotionArgs args;
9875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9876 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9877
9878 // Send another axis event
9879 processAxis(mapper, ABS_Y, 100);
9880 processSync(mapper);
9881
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9883 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9884}
9885
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009886// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009887
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009888class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009889protected:
9890 static const char* DEVICE_NAME;
9891 static const char* DEVICE_LOCATION;
9892 static const int32_t DEVICE_ID;
9893 static const int32_t DEVICE_GENERATION;
9894 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009895 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009896 static const int32_t EVENTHUB_ID;
9897
9898 std::shared_ptr<FakeEventHub> mFakeEventHub;
9899 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009900 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009901 std::unique_ptr<InstrumentedInputReader> mReader;
9902 std::shared_ptr<InputDevice> mDevice;
9903
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009904 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009905 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009906 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009907 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009908 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009909 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009910 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9911 }
9912
9913 void SetUp() override { SetUp(DEVICE_CLASSES); }
9914
9915 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009916 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009917 mFakePolicy.clear();
9918 }
9919
9920 void configureDevice(uint32_t changes) {
9921 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9922 mReader->requestRefreshConfiguration(changes);
9923 mReader->loopOnce();
9924 }
9925 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9926 }
9927
9928 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9929 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009930 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009931 InputDeviceIdentifier identifier;
9932 identifier.name = name;
9933 identifier.location = location;
9934 std::shared_ptr<InputDevice> device =
9935 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9936 identifier);
9937 mReader->pushNextDevice(device);
9938 mFakeEventHub->addDevice(eventHubId, name, classes);
9939 mReader->loopOnce();
9940 return device;
9941 }
9942
9943 template <class T, typename... Args>
9944 T& addControllerAndConfigure(Args... args) {
9945 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9946
9947 return controller;
9948 }
9949};
9950
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009951const char* PeripheralControllerTest::DEVICE_NAME = "device";
9952const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9953const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9954const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9955const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009956const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9957 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009958const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009959
9960// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009961class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962protected:
9963 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009964 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009965 }
9966};
9967
9968TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009969 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009970
9971 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9972 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9973}
9974
9975TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009976 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009977
9978 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9979 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9980}
9981
9982// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009983class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009984protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009985 void SetUp() override {
9986 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9987 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009988};
9989
Chris Ye85758332021-05-16 23:05:17 -07009990TEST_F(LightControllerTest, MonoLight) {
9991 RawLightInfo infoMono = {.id = 1,
9992 .name = "Mono",
9993 .maxBrightness = 255,
9994 .flags = InputLightClass::BRIGHTNESS,
9995 .path = ""};
9996 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009997
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009998 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009999 InputDeviceInfo info;
10000 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010001 std::vector<InputDeviceLightInfo> lights = info.getLights();
10002 ASSERT_EQ(1U, lights.size());
10003 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010004
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010005 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10006 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010007}
10008
10009TEST_F(LightControllerTest, RGBLight) {
10010 RawLightInfo infoRed = {.id = 1,
10011 .name = "red",
10012 .maxBrightness = 255,
10013 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10014 .path = ""};
10015 RawLightInfo infoGreen = {.id = 2,
10016 .name = "green",
10017 .maxBrightness = 255,
10018 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10019 .path = ""};
10020 RawLightInfo infoBlue = {.id = 3,
10021 .name = "blue",
10022 .maxBrightness = 255,
10023 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10024 .path = ""};
10025 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10026 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10027 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10028
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010029 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010030 InputDeviceInfo info;
10031 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010032 std::vector<InputDeviceLightInfo> lights = info.getLights();
10033 ASSERT_EQ(1U, lights.size());
10034 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010035
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010036 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10037 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010038}
10039
10040TEST_F(LightControllerTest, MultiColorRGBLight) {
10041 RawLightInfo infoColor = {.id = 1,
10042 .name = "red",
10043 .maxBrightness = 255,
10044 .flags = InputLightClass::BRIGHTNESS |
10045 InputLightClass::MULTI_INTENSITY |
10046 InputLightClass::MULTI_INDEX,
10047 .path = ""};
10048
10049 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10050
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010051 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010052 InputDeviceInfo info;
10053 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010054 std::vector<InputDeviceLightInfo> lights = info.getLights();
10055 ASSERT_EQ(1U, lights.size());
10056 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010057
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010058 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10059 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010060}
10061
10062TEST_F(LightControllerTest, PlayerIdLight) {
10063 RawLightInfo info1 = {.id = 1,
10064 .name = "player1",
10065 .maxBrightness = 255,
10066 .flags = InputLightClass::BRIGHTNESS,
10067 .path = ""};
10068 RawLightInfo info2 = {.id = 2,
10069 .name = "player2",
10070 .maxBrightness = 255,
10071 .flags = InputLightClass::BRIGHTNESS,
10072 .path = ""};
10073 RawLightInfo info3 = {.id = 3,
10074 .name = "player3",
10075 .maxBrightness = 255,
10076 .flags = InputLightClass::BRIGHTNESS,
10077 .path = ""};
10078 RawLightInfo info4 = {.id = 4,
10079 .name = "player4",
10080 .maxBrightness = 255,
10081 .flags = InputLightClass::BRIGHTNESS,
10082 .path = ""};
10083 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10084 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10085 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10086 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10087
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010088 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010089 InputDeviceInfo info;
10090 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010091 std::vector<InputDeviceLightInfo> lights = info.getLights();
10092 ASSERT_EQ(1U, lights.size());
10093 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010094
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010095 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10096 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10097 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010098}
10099
Michael Wrightd02c5b62014-02-10 15:10:22 -080010100} // namespace android