blob: dadaa6789127d426aa96fc38bdc25a95bfa961f1 [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
Andy Chenf9f1a022022-08-29 20:07:10 -0400994 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
995 return {DEFAULT_BATTERY};
996 }
Chris Yee2b1e5c2021-03-10 22:45:12 -0800997
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000998 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
999 int32_t batteryId) const override {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001000 return std::nullopt;
1001 }
Kim Low03ea0352020-11-06 12:45:07 -08001002
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001003 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001004 std::vector<int32_t> ids;
1005 for (const auto& [rawId, info] : mRawLightInfos) {
1006 ids.push_back(rawId);
1007 }
1008 return ids;
1009 }
1010
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001011 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001012 auto it = mRawLightInfos.find(lightId);
1013 if (it == mRawLightInfos.end()) {
1014 return std::nullopt;
1015 }
1016 return it->second;
1017 }
1018
1019 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1020 mLightBrightness.emplace(lightId, brightness);
1021 }
1022
1023 void setLightIntensities(int32_t deviceId, int32_t lightId,
1024 std::unordered_map<LightColor, int32_t> intensities) override {
1025 mLightIntensities.emplace(lightId, intensities);
1026 };
1027
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001028 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001029 auto lightIt = mLightBrightness.find(lightId);
1030 if (lightIt == mLightBrightness.end()) {
1031 return std::nullopt;
1032 }
1033 return lightIt->second;
1034 }
1035
1036 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001037 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001038 auto lightIt = mLightIntensities.find(lightId);
1039 if (lightIt == mLightIntensities.end()) {
1040 return std::nullopt;
1041 }
1042 return lightIt->second;
1043 };
1044
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001045 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048
Chris Yea52ade12020-08-27 16:49:20 -07001049 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050
Chris Yea52ade12020-08-27 16:49:20 -07001051 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052};
1053
Michael Wrightd02c5b62014-02-10 15:10:22 -08001054// --- FakeInputMapper ---
1055
1056class FakeInputMapper : public InputMapper {
1057 uint32_t mSources;
1058 int32_t mKeyboardType;
1059 int32_t mMetaState;
1060 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1061 KeyedVector<int32_t, int32_t> mScanCodeStates;
1062 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001063 // fake mapping which would normally come from keyCharacterMap
1064 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001065 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001067 std::mutex mLock;
1068 std::condition_variable mStateChangedCondition;
1069 bool mConfigureWasCalled GUARDED_BY(mLock);
1070 bool mResetWasCalled GUARDED_BY(mLock);
1071 bool mProcessWasCalled GUARDED_BY(mLock);
1072 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073
Arthur Hungc23540e2018-11-29 20:42:11 +08001074 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001076 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1077 : InputMapper(deviceContext),
1078 mSources(sources),
1079 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001080 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001081 mConfigureWasCalled(false),
1082 mResetWasCalled(false),
1083 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
Chris Yea52ade12020-08-27 16:49:20 -07001085 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086
1087 void setKeyboardType(int32_t keyboardType) {
1088 mKeyboardType = keyboardType;
1089 }
1090
1091 void setMetaState(int32_t metaState) {
1092 mMetaState = metaState;
1093 }
1094
1095 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001096 std::unique_lock<std::mutex> lock(mLock);
1097 base::ScopedLockAssertion assumeLocked(mLock);
1098 const bool configureCalled =
1099 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1100 return mConfigureWasCalled;
1101 });
1102 if (!configureCalled) {
1103 FAIL() << "Expected configure() to have been called.";
1104 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001105 mConfigureWasCalled = false;
1106 }
1107
1108 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001109 std::unique_lock<std::mutex> lock(mLock);
1110 base::ScopedLockAssertion assumeLocked(mLock);
1111 const bool resetCalled =
1112 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1113 return mResetWasCalled;
1114 });
1115 if (!resetCalled) {
1116 FAIL() << "Expected reset() to have been called.";
1117 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001118 mResetWasCalled = false;
1119 }
1120
Yi Kong9b14ac62018-07-17 13:48:38 -07001121 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001122 std::unique_lock<std::mutex> lock(mLock);
1123 base::ScopedLockAssertion assumeLocked(mLock);
1124 const bool processCalled =
1125 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1126 return mProcessWasCalled;
1127 });
1128 if (!processCalled) {
1129 FAIL() << "Expected process() to have been called.";
1130 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001131 if (outLastEvent) {
1132 *outLastEvent = mLastEvent;
1133 }
1134 mProcessWasCalled = false;
1135 }
1136
1137 void setKeyCodeState(int32_t keyCode, int32_t state) {
1138 mKeyCodeStates.replaceValueFor(keyCode, state);
1139 }
1140
1141 void setScanCodeState(int32_t scanCode, int32_t state) {
1142 mScanCodeStates.replaceValueFor(scanCode, state);
1143 }
1144
1145 void setSwitchState(int32_t switchCode, int32_t state) {
1146 mSwitchStates.replaceValueFor(switchCode, state);
1147 }
1148
1149 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001150 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 }
1152
Philip Junker4af3b3d2021-12-14 10:36:55 +01001153 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1154 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1155 }
1156
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001158 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159
Chris Yea52ade12020-08-27 16:49:20 -07001160 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 InputMapper::populateDeviceInfo(deviceInfo);
1162
1163 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1164 deviceInfo->setKeyboardType(mKeyboardType);
1165 }
1166 }
1167
Chris Yea52ade12020-08-27 16:49:20 -07001168 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001169 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001171
1172 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001173 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001174 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1175 mViewport = config->getDisplayViewportByPort(*displayPort);
1176 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001177
1178 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179 }
1180
Chris Yea52ade12020-08-27 16:49:20 -07001181 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001182 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001184 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 }
1186
Chris Yea52ade12020-08-27 16:49:20 -07001187 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 mLastEvent = *rawEvent;
1190 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001191 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 }
1193
Chris Yea52ade12020-08-27 16:49:20 -07001194 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1196 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1197 }
1198
Philip Junker4af3b3d2021-12-14 10:36:55 +01001199 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1200 auto it = mKeyCodeMapping.find(locationKeyCode);
1201 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1206 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1207 }
1208
Chris Yea52ade12020-08-27 16:49:20 -07001209 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1211 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1212 }
1213
Chris Yea52ade12020-08-27 16:49:20 -07001214 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001215 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001216 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001217 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1219 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1220 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 }
1222 }
1223 }
Chris Yea52ade12020-08-27 16:49:20 -07001224 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 return result;
1226 }
1227
1228 virtual int32_t getMetaState() {
1229 return mMetaState;
1230 }
1231
1232 virtual void fadePointer() {
1233 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001234
1235 virtual std::optional<int32_t> getAssociatedDisplay() {
1236 if (mViewport) {
1237 return std::make_optional(mViewport->displayId);
1238 }
1239 return std::nullopt;
1240 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241};
1242
1243
1244// --- InstrumentedInputReader ---
1245
1246class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001247 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248
1249public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001250 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1251 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001252 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001253 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001255 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001257 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001259 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001260 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 InputDeviceIdentifier identifier;
1262 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001263 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001265 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001266 }
1267
Prabir Pradhan28efc192019-11-05 01:10:04 +00001268 // Make the protected loopOnce method accessible to tests.
1269 using InputReader::loopOnce;
1270
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001272 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1273 const InputDeviceIdentifier& identifier)
1274 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001275 if (!mNextDevices.empty()) {
1276 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1277 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 return device;
1279 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001280 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281 }
1282
arthurhungdcef2dc2020-08-11 14:47:50 +08001283 // --- FakeInputReaderContext ---
1284 class FakeInputReaderContext : public ContextImpl {
1285 int32_t mGlobalMetaState;
1286 bool mUpdateGlobalMetaStateWasCalled;
1287 int32_t mGeneration;
1288
1289 public:
1290 FakeInputReaderContext(InputReader* reader)
1291 : ContextImpl(reader),
1292 mGlobalMetaState(0),
1293 mUpdateGlobalMetaStateWasCalled(false),
1294 mGeneration(1) {}
1295
1296 virtual ~FakeInputReaderContext() {}
1297
1298 void assertUpdateGlobalMetaStateWasCalled() {
1299 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1300 << "Expected updateGlobalMetaState() to have been called.";
1301 mUpdateGlobalMetaStateWasCalled = false;
1302 }
1303
1304 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1305
1306 uint32_t getGeneration() { return mGeneration; }
1307
1308 void updateGlobalMetaState() override {
1309 mUpdateGlobalMetaStateWasCalled = true;
1310 ContextImpl::updateGlobalMetaState();
1311 }
1312
1313 int32_t getGlobalMetaState() override {
1314 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1315 }
1316
1317 int32_t bumpGeneration() override {
1318 mGeneration = ContextImpl::bumpGeneration();
1319 return mGeneration;
1320 }
1321 } mFakeContext;
1322
Michael Wrightd02c5b62014-02-10 15:10:22 -08001323 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001324
1325public:
1326 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001327};
1328
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329// --- InputReaderPolicyTest ---
1330class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001331protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001332 sp<FakeInputReaderPolicy> mFakePolicy;
1333
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001334 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001335 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336};
1337
1338/**
1339 * Check that empty set of viewports is an acceptable configuration.
1340 * Also try to get internal viewport two different ways - by type and by uniqueId.
1341 *
1342 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1343 * Such configuration is not currently allowed.
1344 */
1345TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001346 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347
1348 // We didn't add any viewports yet, so there shouldn't be any.
1349 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001350 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 ASSERT_FALSE(internalViewport);
1352
1353 // Add an internal viewport, then clear it
1354 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001355 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001356 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357
1358 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001359 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362
1363 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001364 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001366 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001367
1368 mFakePolicy->clearViewports();
1369 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001370 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001372 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373 ASSERT_FALSE(internalViewport);
1374}
1375
1376TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1377 const std::string internalUniqueId = "local:0";
1378 const std::string externalUniqueId = "local:1";
1379 const std::string virtualUniqueId1 = "virtual:2";
1380 const std::string virtualUniqueId2 = "virtual:3";
1381 constexpr int32_t virtualDisplayId1 = 2;
1382 constexpr int32_t virtualDisplayId2 = 3;
1383
1384 // Add an internal viewport
1385 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001386 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1387 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001388 // Add an external viewport
1389 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001390 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1391 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001392 // Add an virtual viewport
1393 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001394 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1395 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 // Add another virtual viewport
1397 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001398 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1399 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400
1401 // Check matching by type for internal
1402 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001403 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001404 ASSERT_TRUE(internalViewport);
1405 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1406
1407 // Check matching by type for external
1408 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001409 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001410 ASSERT_TRUE(externalViewport);
1411 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1412
1413 // Check matching by uniqueId for virtual viewport #1
1414 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001415 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001417 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1419 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1420
1421 // Check matching by uniqueId for virtual viewport #2
1422 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001423 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001424 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001425 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001426 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1427 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1428}
1429
1430
1431/**
1432 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1433 * that lookup works by checking display id.
1434 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1435 */
1436TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1437 const std::string uniqueId1 = "uniqueId1";
1438 const std::string uniqueId2 = "uniqueId2";
1439 constexpr int32_t displayId1 = 2;
1440 constexpr int32_t displayId2 = 3;
1441
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001442 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1443 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444 for (const ViewportType& type : types) {
1445 mFakePolicy->clearViewports();
1446 // Add a viewport
1447 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001448 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1449 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 // Add another viewport
1451 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001452 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1453 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454
1455 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001456 std::optional<DisplayViewport> viewport1 =
1457 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001458 ASSERT_TRUE(viewport1);
1459 ASSERT_EQ(displayId1, viewport1->displayId);
1460 ASSERT_EQ(type, viewport1->type);
1461
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001462 std::optional<DisplayViewport> viewport2 =
1463 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001464 ASSERT_TRUE(viewport2);
1465 ASSERT_EQ(displayId2, viewport2->displayId);
1466 ASSERT_EQ(type, viewport2->type);
1467
1468 // When there are multiple viewports of the same kind, and uniqueId is not specified
1469 // in the call to getDisplayViewport, then that situation is not supported.
1470 // The viewports can be stored in any order, so we cannot rely on the order, since that
1471 // is just implementation detail.
1472 // However, we can check that it still returns *a* viewport, we just cannot assert
1473 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001474 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 ASSERT_TRUE(someViewport);
1476 }
1477}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001478
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001480 * When we have multiple internal displays make sure we always return the default display when
1481 * querying by type.
1482 */
1483TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1484 const std::string uniqueId1 = "uniqueId1";
1485 const std::string uniqueId2 = "uniqueId2";
1486 constexpr int32_t nonDefaultDisplayId = 2;
1487 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1488 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1489
1490 // Add the default display first and ensure it gets returned.
1491 mFakePolicy->clearViewports();
1492 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001493 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001494 ViewportType::INTERNAL);
1495 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001496 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001497 ViewportType::INTERNAL);
1498
1499 std::optional<DisplayViewport> viewport =
1500 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1501 ASSERT_TRUE(viewport);
1502 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1503 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1504
1505 // Add the default display second to make sure order doesn't matter.
1506 mFakePolicy->clearViewports();
1507 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001511 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001512 ViewportType::INTERNAL);
1513
1514 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1515 ASSERT_TRUE(viewport);
1516 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1517 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1518}
1519
1520/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001521 * Check getDisplayViewportByPort
1522 */
1523TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001524 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001525 const std::string uniqueId1 = "uniqueId1";
1526 const std::string uniqueId2 = "uniqueId2";
1527 constexpr int32_t displayId1 = 1;
1528 constexpr int32_t displayId2 = 2;
1529 const uint8_t hdmi1 = 0;
1530 const uint8_t hdmi2 = 1;
1531 const uint8_t hdmi3 = 2;
1532
1533 mFakePolicy->clearViewports();
1534 // Add a viewport that's associated with some display port that's not of interest.
1535 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001536 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1537 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 // Add another viewport, connected to HDMI1 port
1539 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001540 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1541 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001542
1543 // Check that correct display viewport was returned by comparing the display ports.
1544 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1545 ASSERT_TRUE(hdmi1Viewport);
1546 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1547 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1548
1549 // Check that we can still get the same viewport using the uniqueId
1550 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1551 ASSERT_TRUE(hdmi1Viewport);
1552 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1553 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1554 ASSERT_EQ(type, hdmi1Viewport->type);
1555
1556 // Check that we cannot find a port with "HDMI2", because we never added one
1557 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1558 ASSERT_FALSE(hdmi2Viewport);
1559}
1560
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561// --- InputReaderTest ---
1562
1563class InputReaderTest : public testing::Test {
1564protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001565 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001567 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001568 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569
Chris Yea52ade12020-08-27 16:49:20 -07001570 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001571 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001572 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001573 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574
Prabir Pradhan28efc192019-11-05 01:10:04 +00001575 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001576 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577 }
1578
Chris Yea52ade12020-08-27 16:49:20 -07001579 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001580 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 }
1583
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001584 void addDevice(int32_t eventHubId, const std::string& name,
1585 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001589 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 }
1591 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001592 mReader->loopOnce();
1593 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001594 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1595 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596 }
1597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001598 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001599 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001600 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601 }
1602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001603 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001604 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001605 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001606 }
1607
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001608 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001609 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001610 ftl::Flags<InputDeviceClass> classes,
1611 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001612 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1614 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001615 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001616 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 return mapper;
1618 }
1619};
1620
Chris Ye98d3f532020-10-01 21:48:59 -07001621TEST_F(InputReaderTest, PolicyGetInputDevices) {
1622 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001623 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001624 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625
1626 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001627 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001630 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1632 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001633 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001634}
1635
Chris Yee7310032020-09-22 15:36:28 -07001636TEST_F(InputReaderTest, GetMergedInputDevices) {
1637 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1638 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1639 // Add two subdevices to device
1640 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1641 // Must add at least one mapper or the device will be ignored!
1642 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1643 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1644
1645 // Push same device instance for next device to be added, so they'll have same identifier.
1646 mReader->pushNextDevice(device);
1647 mReader->pushNextDevice(device);
1648 ASSERT_NO_FATAL_FAILURE(
1649 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1650 ASSERT_NO_FATAL_FAILURE(
1651 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1652
1653 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001654 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001655}
1656
Chris Yee14523a2020-12-19 13:46:00 -08001657TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1658 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1659 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1660 // Add two subdevices to device
1661 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1662 // Must add at least one mapper or the device will be ignored!
1663 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1664 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1665
1666 // Push same device instance for next device to be added, so they'll have same identifier.
1667 mReader->pushNextDevice(device);
1668 mReader->pushNextDevice(device);
1669 // Sensor device is initially disabled
1670 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1671 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1672 nullptr));
1673 // Device is disabled because the only sub device is a sensor device and disabled initially.
1674 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1675 ASSERT_FALSE(device->isEnabled());
1676 ASSERT_NO_FATAL_FAILURE(
1677 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1678 // The merged device is enabled if any sub device is enabled
1679 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1680 ASSERT_TRUE(device->isEnabled());
1681}
1682
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001683TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001684 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001685 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 constexpr int32_t eventHubId = 1;
1687 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001688 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001690 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001692
Yi Kong9b14ac62018-07-17 13:48:38 -07001693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694
1695 NotifyDeviceResetArgs resetArgs;
1696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001697 ASSERT_EQ(deviceId, resetArgs.deviceId);
1698
1699 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001700 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001701 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001704 ASSERT_EQ(deviceId, resetArgs.deviceId);
1705 ASSERT_EQ(device->isEnabled(), false);
1706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001707 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001708 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001711 ASSERT_EQ(device->isEnabled(), false);
1712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001713 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001714 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001716 ASSERT_EQ(deviceId, resetArgs.deviceId);
1717 ASSERT_EQ(device->isEnabled(), true);
1718}
1719
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001721 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001722 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001725 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001726 AINPUT_SOURCE_KEYBOARD, nullptr);
1727 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001728
1729 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1730 AINPUT_SOURCE_ANY, AKEYCODE_A))
1731 << "Should return unknown when the device id is >= 0 but unknown.";
1732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1734 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1735 << "Should return unknown when the device id is valid but the sources are not "
1736 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001738 ASSERT_EQ(AKEY_STATE_DOWN,
1739 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1740 AKEYCODE_A))
1741 << "Should return value provided by mapper when device id is valid and the device "
1742 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001743
1744 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1745 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1746 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1747
1748 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1749 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1750 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1751}
1752
Philip Junker4af3b3d2021-12-14 10:36:55 +01001753TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1754 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1755 constexpr int32_t eventHubId = 1;
1756 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1757 InputDeviceClass::KEYBOARD,
1758 AINPUT_SOURCE_KEYBOARD, nullptr);
1759 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1760
1761 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1762 << "Should return unknown when the device with the specified id is not found.";
1763
1764 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1765 << "Should return correct mapping when device id is valid and mapping exists.";
1766
1767 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1768 << "Should return the location key code when device id is valid and there's no "
1769 "mapping.";
1770}
1771
1772TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1773 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1774 constexpr int32_t eventHubId = 1;
1775 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1776 InputDeviceClass::JOYSTICK,
1777 AINPUT_SOURCE_GAMEPAD, nullptr);
1778 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1779
1780 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1781 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1782}
1783
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001786 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001788 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 AINPUT_SOURCE_KEYBOARD, nullptr);
1791 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001792
1793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1794 AINPUT_SOURCE_ANY, KEY_A))
1795 << "Should return unknown when the device id is >= 0 but unknown.";
1796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1798 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1799 << "Should return unknown when the device id is valid but the sources are not "
1800 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 ASSERT_EQ(AKEY_STATE_DOWN,
1803 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1804 KEY_A))
1805 << "Should return value provided by mapper when device id is valid and the device "
1806 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807
1808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1809 AINPUT_SOURCE_TRACKBALL, KEY_A))
1810 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1811
1812 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1813 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1814 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1815}
1816
1817TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001819 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001823 AINPUT_SOURCE_KEYBOARD, nullptr);
1824 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001825
1826 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1827 AINPUT_SOURCE_ANY, SW_LID))
1828 << "Should return unknown when the device id is >= 0 but unknown.";
1829
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001830 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1831 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1832 << "Should return unknown when the device id is valid but the sources are not "
1833 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001834
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001835 ASSERT_EQ(AKEY_STATE_DOWN,
1836 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1837 SW_LID))
1838 << "Should return value provided by mapper when device id is valid and the device "
1839 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001840
1841 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1842 AINPUT_SOURCE_TRACKBALL, SW_LID))
1843 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1844
1845 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1846 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1847 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1848}
1849
1850TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001852 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001854 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001858 mapper.addSupportedKeyCode(AKEYCODE_A);
1859 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001861 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 uint8_t flags[4] = { 0, 0, 0, 1 };
1863
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001864 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865 << "Should return false when device id is >= 0 but unknown.";
1866 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1867
1868 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001869 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 << "Should return false when device id is valid but the sources are not supported by "
1871 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001872 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1873
1874 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001875 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 keyCodes, flags))
1877 << "Should return value provided by mapper when device id is valid and the device "
1878 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001879 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1880
1881 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001882 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1883 << "Should return false when the device id is < 0 but the sources are not supported by "
1884 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1886
1887 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001888 ASSERT_TRUE(
1889 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1890 << "Should return value provided by mapper when device id is < 0 and one of the "
1891 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1893}
1894
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001895TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001896 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001897 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898
1899 NotifyConfigurationChangedArgs args;
1900
1901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1902 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1903}
1904
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001905TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001907 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001908 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001910 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001911 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001912 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001913 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001915 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001916 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1918
1919 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001920 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001921 ASSERT_EQ(when, event.when);
1922 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924 ASSERT_EQ(EV_KEY, event.type);
1925 ASSERT_EQ(KEY_A, event.code);
1926 ASSERT_EQ(1, event.value);
1927}
1928
Garfield Tan1c7bc862020-01-28 13:24:04 -08001929TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001930 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001931 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 constexpr int32_t eventHubId = 1;
1933 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001934 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001936 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001937 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001938
1939 NotifyDeviceResetArgs resetArgs;
1940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001941 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001944 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001946 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001947 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001949 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001950 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001953 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001955 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001956 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001958 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001959 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001960}
1961
Garfield Tan1c7bc862020-01-28 13:24:04 -08001962TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1963 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001964 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001965 constexpr int32_t eventHubId = 1;
1966 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1967 // Must add at least one mapper or the device will be ignored!
1968 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001969 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001970 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1971
1972 NotifyDeviceResetArgs resetArgs;
1973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1974 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1975}
1976
Arthur Hungc23540e2018-11-29 20:42:11 +08001977TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001978 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001979 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001980 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001981 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001982 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1983 FakeInputMapper& mapper =
1984 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001985 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001986
1987 const uint8_t hdmi1 = 1;
1988
1989 // Associated touch screen with second display.
1990 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1991
1992 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001993 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001994 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001995 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001996 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001997 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001998 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001999 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002000 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002001 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002002
2003 // Add the device, and make sure all of the callbacks are triggered.
2004 // The device is added after the input port associations are processed since
2005 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002009 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002010
Arthur Hung2c9a3342019-07-23 14:18:59 +08002011 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002012 ASSERT_EQ(deviceId, device->getId());
2013 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2014 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002015
2016 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002017 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002018 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002019 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002020}
2021
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002022TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2023 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002024 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002025 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2026 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2027 // Must add at least one mapper or the device will be ignored!
2028 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2029 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2030 mReader->pushNextDevice(device);
2031 mReader->pushNextDevice(device);
2032 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2034
2035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2036
2037 NotifyDeviceResetArgs resetArgs;
2038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2039 ASSERT_EQ(deviceId, resetArgs.deviceId);
2040 ASSERT_TRUE(device->isEnabled());
2041 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2043
2044 disableDevice(deviceId);
2045 mReader->loopOnce();
2046
2047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2048 ASSERT_EQ(deviceId, resetArgs.deviceId);
2049 ASSERT_FALSE(device->isEnabled());
2050 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2051 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2052
2053 enableDevice(deviceId);
2054 mReader->loopOnce();
2055
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2057 ASSERT_EQ(deviceId, resetArgs.deviceId);
2058 ASSERT_TRUE(device->isEnabled());
2059 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2060 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2061}
2062
2063TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2064 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002065 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002066 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2067 // Add two subdevices to device
2068 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2069 FakeInputMapper& mapperDevice1 =
2070 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2071 FakeInputMapper& mapperDevice2 =
2072 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2073 mReader->pushNextDevice(device);
2074 mReader->pushNextDevice(device);
2075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2077
2078 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2079 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2080
2081 ASSERT_EQ(AKEY_STATE_DOWN,
2082 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2083 ASSERT_EQ(AKEY_STATE_DOWN,
2084 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2085 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2086 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2087}
2088
Prabir Pradhan7e186182020-11-10 13:56:45 -08002089TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2090 NotifyPointerCaptureChangedArgs args;
2091
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002093 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2094 mReader->loopOnce();
2095 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002096 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2097 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002098
2099 mFakePolicy->setPointerCapture(false);
2100 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2101 mReader->loopOnce();
2102 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002105 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002106 // does not change.
2107 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2108 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002109 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002110}
2111
Chris Ye87143712020-11-10 05:05:58 +00002112class FakeVibratorInputMapper : public FakeInputMapper {
2113public:
2114 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2115 : FakeInputMapper(deviceContext, sources) {}
2116
2117 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2118};
2119
2120TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2121 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002122 ftl::Flags<InputDeviceClass> deviceClass =
2123 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002124 constexpr int32_t eventHubId = 1;
2125 const char* DEVICE_LOCATION = "BLUETOOTH";
2126 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2127 FakeVibratorInputMapper& mapper =
2128 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2129 mReader->pushNextDevice(device);
2130
2131 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2132 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2133
2134 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2135 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2136}
2137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002139
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002140class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002141public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002142 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002143
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002144 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002145
Andy Chenf9f1a022022-08-29 20:07:10 -04002146 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2147
Chris Yee2b1e5c2021-03-10 22:45:12 -08002148 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2149
2150 void dump(std::string& dump) override {}
2151
2152 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2153 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002154 }
2155
Chris Yee2b1e5c2021-03-10 22:45:12 -08002156 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2157 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002158 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002159
2160 bool setLightColor(int32_t lightId, int32_t color) override {
2161 getDeviceContext().setLightBrightness(lightId, color >> 24);
2162 return true;
2163 }
2164
2165 std::optional<int32_t> getLightColor(int32_t lightId) override {
2166 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2167 if (!result.has_value()) {
2168 return std::nullopt;
2169 }
2170 return result.value() << 24;
2171 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002172
2173 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2174
2175 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2176
2177private:
2178 InputDeviceContext& mDeviceContext;
2179 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2180 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002181 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002182};
2183
Chris Yee2b1e5c2021-03-10 22:45:12 -08002184TEST_F(InputReaderTest, BatteryGetCapacity) {
2185 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002186 ftl::Flags<InputDeviceClass> deviceClass =
2187 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188 constexpr int32_t eventHubId = 1;
2189 const char* DEVICE_LOCATION = "BLUETOOTH";
2190 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002191 FakePeripheralController& controller =
2192 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002193 mReader->pushNextDevice(device);
2194
2195 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2196
2197 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2198 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2199}
2200
2201TEST_F(InputReaderTest, BatteryGetStatus) {
2202 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002203 ftl::Flags<InputDeviceClass> deviceClass =
2204 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205 constexpr int32_t eventHubId = 1;
2206 const char* DEVICE_LOCATION = "BLUETOOTH";
2207 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002208 FakePeripheralController& controller =
2209 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002210 mReader->pushNextDevice(device);
2211
2212 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2213
2214 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2215 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2216}
2217
Chris Ye3fdbfef2021-01-06 18:45:18 -08002218TEST_F(InputReaderTest, LightGetColor) {
2219 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002220 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002221 constexpr int32_t eventHubId = 1;
2222 const char* DEVICE_LOCATION = "BLUETOOTH";
2223 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002224 FakePeripheralController& controller =
2225 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002226 mReader->pushNextDevice(device);
2227 RawLightInfo info = {.id = 1,
2228 .name = "Mono",
2229 .maxBrightness = 255,
2230 .flags = InputLightClass::BRIGHTNESS,
2231 .path = ""};
2232 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2233 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2234
2235 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002236
Chris Yee2b1e5c2021-03-10 22:45:12 -08002237 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2238 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002239 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2240 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2241}
2242
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002243// --- InputReaderIntegrationTest ---
2244
2245// These tests create and interact with the InputReader only through its interface.
2246// The InputReader is started during SetUp(), which starts its processing in its own
2247// thread. The tests use linux uinput to emulate input devices.
2248// NOTE: Interacting with the physical device while these tests are running may cause
2249// the tests to fail.
2250class InputReaderIntegrationTest : public testing::Test {
2251protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002252 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002254 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002255
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002256 std::shared_ptr<FakePointerController> mFakePointerController;
2257
Chris Yea52ade12020-08-27 16:49:20 -07002258 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002259 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002260 mFakePointerController = std::make_shared<FakePointerController>();
2261 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002262 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2263 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002264
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002265 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2266 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002267 ASSERT_EQ(mReader->start(), OK);
2268
2269 // Since this test is run on a real device, all the input devices connected
2270 // to the test device will show up in mReader. We wait for those input devices to
2271 // show up before beginning the tests.
2272 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2273 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2274 }
2275
Chris Yea52ade12020-08-27 16:49:20 -07002276 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002277 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002278 mReader.reset();
2279 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002280 mFakePolicy.clear();
2281 }
2282};
2283
2284TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2285 // An invalid input device that is only used for this test.
2286 class InvalidUinputDevice : public UinputDevice {
2287 public:
2288 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2289
2290 private:
2291 void configureDevice(int fd, uinput_user_dev* device) override {}
2292 };
2293
2294 const size_t numDevices = mFakePolicy->getInputDevices().size();
2295
2296 // UinputDevice does not set any event or key bits, so InputReader should not
2297 // consider it as a valid device.
2298 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2301 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2302
2303 invalidDevice.reset();
2304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2306 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2307}
2308
2309TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2310 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2311
2312 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2313 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2314 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2315 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2316
2317 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002318 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002319 const auto& it =
2320 std::find_if(inputDevices.begin(), inputDevices.end(),
2321 [&keyboard](const InputDeviceInfo& info) {
2322 return info.getIdentifier().name == keyboard->getName();
2323 });
2324
2325 ASSERT_NE(it, inputDevices.end());
2326 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2327 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2328 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002329
2330 keyboard.reset();
2331 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2332 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2333 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2334}
2335
2336TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2337 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2338 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2339
2340 NotifyConfigurationChangedArgs configChangedArgs;
2341 ASSERT_NO_FATAL_FAILURE(
2342 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002343 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002344 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2345
2346 NotifyKeyArgs keyArgs;
2347 keyboard->pressAndReleaseHomeKey();
2348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002350 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002351 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002353 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002354 prevTimestamp = keyArgs.eventTime;
2355
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2357 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002358 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002360 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002361}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002362
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002363/**
2364 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2365 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2366 * are passed to the listener.
2367 */
2368static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2369TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2370 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2371 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2372 NotifyKeyArgs keyArgs;
2373
2374 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
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_DOWN, keyArgs.scanCode);
2378
2379 controller->pressAndReleaseKey(BTN_GEAR_UP);
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2381 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2382 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2383}
2384
Arthur Hungaab25622020-01-16 11:22:11 +08002385// --- TouchProcessTest ---
2386class TouchIntegrationTest : public InputReaderIntegrationTest {
2387protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002388 const std::string UNIQUE_ID = "local:0";
2389
Chris Yea52ade12020-08-27 16:49:20 -07002390 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002391 InputReaderIntegrationTest::SetUp();
2392 // At least add an internal display.
2393 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2394 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002395 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002396
2397 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2398 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2400 }
2401
2402 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2403 int32_t orientation, const std::string& uniqueId,
2404 std::optional<uint8_t> physicalPort,
2405 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002406 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2407 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002408 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2409 }
2410
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002411 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2412 NotifyMotionArgs args;
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2414 EXPECT_EQ(action, args.action);
2415 ASSERT_EQ(points.size(), args.pointerCount);
2416 for (size_t i = 0; i < args.pointerCount; i++) {
2417 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2418 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2419 }
2420 }
2421
Arthur Hungaab25622020-01-16 11:22:11 +08002422 std::unique_ptr<UinputTouchScreen> mDevice;
2423};
2424
2425TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2426 NotifyMotionArgs args;
2427 const Point centerPoint = mDevice->getCenterPoint();
2428
2429 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002430 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002431 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002432 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2435
2436 // ACTION_MOVE
2437 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002438 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2441
2442 // ACTION_UP
2443 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002444 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2447}
2448
2449TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2450 NotifyMotionArgs args;
2451 const Point centerPoint = mDevice->getCenterPoint();
2452
2453 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002454 mDevice->sendSlot(FIRST_SLOT);
2455 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002456 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002457 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002458 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2460
2461 // ACTION_POINTER_DOWN (Second slot)
2462 const Point secondPoint = centerPoint + Point(100, 100);
2463 mDevice->sendSlot(SECOND_SLOT);
2464 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002465 mDevice->sendDown(secondPoint);
2466 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002468 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002469
2470 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471 mDevice->sendMove(secondPoint + Point(1, 1));
2472 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2475
2476 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002477 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002478 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002480 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002481
2482 // ACTION_UP
2483 mDevice->sendSlot(FIRST_SLOT);
2484 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2488}
2489
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002490/**
2491 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2492 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2493 * data?
2494 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2495 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2496 * for Pointer 0 only is generated after.
2497 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2498 * events, we will not miss any information.
2499 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2500 * event generated afterwards that contains the newest movement of pointer 0.
2501 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2502 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2503 * losing information about non-palm pointers.
2504 */
2505TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2506 NotifyMotionArgs args;
2507 const Point centerPoint = mDevice->getCenterPoint();
2508
2509 // ACTION_DOWN
2510 mDevice->sendSlot(FIRST_SLOT);
2511 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2512 mDevice->sendDown(centerPoint);
2513 mDevice->sendSync();
2514 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2515
2516 // ACTION_POINTER_DOWN (Second slot)
2517 const Point secondPoint = centerPoint + Point(100, 100);
2518 mDevice->sendSlot(SECOND_SLOT);
2519 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2520 mDevice->sendDown(secondPoint);
2521 mDevice->sendSync();
2522 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2523
2524 // ACTION_MOVE (First slot)
2525 mDevice->sendSlot(FIRST_SLOT);
2526 mDevice->sendMove(centerPoint + Point(5, 5));
2527 // ACTION_POINTER_UP (Second slot)
2528 mDevice->sendSlot(SECOND_SLOT);
2529 mDevice->sendPointerUp();
2530 // Send a single sync for the above 2 pointer updates
2531 mDevice->sendSync();
2532
2533 // First, we should get POINTER_UP for the second pointer
2534 assertReceivedMotion(ACTION_POINTER_1_UP,
2535 {/*first pointer */ centerPoint + Point(5, 5),
2536 /*second pointer*/ secondPoint});
2537
2538 // Next, the MOVE event for the first pointer
2539 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2540}
2541
2542/**
2543 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2544 * move, and then it will go up, all in the same frame.
2545 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2546 * gets sent to the listener.
2547 */
2548TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2549 NotifyMotionArgs args;
2550 const Point centerPoint = mDevice->getCenterPoint();
2551
2552 // ACTION_DOWN
2553 mDevice->sendSlot(FIRST_SLOT);
2554 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2555 mDevice->sendDown(centerPoint);
2556 mDevice->sendSync();
2557 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2558
2559 // ACTION_POINTER_DOWN (Second slot)
2560 const Point secondPoint = centerPoint + Point(100, 100);
2561 mDevice->sendSlot(SECOND_SLOT);
2562 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2563 mDevice->sendDown(secondPoint);
2564 mDevice->sendSync();
2565 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2566
2567 // ACTION_MOVE (First slot)
2568 mDevice->sendSlot(FIRST_SLOT);
2569 mDevice->sendMove(centerPoint + Point(5, 5));
2570 // ACTION_POINTER_UP (Second slot)
2571 mDevice->sendSlot(SECOND_SLOT);
2572 mDevice->sendMove(secondPoint + Point(6, 6));
2573 mDevice->sendPointerUp();
2574 // Send a single sync for the above 2 pointer updates
2575 mDevice->sendSync();
2576
2577 // First, we should get POINTER_UP for the second pointer
2578 // The movement of the second pointer during the liftoff frame is ignored.
2579 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2580 assertReceivedMotion(ACTION_POINTER_1_UP,
2581 {/*first pointer */ centerPoint + Point(5, 5),
2582 /*second pointer*/ secondPoint});
2583
2584 // Next, the MOVE event for the first pointer
2585 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2586}
2587
Arthur Hungaab25622020-01-16 11:22:11 +08002588TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2589 NotifyMotionArgs args;
2590 const Point centerPoint = mDevice->getCenterPoint();
2591
2592 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002593 mDevice->sendSlot(FIRST_SLOT);
2594 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002595 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002596 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002597 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2598 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2599
arthurhungcc7f9802020-04-30 17:55:40 +08002600 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002601 const Point secondPoint = centerPoint + Point(100, 100);
2602 mDevice->sendSlot(SECOND_SLOT);
2603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2604 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002605 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002607 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002608
arthurhungcc7f9802020-04-30 17:55:40 +08002609 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002610 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002611 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002612 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2614
arthurhungcc7f9802020-04-30 17:55:40 +08002615 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2616 // a palm event.
2617 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002618 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002620 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002621 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002622 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002623
arthurhungcc7f9802020-04-30 17:55:40 +08002624 // Send up to second slot, expect first slot send moving.
2625 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002626 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002627 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002629
arthurhungcc7f9802020-04-30 17:55:40 +08002630 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002631 mDevice->sendSlot(FIRST_SLOT);
2632 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002633 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002634
arthurhungcc7f9802020-04-30 17:55:40 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002637}
2638
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640class InputDeviceTest : public testing::Test {
2641protected:
2642 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002643 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 static const int32_t DEVICE_ID;
2645 static const int32_t DEVICE_GENERATION;
2646 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002647 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002648 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002650 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002652 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002653 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002654 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655
Chris Yea52ade12020-08-27 16:49:20 -07002656 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002657 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002658 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002661 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 InputDeviceIdentifier identifier;
2663 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002664 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002666 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002668 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002669 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 }
2671
Chris Yea52ade12020-08-27 16:49:20 -07002672 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002673 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675 }
2676};
2677
2678const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002679const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2682const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002683const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002684 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002685const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686
2687TEST_F(InputDeviceTest, ImmutableProperties) {
2688 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002689 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002690 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691}
2692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002693TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2694 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002695}
2696
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2698 // Configuration.
2699 InputReaderConfiguration config;
2700 mDevice->configure(ARBITRARY_TIME, &config, 0);
2701
2702 // Reset.
2703 mDevice->reset(ARBITRARY_TIME);
2704
2705 NotifyDeviceResetArgs resetArgs;
2706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2707 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2708 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2709
2710 // Metadata.
2711 ASSERT_TRUE(mDevice->isIgnored());
2712 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2713
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002714 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002716 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2718 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2719
2720 // State queries.
2721 ASSERT_EQ(0, mDevice->getMetaState());
2722
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown key code state.";
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2726 << "Ignored device should return unknown scan code state.";
2727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2728 << "Ignored device should return unknown switch state.";
2729
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002730 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002732 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733 << "Ignored device should never mark any key codes.";
2734 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2735 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2736}
2737
2738TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2739 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002740 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002742 FakeInputMapper& mapper1 =
2743 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002744 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2745 mapper1.setMetaState(AMETA_ALT_ON);
2746 mapper1.addSupportedKeyCode(AKEYCODE_A);
2747 mapper1.addSupportedKeyCode(AKEYCODE_B);
2748 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2749 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2750 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2751 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2752 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002754 FakeInputMapper& mapper2 =
2755 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002756 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757
2758 InputReaderConfiguration config;
2759 mDevice->configure(ARBITRARY_TIME, &config, 0);
2760
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002761 std::string propertyValue;
2762 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002764 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2767 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
2769 // Reset
2770 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002771 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2772 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002773
2774 NotifyDeviceResetArgs resetArgs;
2775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2776 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2777 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2778
2779 // Metadata.
2780 ASSERT_FALSE(mDevice->isIgnored());
2781 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2782
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002783 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002785 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002786 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2787 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2788
2789 // State queries.
2790 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2791 << "Should query mappers and combine meta states.";
2792
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown key code state when source not supported.";
2795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2796 << "Should return unknown scan code state when source not supported.";
2797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2798 << "Should return unknown switch state when source not supported.";
2799
2800 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2801 << "Should query mapper when source is supported.";
2802 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2803 << "Should query mapper when source is supported.";
2804 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2805 << "Should query mapper when source is supported.";
2806
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002809 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810 << "Should do nothing when source is unsupported.";
2811 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2812 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2813 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2814 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2815
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002816 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002817 << "Should query mapper when source is supported.";
2818 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2819 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2820 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2821 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2822
2823 // Event handling.
2824 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002825 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002826 mDevice->process(&event, 1);
2827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002828 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2829 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830}
2831
Arthur Hung2c9a3342019-07-23 14:18:59 +08002832// A single input device is associated with a specific display. Check that:
2833// 1. Device is disabled if the viewport corresponding to the associated display is not found
2834// 2. Device is disabled when setEnabled API is called
2835TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002836 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002837
2838 // First Configuration.
2839 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2840
2841 // Device should be enabled by default.
2842 ASSERT_TRUE(mDevice->isEnabled());
2843
2844 // Prepare associated info.
2845 constexpr uint8_t hdmi = 1;
2846 const std::string UNIQUE_ID = "local:1";
2847
2848 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2849 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2850 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2851 // Device should be disabled because it is associated with a specific display via
2852 // input port <-> display port association, but the corresponding display is not found
2853 ASSERT_FALSE(mDevice->isEnabled());
2854
2855 // Prepare displays.
2856 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002857 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2858 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002859 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2860 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2861 ASSERT_TRUE(mDevice->isEnabled());
2862
2863 // Device should be disabled after set disable.
2864 mFakePolicy->addDisabledDevice(mDevice->getId());
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2867 ASSERT_FALSE(mDevice->isEnabled());
2868
2869 // Device should still be disabled even found the associated display.
2870 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2871 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2872 ASSERT_FALSE(mDevice->isEnabled());
2873}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002874
Christine Franks1ba71cc2021-04-07 14:37:42 -07002875TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2876 // Device should be enabled by default.
2877 mFakePolicy->clearViewports();
2878 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2879 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2880 ASSERT_TRUE(mDevice->isEnabled());
2881
2882 // Device should be disabled because it is associated with a specific display, but the
2883 // corresponding display is not found.
2884 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002885 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889
2890 // Device should be enabled when a display is found.
2891 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2892 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2893 NO_PORT, ViewportType::INTERNAL);
2894 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2895 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2896 ASSERT_TRUE(mDevice->isEnabled());
2897
2898 // Device should be disabled after set disable.
2899 mFakePolicy->addDisabledDevice(mDevice->getId());
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2902 ASSERT_FALSE(mDevice->isEnabled());
2903
2904 // Device should still be disabled even found the associated display.
2905 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2907 ASSERT_FALSE(mDevice->isEnabled());
2908}
2909
Christine Franks2a2293c2022-01-18 11:51:16 -08002910TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2911 mFakePolicy->clearViewports();
2912 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2913 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2914
2915 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2916 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2917 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2918 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2919 NO_PORT, ViewportType::INTERNAL);
2920 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2921 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2922 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2923}
2924
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925// --- InputMapperTest ---
2926
2927class InputMapperTest : public testing::Test {
2928protected:
2929 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002930 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 static const int32_t DEVICE_ID;
2932 static const int32_t DEVICE_GENERATION;
2933 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002934 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002935 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002937 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002939 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 std::unique_ptr<InstrumentedInputReader> mReader;
2941 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002943 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002944 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002945 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002946 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002948 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002949 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002950 // Consume the device reset notification generated when adding a new device.
2951 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 }
2953
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002954 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002955 SetUp(DEVICE_CLASSES);
2956 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002957
Chris Yea52ade12020-08-27 16:49:20 -07002958 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002959 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 }
2962
2963 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002964 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965 }
2966
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002967 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002968 if (!changes ||
2969 (changes &
2970 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2971 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002972 mReader->requestRefreshConfiguration(changes);
2973 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002974 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002975 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002976 // Loop the reader to flush the input listener queue.
2977 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002978 }
2979
arthurhungdcef2dc2020-08-11 14:47:50 +08002980 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2981 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002982 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002983 InputDeviceIdentifier identifier;
2984 identifier.name = name;
2985 identifier.location = location;
2986 std::shared_ptr<InputDevice> device =
2987 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2988 identifier);
2989 mReader->pushNextDevice(device);
2990 mFakeEventHub->addDevice(eventHubId, name, classes);
2991 mReader->loopOnce();
2992 return device;
2993 }
2994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002995 template <class T, typename... Args>
2996 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002997 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002998 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003000 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003001 // Loop the reader to flush the input listener queue.
3002 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003003 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 }
3005
3006 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003007 int32_t orientation, const std::string& uniqueId,
3008 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003009 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3010 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003011 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3012 }
3013
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003014 void clearViewports() {
3015 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 }
3017
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003018 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3019 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 RawEvent event;
3021 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003022 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003023 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 event.type = type;
3025 event.code = code;
3026 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003027 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003028 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003029 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 }
3031
3032 static void assertMotionRange(const InputDeviceInfo& info,
3033 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3034 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003035 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3037 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3038 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3039 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3040 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3041 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3042 }
3043
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003044 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3045 float size, float touchMajor, float touchMinor, float toolMajor,
3046 float toolMinor, float orientation, float distance,
3047 float scaledAxisEpsilon = 1.f) {
3048 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3049 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3051 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003052 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3053 scaledAxisEpsilon);
3054 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3055 scaledAxisEpsilon);
3056 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3057 scaledAxisEpsilon);
3058 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3059 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3061 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3062 }
3063
Michael Wright17db18e2020-06-26 20:51:44 +01003064 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003066 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 ASSERT_NEAR(x, actualX, 1);
3068 ASSERT_NEAR(y, actualY, 1);
3069 }
3070};
3071
3072const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003073const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003074const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3076const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003077const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3078 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003079const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
3081// --- SwitchInputMapperTest ---
3082
3083class SwitchInputMapperTest : public InputMapperTest {
3084protected:
3085};
3086
3087TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003090 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091}
3092
3093TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003094 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003096 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003097 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003099 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101}
3102
3103TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003104 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 NotifySwitchArgs args;
3112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003114 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3115 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 args.switchMask);
3117 ASSERT_EQ(uint32_t(0), args.policyFlags);
3118}
3119
Chris Ye87143712020-11-10 05:05:58 +00003120// --- VibratorInputMapperTest ---
3121class VibratorInputMapperTest : public InputMapperTest {
3122protected:
3123 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3124};
3125
3126TEST_F(VibratorInputMapperTest, GetSources) {
3127 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3128
3129 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3130}
3131
3132TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3133 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3134
3135 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3136}
3137
3138TEST_F(VibratorInputMapperTest, Vibrate) {
3139 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003140 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003141 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3142
3143 VibrationElement pattern(2);
3144 VibrationSequence sequence(2);
3145 pattern.duration = std::chrono::milliseconds(200);
3146 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3147 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3148 sequence.addElement(pattern);
3149 pattern.duration = std::chrono::milliseconds(500);
3150 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3151 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3152 sequence.addElement(pattern);
3153
3154 std::vector<int64_t> timings = {0, 1};
3155 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3156
3157 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003158 // Start vibrating
3159 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003160 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003161 // Verify vibrator state listener was notified.
3162 mReader->loopOnce();
3163 NotifyVibratorStateArgs args;
3164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3165 ASSERT_EQ(DEVICE_ID, args.deviceId);
3166 ASSERT_TRUE(args.isOn);
3167 // Stop vibrating
3168 mapper.cancelVibrate(VIBRATION_TOKEN);
3169 ASSERT_FALSE(mapper.isVibrating());
3170 // Verify vibrator state listener was notified.
3171 mReader->loopOnce();
3172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3173 ASSERT_EQ(DEVICE_ID, args.deviceId);
3174 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003175}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003176
Chris Yef59a2f42020-10-16 12:55:26 -07003177// --- SensorInputMapperTest ---
3178
3179class SensorInputMapperTest : public InputMapperTest {
3180protected:
3181 static const int32_t ACCEL_RAW_MIN;
3182 static const int32_t ACCEL_RAW_MAX;
3183 static const int32_t ACCEL_RAW_FUZZ;
3184 static const int32_t ACCEL_RAW_FLAT;
3185 static const int32_t ACCEL_RAW_RESOLUTION;
3186
3187 static const int32_t GYRO_RAW_MIN;
3188 static const int32_t GYRO_RAW_MAX;
3189 static const int32_t GYRO_RAW_FUZZ;
3190 static const int32_t GYRO_RAW_FLAT;
3191 static const int32_t GYRO_RAW_RESOLUTION;
3192
3193 static const float GRAVITY_MS2_UNIT;
3194 static const float DEGREE_RADIAN_UNIT;
3195
3196 void prepareAccelAxes();
3197 void prepareGyroAxes();
3198 void setAccelProperties();
3199 void setGyroProperties();
3200 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3201};
3202
3203const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3204const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3205const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3206const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3207const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3208
3209const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3210const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3211const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3212const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3213const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3214
3215const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3216const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3217
3218void SensorInputMapperTest::prepareAccelAxes() {
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3220 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3222 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3224 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3225}
3226
3227void SensorInputMapperTest::prepareGyroAxes() {
3228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3229 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3231 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3232 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3233 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3234}
3235
3236void SensorInputMapperTest::setAccelProperties() {
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3238 /* sensorDataIndex */ 0);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3240 /* sensorDataIndex */ 1);
3241 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3242 /* sensorDataIndex */ 2);
3243 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3244 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3245 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3246 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3247 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3248}
3249
3250void SensorInputMapperTest::setGyroProperties() {
3251 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3252 /* sensorDataIndex */ 0);
3253 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3254 /* sensorDataIndex */ 1);
3255 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3256 /* sensorDataIndex */ 2);
3257 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3258 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3259 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3260 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3261 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3262}
3263
3264TEST_F(SensorInputMapperTest, GetSources) {
3265 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3266
3267 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3268}
3269
3270TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3271 setAccelProperties();
3272 prepareAccelAxes();
3273 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3274
3275 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3276 std::chrono::microseconds(10000),
3277 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003278 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003284
3285 NotifySensorArgs args;
3286 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3287 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3288 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3289
3290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3291 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3292 ASSERT_EQ(args.deviceId, DEVICE_ID);
3293 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3294 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3295 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3296 ASSERT_EQ(args.values, values);
3297 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3298}
3299
3300TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3301 setGyroProperties();
3302 prepareGyroAxes();
3303 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3304
3305 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3306 std::chrono::microseconds(10000),
3307 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003308 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003314
3315 NotifySensorArgs args;
3316 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3317 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3318 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3319
3320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3321 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3322 ASSERT_EQ(args.deviceId, DEVICE_ID);
3323 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3324 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3325 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3326 ASSERT_EQ(args.values, values);
3327 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3328}
3329
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330// --- KeyboardInputMapperTest ---
3331
3332class KeyboardInputMapperTest : public InputMapperTest {
3333protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334 const std::string UNIQUE_ID = "local:0";
3335
3336 void prepareDisplay(int32_t orientation);
3337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 int32_t originalKeyCode, int32_t rotatedKeyCode,
3340 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341};
3342
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003343/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3344 * orientation.
3345 */
3346void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003347 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3348 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003349}
3350
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 int32_t originalScanCode, int32_t originalKeyCode,
3353 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 NotifyKeyArgs args;
3355
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3358 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3359 ASSERT_EQ(originalScanCode, args.scanCode);
3360 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3365 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3366 ASSERT_EQ(originalScanCode, args.scanCode);
3367 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003368 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369}
3370
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
3373 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003376 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377}
3378
3379TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3380 const int32_t USAGE_A = 0x070004;
3381 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003382 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3383 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003384 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3385 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3386 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003391 // Initial metastate is AMETA_NONE.
3392 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
3394 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 NotifyKeyArgs args;
3397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(DEVICE_ID, args.deviceId);
3399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3400 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3402 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3403 ASSERT_EQ(KEY_HOME, args.scanCode);
3404 ASSERT_EQ(AMETA_NONE, args.metaState);
3405 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3406 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3407 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3408
3409 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3412 ASSERT_EQ(DEVICE_ID, args.deviceId);
3413 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3414 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3415 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3416 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3417 ASSERT_EQ(KEY_HOME, args.scanCode);
3418 ASSERT_EQ(AMETA_NONE, args.metaState);
3419 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3420 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3421 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3422
3423 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(DEVICE_ID, args.deviceId);
3428 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3429 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3431 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3432 ASSERT_EQ(0, args.scanCode);
3433 ASSERT_EQ(AMETA_NONE, args.metaState);
3434 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3435 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3436 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3437
3438 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3440 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3442 ASSERT_EQ(DEVICE_ID, args.deviceId);
3443 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3444 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3446 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3447 ASSERT_EQ(0, args.scanCode);
3448 ASSERT_EQ(AMETA_NONE, args.metaState);
3449 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3450 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3451 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3452
3453 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3457 ASSERT_EQ(DEVICE_ID, args.deviceId);
3458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3459 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3460 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3461 ASSERT_EQ(0, args.keyCode);
3462 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3463 ASSERT_EQ(AMETA_NONE, args.metaState);
3464 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3465 ASSERT_EQ(0U, args.policyFlags);
3466 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3467
3468 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3470 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3472 ASSERT_EQ(DEVICE_ID, args.deviceId);
3473 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3474 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3475 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3476 ASSERT_EQ(0, args.keyCode);
3477 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3478 ASSERT_EQ(AMETA_NONE, args.metaState);
3479 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3480 ASSERT_EQ(0U, args.policyFlags);
3481 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3482}
3483
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003484/**
3485 * Ensure that the readTime is set to the time when the EV_KEY is received.
3486 */
3487TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3488 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3489
3490 KeyboardInputMapper& mapper =
3491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3493 NotifyKeyArgs args;
3494
3495 // Key down
3496 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3498 ASSERT_EQ(12, args.readTime);
3499
3500 // Key up
3501 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(15, args.readTime);
3504}
3505
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003509 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3510 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3511 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 KeyboardInputMapper& mapper =
3514 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3515 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
Arthur Hung95f68612022-04-07 14:08:22 +08003517 // Initial metastate is AMETA_NONE.
3518 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519
3520 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 NotifyKeyArgs args;
3523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003526 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
3528 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003529 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3531 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533
3534 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
3540 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003541 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3543 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003545 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546}
3547
3548TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 KeyboardInputMapper& mapper =
3555 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3556 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003558 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3560 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3562 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3564 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3565 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3566 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3567}
3568
3569TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3572 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574
Michael Wrightd02c5b62014-02-10 15:10:22 -08003575 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003576 KeyboardInputMapper& mapper =
3577 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3578 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003580 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_NO_FATAL_FAILURE(
3582 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3584 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3586 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3588 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592 ASSERT_NO_FATAL_FAILURE(
3593 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3595 AKEYCODE_DPAD_UP, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3597 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3599 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003601 clearViewports();
3602 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003603 ASSERT_NO_FATAL_FAILURE(
3604 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3606 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3608 AKEYCODE_DPAD_UP, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3610 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003612 clearViewports();
3613 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614 ASSERT_NO_FATAL_FAILURE(
3615 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3617 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3619 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3621 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622
3623 // Special case: if orientation changes while key is down, we still emit the same keycode
3624 // in the key up as we did in the key down.
3625 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 clearViewports();
3627 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3630 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3631 ASSERT_EQ(KEY_UP, args.scanCode);
3632 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3633
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003634 clearViewports();
3635 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3638 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3639 ASSERT_EQ(KEY_UP, args.scanCode);
3640 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3641}
3642
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003643TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3644 // If the keyboard is not orientation aware,
3645 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003646 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003648 KeyboardInputMapper& mapper =
3649 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3650 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 NotifyKeyArgs args;
3652
3653 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3659
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003660 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3665 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3666}
3667
3668TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3669 // If the keyboard is orientation aware,
3670 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003671 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003672
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 KeyboardInputMapper& mapper =
3675 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677 NotifyKeyArgs args;
3678
3679 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3680 // ^--- already checked by the previous test
3681
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003682 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003683 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(DISPLAY_ID, args.displayId);
3689
3690 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003691 clearViewports();
3692 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003693 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3698 ASSERT_EQ(newDisplayId, args.displayId);
3699}
3700
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 KeyboardInputMapper& mapper =
3703 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003706 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003710 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711}
3712
Philip Junker4af3b3d2021-12-14 10:36:55 +01003713TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3714 KeyboardInputMapper& mapper =
3715 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3716 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3717
3718 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3719 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3720 << "If a mapping is available, the result is equal to the mapping";
3721
3722 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3723 << "If no mapping is available, the result is the key location";
3724}
3725
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 KeyboardInputMapper& mapper =
3728 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3729 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736}
3737
3738TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003739 KeyboardInputMapper& mapper =
3740 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3741 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003746 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_TRUE(flags[0]);
3748 ASSERT_FALSE(flags[1]);
3749}
3750
3751TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003752 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3753 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3754 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 KeyboardInputMapper& mapper =
3760 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3761 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003762 // Initial metastate is AMETA_NONE.
3763 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3767 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3768 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
3770 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3775 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777
3778 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003781 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3782 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3783 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785
3786 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3790 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3791 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003792 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793
3794 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3798 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3799 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
3802 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3807 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003809
3810 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3814 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3815 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003816 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817}
3818
Chris Yea52ade12020-08-27 16:49:20 -07003819TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3822 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3823 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3824
3825 KeyboardInputMapper& mapper =
3826 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3827 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3828
Chris Yea52ade12020-08-27 16:49:20 -07003829 // Meta state should be AMETA_NONE after reset
3830 mapper.reset(ARBITRARY_TIME);
3831 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3832 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3833 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3834 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3835
3836 NotifyKeyArgs args;
3837 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3840 ASSERT_EQ(AMETA_NONE, args.metaState);
3841 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3842 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3843 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3844
3845 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003846 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3848 ASSERT_EQ(AMETA_NONE, args.metaState);
3849 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3850 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3851 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3852}
3853
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3855 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003856 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3857 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3858 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3859 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003860
3861 // keyboard 2.
3862 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003863 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003864 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003866 std::shared_ptr<InputDevice> device2 =
3867 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003868 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3872 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3873 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 KeyboardInputMapper& mapper =
3876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003878
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003880 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003881 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3883 device2->reset(ARBITRARY_TIME);
3884
3885 // Prepared displays and associated info.
3886 constexpr uint8_t hdmi1 = 0;
3887 constexpr uint8_t hdmi2 = 1;
3888 const std::string SECONDARY_UNIQUE_ID = "local:1";
3889
3890 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3891 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3892
3893 // No associated display viewport found, should disable the device.
3894 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3895 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3896 ASSERT_FALSE(device2->isEnabled());
3897
3898 // Prepare second display.
3899 constexpr int32_t newDisplayId = 2;
3900 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003901 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003903 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003904 // Default device will reconfigure above, need additional reconfiguration for another device.
3905 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3906 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3907
3908 // Device should be enabled after the associated display is found.
3909 ASSERT_TRUE(mDevice->isEnabled());
3910 ASSERT_TRUE(device2->isEnabled());
3911
3912 // Test pad key events
3913 ASSERT_NO_FATAL_FAILURE(
3914 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3916 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3918 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3920 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3921
3922 ASSERT_NO_FATAL_FAILURE(
3923 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3925 AKEYCODE_DPAD_RIGHT, newDisplayId));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3927 AKEYCODE_DPAD_DOWN, newDisplayId));
3928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3929 AKEYCODE_DPAD_LEFT, newDisplayId));
3930}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
arthurhungc903df12020-08-11 15:08:42 +08003932TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3933 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3934 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3935 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3937 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3938 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3939
3940 KeyboardInputMapper& mapper =
3941 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3942 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003943 // Initial metastate is AMETA_NONE.
3944 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003945
3946 // Initialization should have turned all of the lights off.
3947 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3948 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3949 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3950
3951 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003954 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3955 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3956
3957 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003960 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3961 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3962
3963 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003966 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3967 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3968
3969 mFakeEventHub->removeDevice(EVENTHUB_ID);
3970 mReader->loopOnce();
3971
3972 // keyboard 2 should default toggle keys.
3973 const std::string USB2 = "USB2";
3974 const std::string DEVICE_NAME2 = "KEYBOARD2";
3975 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3976 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3977 std::shared_ptr<InputDevice> device2 =
3978 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003979 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003980 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3981 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3982 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3983 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3984 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3985 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3986
arthurhung6fe95782020-10-05 22:41:16 +08003987 KeyboardInputMapper& mapper2 =
3988 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3989 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003990 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3991 device2->reset(ARBITRARY_TIME);
3992
3993 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3994 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3995 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003996 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3997 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003998}
3999
Arthur Hungcb40a002021-08-03 14:31:01 +00004000TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4004
4005 // Suppose we have two mappers. (DPAD + KEYBOARD)
4006 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4007 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4008 KeyboardInputMapper& mapper =
4009 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4010 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004011 // Initial metastate is AMETA_NONE.
4012 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004013
4014 mReader->toggleCapsLockState(DEVICE_ID);
4015 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4016}
4017
Arthur Hungfb3cc112022-04-13 07:39:50 +00004018TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4019 // keyboard 1.
4020 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4021 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4022 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4026
4027 KeyboardInputMapper& mapper1 =
4028 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4029 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4030
4031 // keyboard 2.
4032 const std::string USB2 = "USB2";
4033 const std::string DEVICE_NAME2 = "KEYBOARD2";
4034 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4035 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4036 std::shared_ptr<InputDevice> device2 =
4037 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4038 ftl::Flags<InputDeviceClass>(0));
4039 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4040 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4041 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4042 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4043 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4044 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4045
4046 KeyboardInputMapper& mapper2 =
4047 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4048 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4049 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4050 device2->reset(ARBITRARY_TIME);
4051
Arthur Hung95f68612022-04-07 14:08:22 +08004052 // Initial metastate is AMETA_NONE.
4053 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4054 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4055
4056 // Toggle num lock on and off.
4057 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4058 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004059 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4060 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4061 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4062
4063 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4064 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4065 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4066 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4067 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4068
4069 // Toggle caps lock on and off.
4070 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4071 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4072 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4073 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4074 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4075
4076 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4077 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4078 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4079 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4080 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4081
4082 // Toggle scroll lock on and off.
4083 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4084 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4085 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4086 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4087 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4088
4089 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4090 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4091 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4092 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4093 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4094}
4095
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004096// --- KeyboardInputMapperTest_ExternalDevice ---
4097
4098class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4099protected:
Chris Yea52ade12020-08-27 16:49:20 -07004100 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004101};
4102
4103TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004104 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4105 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004106
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4108 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4110 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112 KeyboardInputMapper& mapper =
4113 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4114 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 NotifyKeyArgs args;
4118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4119 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004121 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4123 ASSERT_EQ(uint32_t(0), args.policyFlags);
4124
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4127 ASSERT_EQ(uint32_t(0), args.policyFlags);
4128
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004129 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4131 ASSERT_EQ(uint32_t(0), args.policyFlags);
4132
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4135 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4136
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004137 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4139 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4140}
4141
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004142TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004143 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004144
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 KeyboardInputMapper& mapper =
4151 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 NotifyKeyArgs args;
4156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4158
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
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, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4165 ASSERT_EQ(uint32_t(0), args.policyFlags);
4166
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4169 ASSERT_EQ(uint32_t(0), args.policyFlags);
4170
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4173 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4174
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004175 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4177 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4178}
4179
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180// --- CursorInputMapperTest ---
4181
4182class CursorInputMapperTest : public InputMapperTest {
4183protected:
4184 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4185
Michael Wright17db18e2020-06-26 20:51:44 +01004186 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
Chris Yea52ade12020-08-27 16:49:20 -07004188 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 InputMapperTest::SetUp();
4190
Michael Wright17db18e2020-06-26 20:51:44 +01004191 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004192 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 }
4194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004195 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4196 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004197
4198 void prepareDisplay(int32_t orientation) {
4199 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004200 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004201 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4202 orientation, uniqueId, NO_PORT, viewportType);
4203 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204
4205 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4206 float pressure) {
4207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4208 0.0f, 0.0f, 0.0f, EPSILON));
4209 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210};
4211
4212const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004214void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4215 int32_t originalY, int32_t rotatedX,
4216 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 NotifyMotionArgs args;
4218
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004224 ASSERT_NO_FATAL_FAILURE(
4225 assertCursorPointerCoords(args.pointerCoords[0],
4226 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4227 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228}
4229
4230TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235}
4236
4237TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004241 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242}
4243
4244TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004246 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247
4248 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004249 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250
4251 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004252 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4253 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4255 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4256
4257 // When the bounds are set, then there should be a valid motion range.
4258 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4259
4260 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262
4263 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4264 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4265 1, 800 - 1, 0.0f, 0.0f));
4266 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4267 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4268 2, 480 - 1, 0.0f, 0.0f));
4269 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4270 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4271 0.0f, 1.0f, 0.0f, 0.0f));
4272}
4273
4274TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004276 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
4278 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004279 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
4281 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4282 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4283 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4284 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4285 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4286 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4287 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4288 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4289 0.0f, 1.0f, 0.0f, 0.0f));
4290}
4291
4292TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004294 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
arthurhungdcef2dc2020-08-11 14:47:50 +08004296 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
4298 NotifyMotionArgs args;
4299
4300 // Button press.
4301 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4305 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4306 ASSERT_EQ(DEVICE_ID, args.deviceId);
4307 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4308 ASSERT_EQ(uint32_t(0), args.policyFlags);
4309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4310 ASSERT_EQ(0, args.flags);
4311 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4312 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4313 ASSERT_EQ(0, args.edgeFlags);
4314 ASSERT_EQ(uint32_t(1), args.pointerCount);
4315 ASSERT_EQ(0, args.pointerProperties[0].id);
4316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004317 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4319 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4320 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4321
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4323 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4324 ASSERT_EQ(DEVICE_ID, args.deviceId);
4325 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4326 ASSERT_EQ(uint32_t(0), args.policyFlags);
4327 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4328 ASSERT_EQ(0, args.flags);
4329 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4331 ASSERT_EQ(0, args.edgeFlags);
4332 ASSERT_EQ(uint32_t(1), args.pointerCount);
4333 ASSERT_EQ(0, args.pointerProperties[0].id);
4334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004335 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004336 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4337 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4338 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4339
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4342 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4344 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4345 ASSERT_EQ(DEVICE_ID, args.deviceId);
4346 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4347 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4349 ASSERT_EQ(0, args.flags);
4350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4351 ASSERT_EQ(0, args.buttonState);
4352 ASSERT_EQ(0, args.edgeFlags);
4353 ASSERT_EQ(uint32_t(1), args.pointerCount);
4354 ASSERT_EQ(0, args.pointerProperties[0].id);
4355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4358 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4359 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4360
4361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4362 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4363 ASSERT_EQ(DEVICE_ID, args.deviceId);
4364 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4365 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4367 ASSERT_EQ(0, args.flags);
4368 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4369 ASSERT_EQ(0, args.buttonState);
4370 ASSERT_EQ(0, args.edgeFlags);
4371 ASSERT_EQ(uint32_t(1), args.pointerCount);
4372 ASSERT_EQ(0, args.pointerProperties[0].id);
4373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4376 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4377 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4378}
4379
4380TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004382 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383
4384 NotifyMotionArgs args;
4385
4386 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4392 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4393 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
4395 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4401 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402}
4403
4404TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
4408 NotifyMotionArgs args;
4409
4410 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4418 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004419 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004427
4428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004430 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431}
4432
4433TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004435 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436
4437 NotifyMotionArgs args;
4438
4439 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004446 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4447 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4448 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4451 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004452 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4453 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4454 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4463 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4464 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004471 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476}
4477
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004478TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004480 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4481 // need to be rotated.
4482 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004483 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004485 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4494}
4495
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004496TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004498 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4499 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004500 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004502 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4511
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004512 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004522 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4531
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004532 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541}
4542
4543TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546
4547 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4548 mFakePointerController->setPosition(100, 200);
4549 mFakePointerController->setButtonState(0);
4550
4551 NotifyMotionArgs motionArgs;
4552 NotifyKeyArgs keyArgs;
4553
4554 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4558 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4559 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4560 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(
4562 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4566 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4567 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004570
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_EQ(0, motionArgs.buttonState);
4576 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004577 ASSERT_NO_FATAL_FAILURE(
4578 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_EQ(0, motionArgs.buttonState);
4583 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004584 ASSERT_NO_FATAL_FAILURE(
4585 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004586
4587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004589 ASSERT_EQ(0, motionArgs.buttonState);
4590 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004591 ASSERT_NO_FATAL_FAILURE(
4592 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
4594 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4601 motionArgs.buttonState);
4602 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4603 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004604 ASSERT_NO_FATAL_FAILURE(
4605 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4609 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4611 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(
4613 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4618 motionArgs.buttonState);
4619 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4620 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004621 ASSERT_NO_FATAL_FAILURE(
4622 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4629 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004632
4633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4636 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004637 ASSERT_NO_FATAL_FAILURE(
4638 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4644 ASSERT_EQ(0, motionArgs.buttonState);
4645 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004650
4651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_EQ(0, motionArgs.buttonState);
4653 ASSERT_EQ(0, mFakePointerController->getButtonState());
4654 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004655 ASSERT_NO_FATAL_FAILURE(
4656 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004657
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4659 ASSERT_EQ(0, motionArgs.buttonState);
4660 ASSERT_EQ(0, mFakePointerController->getButtonState());
4661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004662 ASSERT_NO_FATAL_FAILURE(
4663 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664
4665 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
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->assertNotifyKeyWasCalled(&keyArgs));
4669 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4670 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4675 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004676 ASSERT_NO_FATAL_FAILURE(
4677 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004678
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4681 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4682 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004683 ASSERT_NO_FATAL_FAILURE(
4684 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690 ASSERT_EQ(0, motionArgs.buttonState);
4691 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004692 ASSERT_NO_FATAL_FAILURE(
4693 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_EQ(0, motionArgs.buttonState);
4698 ASSERT_EQ(0, mFakePointerController->getButtonState());
4699
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4703 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4704 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4705
4706 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4710 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4711 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4716 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(
4718 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004719
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4723 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004724 ASSERT_NO_FATAL_FAILURE(
4725 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_EQ(0, motionArgs.buttonState);
4732 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004733 ASSERT_NO_FATAL_FAILURE(
4734 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4737 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4738 ASSERT_EQ(0, motionArgs.buttonState);
4739 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004740 ASSERT_NO_FATAL_FAILURE(
4741 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004742
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4744 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4745 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4746
4747 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4751 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4752 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4757 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004758 ASSERT_NO_FATAL_FAILURE(
4759 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004760
4761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4763 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4764 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004765 ASSERT_NO_FATAL_FAILURE(
4766 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_EQ(0, motionArgs.buttonState);
4773 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004774 ASSERT_NO_FATAL_FAILURE(
4775 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004776
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4779 ASSERT_EQ(0, motionArgs.buttonState);
4780 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004781 ASSERT_NO_FATAL_FAILURE(
4782 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004783
Michael Wrightd02c5b62014-02-10 15:10:22 -08004784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4786 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4787
4788 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4792 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4793 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4798 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004799 ASSERT_NO_FATAL_FAILURE(
4800 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4804 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4805 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004806 ASSERT_NO_FATAL_FAILURE(
4807 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813 ASSERT_EQ(0, motionArgs.buttonState);
4814 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004815 ASSERT_NO_FATAL_FAILURE(
4816 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004817
4818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4819 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4820 ASSERT_EQ(0, motionArgs.buttonState);
4821 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004822 ASSERT_NO_FATAL_FAILURE(
4823 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004824
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4826 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4827 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4828}
4829
4830TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004832 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833
4834 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4835 mFakePointerController->setPosition(100, 200);
4836 mFakePointerController->setButtonState(0);
4837
4838 NotifyMotionArgs args;
4839
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004844 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4847 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 +01004848 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004849}
4850
4851TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004852 addConfigurationProperty("cursor.mode", "pointer");
4853 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004854 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004855
4856 NotifyDeviceResetArgs resetArgs;
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4858 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4859 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4860
4861 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4862 mFakePointerController->setPosition(100, 200);
4863 mFakePointerController->setButtonState(0);
4864
4865 NotifyMotionArgs args;
4866
4867 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4872 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4875 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 +01004876 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004877
4878 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4882 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4885 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4887 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4890 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4891
4892 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004893 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4894 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4896 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4899 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4901 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4904 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4905
4906 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4909 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4911 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4914 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 +01004915 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004916
4917 // Disable pointer capture and check that the device generation got bumped
4918 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004919 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004920 mFakePolicy->setPointerCapture(false);
4921 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004922 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004925 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4926
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4931 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4934 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 +01004935 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004938/**
4939 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4940 * pointer acceleration or speed processing should not be applied.
4941 */
4942TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4943 addConfigurationProperty("cursor.mode", "pointer");
4944 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4945 100.f /*high threshold*/, 10.f /*acceleration*/);
4946 mFakePolicy->setVelocityControlParams(testParams);
4947 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4948
4949 NotifyDeviceResetArgs resetArgs;
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4951 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4952 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4953
4954 NotifyMotionArgs args;
4955
4956 // Move and verify scale is applied.
4957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4961 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4962 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4963 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4964 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4965 ASSERT_GT(relX, 10);
4966 ASSERT_GT(relY, 20);
4967
4968 // Enable Pointer Capture
4969 mFakePolicy->setPointerCapture(true);
4970 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4971 NotifyPointerCaptureChangedArgs captureArgs;
4972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4973 ASSERT_TRUE(captureArgs.request.enable);
4974
4975 // Move and verify scale is not applied.
4976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4980 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4982 ASSERT_EQ(10, args.pointerCoords[0].getX());
4983 ASSERT_EQ(20, args.pointerCoords[0].getY());
4984}
4985
Prabir Pradhan208360b2022-06-24 18:37:04 +00004986TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4987 addConfigurationProperty("cursor.mode", "pointer");
4988 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4989
4990 NotifyDeviceResetArgs resetArgs;
4991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4992 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4993 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4994
4995 // Ensure the display is rotated.
4996 prepareDisplay(DISPLAY_ORIENTATION_90);
4997
4998 NotifyMotionArgs args;
4999
5000 // Verify that the coordinates are rotated.
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5005 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5007 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5008 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5009
5010 // Enable Pointer Capture.
5011 mFakePolicy->setPointerCapture(true);
5012 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5013 NotifyPointerCaptureChangedArgs captureArgs;
5014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5015 ASSERT_TRUE(captureArgs.request.enable);
5016
5017 // Move and verify rotation is not applied.
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5022 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5024 ASSERT_EQ(10, args.pointerCoords[0].getX());
5025 ASSERT_EQ(20, args.pointerCoords[0].getY());
5026}
5027
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005028TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005029 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005030
Garfield Tan888a6a42020-01-09 11:39:16 -08005031 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005032 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005033 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5034 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005035 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5036 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005037 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5038 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5039
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005040 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5041 mFakePointerController->setPosition(100, 200);
5042 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005043
5044 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5049 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5050 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5052 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 +01005053 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005054 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5055}
5056
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057// --- TouchInputMapperTest ---
5058
5059class TouchInputMapperTest : public InputMapperTest {
5060protected:
5061 static const int32_t RAW_X_MIN;
5062 static const int32_t RAW_X_MAX;
5063 static const int32_t RAW_Y_MIN;
5064 static const int32_t RAW_Y_MAX;
5065 static const int32_t RAW_TOUCH_MIN;
5066 static const int32_t RAW_TOUCH_MAX;
5067 static const int32_t RAW_TOOL_MIN;
5068 static const int32_t RAW_TOOL_MAX;
5069 static const int32_t RAW_PRESSURE_MIN;
5070 static const int32_t RAW_PRESSURE_MAX;
5071 static const int32_t RAW_ORIENTATION_MIN;
5072 static const int32_t RAW_ORIENTATION_MAX;
5073 static const int32_t RAW_DISTANCE_MIN;
5074 static const int32_t RAW_DISTANCE_MAX;
5075 static const int32_t RAW_TILT_MIN;
5076 static const int32_t RAW_TILT_MAX;
5077 static const int32_t RAW_ID_MIN;
5078 static const int32_t RAW_ID_MAX;
5079 static const int32_t RAW_SLOT_MIN;
5080 static const int32_t RAW_SLOT_MAX;
5081 static const float X_PRECISION;
5082 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005083 static const float X_PRECISION_VIRTUAL;
5084 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085
5086 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005087 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
5089 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5090
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005091 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005092 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005093
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094 enum Axes {
5095 POSITION = 1 << 0,
5096 TOUCH = 1 << 1,
5097 TOOL = 1 << 2,
5098 PRESSURE = 1 << 3,
5099 ORIENTATION = 1 << 4,
5100 MINOR = 1 << 5,
5101 ID = 1 << 6,
5102 DISTANCE = 1 << 7,
5103 TILT = 1 << 8,
5104 SLOT = 1 << 9,
5105 TOOL_TYPE = 1 << 10,
5106 };
5107
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005108 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5109 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005110 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005112 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 int32_t toRawX(float displayX);
5114 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005115 int32_t toRotatedRawX(float displayX);
5116 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005117 float toCookedX(float rawX, float rawY);
5118 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005120 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005122 float toDisplayY(int32_t rawY, int32_t displayHeight);
5123
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124};
5125
5126const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5127const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5128const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5129const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5130const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5132const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005134const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5135const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5137const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5138const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5139const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5140const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5141const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5142const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5143const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5144const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5145const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5146const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5147const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005148const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5149 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5150const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5151 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005152const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5153 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154
5155const float TouchInputMapperTest::GEOMETRIC_SCALE =
5156 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5157 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5158
5159const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5160 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5161 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5162};
5163
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005164void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005165 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5166 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005167}
5168
5169void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5170 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5171 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172}
5173
Santos Cordonfa5cf462017-04-05 10:37:00 -07005174void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005175 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5176 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5177 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005178}
5179
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005181 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5182 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5183 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5184 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185}
5186
Jason Gerecke489fda82012-09-07 17:19:40 -07005187void TouchInputMapperTest::prepareLocationCalibration() {
5188 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5189}
5190
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191int32_t TouchInputMapperTest::toRawX(float displayX) {
5192 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5193}
5194
5195int32_t TouchInputMapperTest::toRawY(float displayY) {
5196 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5197}
5198
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005199int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5200 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5201}
5202
5203int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5204 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5205}
5206
Jason Gerecke489fda82012-09-07 17:19:40 -07005207float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5208 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5209 return rawX;
5210}
5211
5212float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5213 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5214 return rawY;
5215}
5216
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005218 return toDisplayX(rawX, DISPLAY_WIDTH);
5219}
5220
5221float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5222 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
5225float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005226 return toDisplayY(rawY, DISPLAY_HEIGHT);
5227}
5228
5229float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5230 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231}
5232
5233
5234// --- SingleTouchInputMapperTest ---
5235
5236class SingleTouchInputMapperTest : public TouchInputMapperTest {
5237protected:
5238 void prepareButtons();
5239 void prepareAxes(int axes);
5240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5242 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5243 void processUp(SingleTouchInputMapper& mappery);
5244 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5245 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5246 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5247 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5248 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5249 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250};
5251
5252void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005253 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254}
5255
5256void SingleTouchInputMapperTest::prepareAxes(int axes) {
5257 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 }
5261 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5263 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 }
5265 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5267 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 }
5269 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5271 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272 }
5273 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005274 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5275 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276 }
5277}
5278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005279void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283}
5284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005285void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288}
5289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292}
5293
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005294void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296}
5297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5299 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005301}
5302
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005303void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305}
5306
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005307void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5308 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5314 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316}
5317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005318void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320}
5321
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 prepareButtons();
5324 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328}
5329
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 prepareButtons();
5332 prepareAxes(POSITION);
5333 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005334 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337}
5338
5339TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 addConfigurationProperty("touch.deviceType", "touchScreen");
5341 prepareDisplay(DISPLAY_ORIENTATION_0);
5342 prepareButtons();
5343 prepareAxes(POSITION);
5344 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
5347 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005348 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
5350 // Virtual key is down.
5351 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5352 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5353 processDown(mapper, x, y);
5354 processSync(mapper);
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358
5359 // Virtual key is up.
5360 processUp(mapper);
5361 processSync(mapper);
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365}
5366
5367TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368 addConfigurationProperty("touch.deviceType", "touchScreen");
5369 prepareDisplay(DISPLAY_ORIENTATION_0);
5370 prepareButtons();
5371 prepareAxes(POSITION);
5372 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005376 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377
5378 // Virtual key is down.
5379 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5380 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5381 processDown(mapper, x, y);
5382 processSync(mapper);
5383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5384
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005385 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386
5387 // Virtual key is up.
5388 processUp(mapper);
5389 processSync(mapper);
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393}
5394
5395TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396 addConfigurationProperty("touch.deviceType", "touchScreen");
5397 prepareDisplay(DISPLAY_ORIENTATION_0);
5398 prepareButtons();
5399 prepareAxes(POSITION);
5400 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005401 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005404 ASSERT_TRUE(
5405 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 ASSERT_TRUE(flags[0]);
5407 ASSERT_FALSE(flags[1]);
5408}
5409
5410TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411 addConfigurationProperty("touch.deviceType", "touchScreen");
5412 prepareDisplay(DISPLAY_ORIENTATION_0);
5413 prepareButtons();
5414 prepareAxes(POSITION);
5415 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
arthurhungdcef2dc2020-08-11 14:47:50 +08005418 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
5420 NotifyKeyArgs args;
5421
5422 // Press virtual key.
5423 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5424 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5425 processDown(mapper, x, y);
5426 processSync(mapper);
5427
5428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5429 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5430 ASSERT_EQ(DEVICE_ID, args.deviceId);
5431 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5432 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5433 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5434 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5435 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5436 ASSERT_EQ(KEY_HOME, args.scanCode);
5437 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5438 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5439
5440 // Release virtual key.
5441 processUp(mapper);
5442 processSync(mapper);
5443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5445 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5446 ASSERT_EQ(DEVICE_ID, args.deviceId);
5447 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5448 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5449 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5450 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5451 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5452 ASSERT_EQ(KEY_HOME, args.scanCode);
5453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5454 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5455
5456 // Should not have sent any motions.
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5458}
5459
5460TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461 addConfigurationProperty("touch.deviceType", "touchScreen");
5462 prepareDisplay(DISPLAY_ORIENTATION_0);
5463 prepareButtons();
5464 prepareAxes(POSITION);
5465 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005466 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467
arthurhungdcef2dc2020-08-11 14:47:50 +08005468 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469
5470 NotifyKeyArgs keyArgs;
5471
5472 // Press virtual key.
5473 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5474 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5475 processDown(mapper, x, y);
5476 processSync(mapper);
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5479 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5480 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5481 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5482 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5484 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5485 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5486 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5488 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5489
5490 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5491 // into the display area.
5492 y -= 100;
5493 processMove(mapper, x, y);
5494 processSync(mapper);
5495
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5497 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5498 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5499 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5500 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5501 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5502 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5503 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5504 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5505 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5507 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5508
5509 NotifyMotionArgs motionArgs;
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5512 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5513 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5514 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5516 ASSERT_EQ(0, motionArgs.flags);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5518 ASSERT_EQ(0, motionArgs.buttonState);
5519 ASSERT_EQ(0, motionArgs.edgeFlags);
5520 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5524 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5525 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5526 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5528
5529 // Keep moving out of bounds. Should generate a pointer move.
5530 y -= 50;
5531 processMove(mapper, x, y);
5532 processSync(mapper);
5533
5534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5536 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5537 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5538 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5540 ASSERT_EQ(0, motionArgs.flags);
5541 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5542 ASSERT_EQ(0, motionArgs.buttonState);
5543 ASSERT_EQ(0, motionArgs.edgeFlags);
5544 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5545 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5548 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5549 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5550 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5552
5553 // Release out of bounds. Should generate a pointer up.
5554 processUp(mapper);
5555 processSync(mapper);
5556
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5563 ASSERT_EQ(0, motionArgs.flags);
5564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5565 ASSERT_EQ(0, motionArgs.buttonState);
5566 ASSERT_EQ(0, motionArgs.edgeFlags);
5567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5571 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5572 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5573 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5574 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5575
5576 // Should not have sent any more keys or motions.
5577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5579}
5580
5581TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 addConfigurationProperty("touch.deviceType", "touchScreen");
5583 prepareDisplay(DISPLAY_ORIENTATION_0);
5584 prepareButtons();
5585 prepareAxes(POSITION);
5586 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005587 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588
arthurhungdcef2dc2020-08-11 14:47:50 +08005589 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
5591 NotifyMotionArgs motionArgs;
5592
5593 // Initially go down out of bounds.
5594 int32_t x = -10;
5595 int32_t y = -10;
5596 processDown(mapper, x, y);
5597 processSync(mapper);
5598
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5600
5601 // Move into the display area. Should generate a pointer down.
5602 x = 50;
5603 y = 75;
5604 processMove(mapper, x, y);
5605 processSync(mapper);
5606
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5608 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5609 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5610 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5611 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5613 ASSERT_EQ(0, motionArgs.flags);
5614 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5615 ASSERT_EQ(0, motionArgs.buttonState);
5616 ASSERT_EQ(0, motionArgs.edgeFlags);
5617 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5621 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5622 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5623 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5624 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5625
5626 // Release. Should generate a pointer up.
5627 processUp(mapper);
5628 processSync(mapper);
5629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5632 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5636 ASSERT_EQ(0, motionArgs.flags);
5637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639 ASSERT_EQ(0, motionArgs.edgeFlags);
5640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5645 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5646 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5647 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5648
5649 // Should not have sent any more keys or motions.
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5652}
5653
Santos Cordonfa5cf462017-04-05 10:37:00 -07005654TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005655 addConfigurationProperty("touch.deviceType", "touchScreen");
5656 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5657
5658 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5659 prepareButtons();
5660 prepareAxes(POSITION);
5661 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005662 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005663
arthurhungdcef2dc2020-08-11 14:47:50 +08005664 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665
5666 NotifyMotionArgs motionArgs;
5667
5668 // Down.
5669 int32_t x = 100;
5670 int32_t y = 125;
5671 processDown(mapper, x, y);
5672 processSync(mapper);
5673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5677 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5678 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5679 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5681 ASSERT_EQ(0, motionArgs.flags);
5682 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5683 ASSERT_EQ(0, motionArgs.buttonState);
5684 ASSERT_EQ(0, motionArgs.edgeFlags);
5685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5689 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5690 1, 0, 0, 0, 0, 0, 0, 0));
5691 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5692 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5693 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5694
5695 // Move.
5696 x += 50;
5697 y += 75;
5698 processMove(mapper, x, y);
5699 processSync(mapper);
5700
5701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5702 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5703 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5704 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5705 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5706 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5708 ASSERT_EQ(0, motionArgs.flags);
5709 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5710 ASSERT_EQ(0, motionArgs.buttonState);
5711 ASSERT_EQ(0, motionArgs.edgeFlags);
5712 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5716 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5717 1, 0, 0, 0, 0, 0, 0, 0));
5718 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5719 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5721
5722 // Up.
5723 processUp(mapper);
5724 processSync(mapper);
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5728 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5729 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5730 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5731 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5732 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5733 ASSERT_EQ(0, motionArgs.flags);
5734 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5735 ASSERT_EQ(0, motionArgs.buttonState);
5736 ASSERT_EQ(0, motionArgs.edgeFlags);
5737 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5738 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5741 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5742 1, 0, 0, 0, 0, 0, 0, 0));
5743 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5744 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5745 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5746
5747 // Should not have sent any more keys or motions.
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5750}
5751
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753 addConfigurationProperty("touch.deviceType", "touchScreen");
5754 prepareDisplay(DISPLAY_ORIENTATION_0);
5755 prepareButtons();
5756 prepareAxes(POSITION);
5757 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005758 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759
arthurhungdcef2dc2020-08-11 14:47:50 +08005760 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761
5762 NotifyMotionArgs motionArgs;
5763
5764 // Down.
5765 int32_t x = 100;
5766 int32_t y = 125;
5767 processDown(mapper, x, y);
5768 processSync(mapper);
5769
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5772 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5773 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5774 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5776 ASSERT_EQ(0, motionArgs.flags);
5777 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5778 ASSERT_EQ(0, motionArgs.buttonState);
5779 ASSERT_EQ(0, motionArgs.edgeFlags);
5780 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5784 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5785 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5786 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5788
5789 // Move.
5790 x += 50;
5791 y += 75;
5792 processMove(mapper, x, y);
5793 processSync(mapper);
5794
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5799 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5801 ASSERT_EQ(0, motionArgs.flags);
5802 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5803 ASSERT_EQ(0, motionArgs.buttonState);
5804 ASSERT_EQ(0, motionArgs.edgeFlags);
5805 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5809 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5810 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5811 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5813
5814 // Up.
5815 processUp(mapper);
5816 processSync(mapper);
5817
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5820 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5821 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5822 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5824 ASSERT_EQ(0, motionArgs.flags);
5825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5826 ASSERT_EQ(0, motionArgs.buttonState);
5827 ASSERT_EQ(0, motionArgs.edgeFlags);
5828 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5833 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5834 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5836
5837 // Should not have sent any more keys or motions.
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5840}
5841
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005842TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareButtons();
5845 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005846 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5847 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005848 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849
5850 NotifyMotionArgs args;
5851
5852 // Rotation 90.
5853 prepareDisplay(DISPLAY_ORIENTATION_90);
5854 processDown(mapper, toRawX(50), toRawY(75));
5855 processSync(mapper);
5856
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5858 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5859 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5860
5861 processUp(mapper);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5864}
5865
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005866TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 addConfigurationProperty("touch.deviceType", "touchScreen");
5868 prepareButtons();
5869 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005870 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5871 // orientation-aware are affected by display rotation.
5872 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005873 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874
5875 NotifyMotionArgs args;
5876
5877 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005878 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 prepareDisplay(DISPLAY_ORIENTATION_0);
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 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005892 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005894 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895 processSync(mapper);
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5898 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5899 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5900
5901 processUp(mapper);
5902 processSync(mapper);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5904
5905 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005906 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 prepareDisplay(DISPLAY_ORIENTATION_180);
5908 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5909 processSync(mapper);
5910
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5912 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5913 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5914
5915 processUp(mapper);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5918
5919 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005920 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005922 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 processSync(mapper);
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5927 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5928
5929 processUp(mapper);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5932}
5933
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005934TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 prepareButtons();
5937 prepareAxes(POSITION);
5938 addConfigurationProperty("touch.orientationAware", "1");
5939 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5940 clearViewports();
5941 prepareDisplay(DISPLAY_ORIENTATION_0);
5942 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5943 NotifyMotionArgs args;
5944
5945 // Orientation 0.
5946 processDown(mapper, toRawX(50), toRawY(75));
5947 processSync(mapper);
5948
5949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5950 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5951 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5952
5953 processUp(mapper);
5954 processSync(mapper);
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5956}
5957
5958TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5959 addConfigurationProperty("touch.deviceType", "touchScreen");
5960 prepareButtons();
5961 prepareAxes(POSITION);
5962 addConfigurationProperty("touch.orientationAware", "1");
5963 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5964 clearViewports();
5965 prepareDisplay(DISPLAY_ORIENTATION_0);
5966 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5967 NotifyMotionArgs args;
5968
5969 // Orientation 90.
5970 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5971 processSync(mapper);
5972
5973 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5974 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5975 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5976
5977 processUp(mapper);
5978 processSync(mapper);
5979 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5980}
5981
5982TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5983 addConfigurationProperty("touch.deviceType", "touchScreen");
5984 prepareButtons();
5985 prepareAxes(POSITION);
5986 addConfigurationProperty("touch.orientationAware", "1");
5987 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5988 clearViewports();
5989 prepareDisplay(DISPLAY_ORIENTATION_0);
5990 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5991 NotifyMotionArgs args;
5992
5993 // Orientation 180.
5994 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5995 processSync(mapper);
5996
5997 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5998 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5999 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6000
6001 processUp(mapper);
6002 processSync(mapper);
6003 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6004}
6005
6006TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6007 addConfigurationProperty("touch.deviceType", "touchScreen");
6008 prepareButtons();
6009 prepareAxes(POSITION);
6010 addConfigurationProperty("touch.orientationAware", "1");
6011 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6012 clearViewports();
6013 prepareDisplay(DISPLAY_ORIENTATION_0);
6014 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6015 NotifyMotionArgs args;
6016
6017 // Orientation 270.
6018 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6019 processSync(mapper);
6020
6021 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6022 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6023 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6024
6025 processUp(mapper);
6026 processSync(mapper);
6027 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6028}
6029
6030TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6031 addConfigurationProperty("touch.deviceType", "touchScreen");
6032 prepareButtons();
6033 prepareAxes(POSITION);
6034 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6035 // orientation-aware are affected by display rotation.
6036 addConfigurationProperty("touch.orientationAware", "0");
6037 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6038 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6039
6040 NotifyMotionArgs args;
6041
6042 // Orientation 90, Rotation 0.
6043 clearViewports();
6044 prepareDisplay(DISPLAY_ORIENTATION_0);
6045 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6046 processSync(mapper);
6047
6048 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6049 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6050 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6051
6052 processUp(mapper);
6053 processSync(mapper);
6054 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6055
6056 // Orientation 90, Rotation 90.
6057 clearViewports();
6058 prepareDisplay(DISPLAY_ORIENTATION_90);
6059 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6060 processSync(mapper);
6061
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6063 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6064 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6065
6066 processUp(mapper);
6067 processSync(mapper);
6068 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6069
6070 // Orientation 90, Rotation 180.
6071 clearViewports();
6072 prepareDisplay(DISPLAY_ORIENTATION_180);
6073 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6074 processSync(mapper);
6075
6076 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6077 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6078 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6079
6080 processUp(mapper);
6081 processSync(mapper);
6082 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6083
6084 // Orientation 90, Rotation 270.
6085 clearViewports();
6086 prepareDisplay(DISPLAY_ORIENTATION_270);
6087 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6088 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6089 processSync(mapper);
6090
6091 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6092 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6093 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6094
6095 processUp(mapper);
6096 processSync(mapper);
6097 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6098}
6099
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 addConfigurationProperty("touch.deviceType", "touchScreen");
6102 prepareDisplay(DISPLAY_ORIENTATION_0);
6103 prepareButtons();
6104 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006105 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106
6107 // These calculations are based on the input device calibration documentation.
6108 int32_t rawX = 100;
6109 int32_t rawY = 200;
6110 int32_t rawPressure = 10;
6111 int32_t rawToolMajor = 12;
6112 int32_t rawDistance = 2;
6113 int32_t rawTiltX = 30;
6114 int32_t rawTiltY = 110;
6115
6116 float x = toDisplayX(rawX);
6117 float y = toDisplayY(rawY);
6118 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6119 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6120 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6121 float distance = float(rawDistance);
6122
6123 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6124 float tiltScale = M_PI / 180;
6125 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6126 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6127 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6128 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6129
6130 processDown(mapper, rawX, rawY);
6131 processPressure(mapper, rawPressure);
6132 processToolMajor(mapper, rawToolMajor);
6133 processDistance(mapper, rawDistance);
6134 processTilt(mapper, rawTiltX, rawTiltY);
6135 processSync(mapper);
6136
6137 NotifyMotionArgs args;
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6140 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6141 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6142}
6143
Jason Gerecke489fda82012-09-07 17:19:40 -07006144TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006145 addConfigurationProperty("touch.deviceType", "touchScreen");
6146 prepareDisplay(DISPLAY_ORIENTATION_0);
6147 prepareLocationCalibration();
6148 prepareButtons();
6149 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006150 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006151
6152 int32_t rawX = 100;
6153 int32_t rawY = 200;
6154
6155 float x = toDisplayX(toCookedX(rawX, rawY));
6156 float y = toDisplayY(toCookedY(rawX, rawY));
6157
6158 processDown(mapper, rawX, rawY);
6159 processSync(mapper);
6160
6161 NotifyMotionArgs args;
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6164 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6165}
6166
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 addConfigurationProperty("touch.deviceType", "touchScreen");
6169 prepareDisplay(DISPLAY_ORIENTATION_0);
6170 prepareButtons();
6171 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173
6174 NotifyMotionArgs motionArgs;
6175 NotifyKeyArgs keyArgs;
6176
6177 processDown(mapper, 100, 200);
6178 processSync(mapper);
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6181 ASSERT_EQ(0, motionArgs.buttonState);
6182
6183 // press BTN_LEFT, release BTN_LEFT
6184 processKey(mapper, BTN_LEFT, 1);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6188 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6189
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6192 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6193
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 processKey(mapper, BTN_LEFT, 0);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006202 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203
6204 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6205 processKey(mapper, BTN_RIGHT, 1);
6206 processKey(mapper, BTN_MIDDLE, 1);
6207 processSync(mapper);
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6210 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6211 motionArgs.buttonState);
6212
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6219 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6220 motionArgs.buttonState);
6221
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222 processKey(mapper, BTN_RIGHT, 0);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231
6232 processKey(mapper, BTN_MIDDLE, 0);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006240 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 // press BTN_BACK, release BTN_BACK
6243 processKey(mapper, BTN_BACK, 1);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6246 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6247 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6252
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6255 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256
6257 processKey(mapper, BTN_BACK, 0);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262
6263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265 ASSERT_EQ(0, motionArgs.buttonState);
6266
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6268 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6269 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6270
6271 // press BTN_SIDE, release BTN_SIDE
6272 processKey(mapper, BTN_SIDE, 1);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6275 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6276 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6281
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285
6286 processKey(mapper, BTN_SIDE, 0);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294 ASSERT_EQ(0, motionArgs.buttonState);
6295
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6297 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6298 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6299
6300 // press BTN_FORWARD, release BTN_FORWARD
6301 processKey(mapper, BTN_FORWARD, 1);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6304 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6305 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006309 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6310
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6313 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
6315 processKey(mapper, BTN_FORWARD, 0);
6316 processSync(mapper);
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006322 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323 ASSERT_EQ(0, motionArgs.buttonState);
6324
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6327 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6328
6329 // press BTN_EXTRA, release BTN_EXTRA
6330 processKey(mapper, BTN_EXTRA, 1);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6334 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006338 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6339
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6342 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343
6344 processKey(mapper, BTN_EXTRA, 0);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352 ASSERT_EQ(0, motionArgs.buttonState);
6353
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6356 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6357
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6359
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 // press BTN_STYLUS, release BTN_STYLUS
6361 processKey(mapper, BTN_STYLUS, 1);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6369 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 processKey(mapper, BTN_STYLUS, 0);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006379 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006380
6381 // press BTN_STYLUS2, release BTN_STYLUS2
6382 processKey(mapper, BTN_STYLUS2, 1);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6390 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 processKey(mapper, BTN_STYLUS2, 0);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006400 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006401
6402 // release touch
6403 processUp(mapper);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6407 ASSERT_EQ(0, motionArgs.buttonState);
6408}
6409
6410TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411 addConfigurationProperty("touch.deviceType", "touchScreen");
6412 prepareDisplay(DISPLAY_ORIENTATION_0);
6413 prepareButtons();
6414 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416
6417 NotifyMotionArgs motionArgs;
6418
6419 // default tool type is finger
6420 processDown(mapper, 100, 200);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6425
6426 // eraser
6427 processKey(mapper, BTN_TOOL_RUBBER, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6432
6433 // stylus
6434 processKey(mapper, BTN_TOOL_RUBBER, 0);
6435 processKey(mapper, BTN_TOOL_PEN, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6440
6441 // brush
6442 processKey(mapper, BTN_TOOL_PEN, 0);
6443 processKey(mapper, BTN_TOOL_BRUSH, 1);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6448
6449 // pencil
6450 processKey(mapper, BTN_TOOL_BRUSH, 0);
6451 processKey(mapper, BTN_TOOL_PENCIL, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6456
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006457 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 processKey(mapper, BTN_TOOL_PENCIL, 0);
6459 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6464
6465 // mouse
6466 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6467 processKey(mapper, BTN_TOOL_MOUSE, 1);
6468 processSync(mapper);
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6472
6473 // lens
6474 processKey(mapper, BTN_TOOL_MOUSE, 0);
6475 processKey(mapper, BTN_TOOL_LENS, 1);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6480
6481 // double-tap
6482 processKey(mapper, BTN_TOOL_LENS, 0);
6483 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6488
6489 // triple-tap
6490 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6491 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6492 processSync(mapper);
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6496
6497 // quad-tap
6498 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6499 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6500 processSync(mapper);
6501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6504
6505 // finger
6506 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6507 processKey(mapper, BTN_TOOL_FINGER, 1);
6508 processSync(mapper);
6509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6512
6513 // stylus trumps finger
6514 processKey(mapper, BTN_TOOL_PEN, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6519
6520 // eraser trumps stylus
6521 processKey(mapper, BTN_TOOL_RUBBER, 1);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6526
6527 // mouse trumps eraser
6528 processKey(mapper, BTN_TOOL_MOUSE, 1);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6533
6534 // back to default tool type
6535 processKey(mapper, BTN_TOOL_MOUSE, 0);
6536 processKey(mapper, BTN_TOOL_RUBBER, 0);
6537 processKey(mapper, BTN_TOOL_PEN, 0);
6538 processKey(mapper, BTN_TOOL_FINGER, 0);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6543}
6544
6545TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546 addConfigurationProperty("touch.deviceType", "touchScreen");
6547 prepareDisplay(DISPLAY_ORIENTATION_0);
6548 prepareButtons();
6549 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552
6553 NotifyMotionArgs motionArgs;
6554
6555 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6556 processKey(mapper, BTN_TOOL_FINGER, 1);
6557 processMove(mapper, 100, 200);
6558 processSync(mapper);
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6563
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6567 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6568
6569 // move a little
6570 processMove(mapper, 150, 250);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6575 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6576
6577 // down when BTN_TOUCH is pressed, pressure defaults to 1
6578 processKey(mapper, BTN_TOUCH, 1);
6579 processSync(mapper);
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6584
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6588 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6589
6590 // up when BTN_TOUCH is released, hover restored
6591 processKey(mapper, BTN_TOUCH, 0);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6607
6608 // exit hover when pointer goes away
6609 processKey(mapper, BTN_TOOL_FINGER, 0);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6615}
6616
6617TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006618 addConfigurationProperty("touch.deviceType", "touchScreen");
6619 prepareDisplay(DISPLAY_ORIENTATION_0);
6620 prepareButtons();
6621 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006622 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623
6624 NotifyMotionArgs motionArgs;
6625
6626 // initially hovering because pressure is 0
6627 processDown(mapper, 100, 200);
6628 processPressure(mapper, 0);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6634
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6638 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6639
6640 // move a little
6641 processMove(mapper, 150, 250);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6646 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6647
6648 // down when pressure is non-zero
6649 processPressure(mapper, RAW_PRESSURE_MAX);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6655
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6657 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6659 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6660
6661 // up when pressure becomes 0, hover restored
6662 processPressure(mapper, 0);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6668
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6677 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6678
6679 // exit hover when pointer goes away
6680 processUp(mapper);
6681 processSync(mapper);
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6685 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6686}
6687
lilinnan687e58f2022-07-19 16:00:50 +08006688TEST_F(SingleTouchInputMapperTest,
6689 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6690 addConfigurationProperty("touch.deviceType", "touchScreen");
6691 prepareDisplay(DISPLAY_ORIENTATION_0);
6692 prepareButtons();
6693 prepareAxes(POSITION);
6694 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6695 NotifyMotionArgs motionArgs;
6696
6697 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006698 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006699 processSync(mapper);
6700
6701 // We should receive a down event
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6704
6705 // Change display id
6706 clearViewports();
6707 prepareSecondaryDisplay(ViewportType::INTERNAL);
6708
6709 // We should receive a cancel event
6710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6711 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6712 // Then receive reset called
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6714}
6715
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006716TEST_F(SingleTouchInputMapperTest,
6717 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6718 addConfigurationProperty("touch.deviceType", "touchScreen");
6719 prepareDisplay(DISPLAY_ORIENTATION_0);
6720 prepareButtons();
6721 prepareAxes(POSITION);
6722 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6724 NotifyMotionArgs motionArgs;
6725
6726 // Start a new gesture.
6727 processDown(mapper, 100, 200);
6728 processSync(mapper);
6729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6731
6732 // Make the viewport inactive. This will put the device in disabled mode.
6733 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6734 viewport->isActive = false;
6735 mFakePolicy->updateViewport(*viewport);
6736 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6737
6738 // We should receive a cancel event for the ongoing gesture.
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6740 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6741 // Then we should be notified that the device was reset.
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6743
6744 // No events are generated while the viewport is inactive.
6745 processMove(mapper, 101, 201);
6746 processSync(mapper);
6747 processDown(mapper, 102, 202);
6748 processSync(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6750
6751 // Make the viewport active again. The device should resume processing events.
6752 viewport->isActive = true;
6753 mFakePolicy->updateViewport(*viewport);
6754 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6755
6756 // The device is reset because it changes back to direct mode, without generating any events.
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6759
6760 // Start a new gesture.
6761 processDown(mapper, 100, 200);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6765
6766 // No more events.
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6769}
6770
Prabir Pradhan5632d622021-09-06 07:57:20 -07006771// --- TouchDisplayProjectionTest ---
6772
6773class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6774public:
6775 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6776 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6777 // rotated equivalent of the given un-rotated physical display bounds.
6778 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6779 uint32_t inverseRotationFlags;
6780 auto width = DISPLAY_WIDTH;
6781 auto height = DISPLAY_HEIGHT;
6782 switch (orientation) {
6783 case DISPLAY_ORIENTATION_90:
6784 inverseRotationFlags = ui::Transform::ROT_270;
6785 std::swap(width, height);
6786 break;
6787 case DISPLAY_ORIENTATION_180:
6788 inverseRotationFlags = ui::Transform::ROT_180;
6789 break;
6790 case DISPLAY_ORIENTATION_270:
6791 inverseRotationFlags = ui::Transform::ROT_90;
6792 std::swap(width, height);
6793 break;
6794 case DISPLAY_ORIENTATION_0:
6795 inverseRotationFlags = ui::Transform::ROT_0;
6796 break;
6797 default:
6798 FAIL() << "Invalid orientation: " << orientation;
6799 }
6800
6801 const ui::Transform rotation(inverseRotationFlags, width, height);
6802 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6803
6804 std::optional<DisplayViewport> internalViewport =
6805 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6806 DisplayViewport& v = *internalViewport;
6807 v.displayId = DISPLAY_ID;
6808 v.orientation = orientation;
6809
6810 v.logicalLeft = 0;
6811 v.logicalTop = 0;
6812 v.logicalRight = 100;
6813 v.logicalBottom = 100;
6814
6815 v.physicalLeft = rotatedPhysicalDisplay.left;
6816 v.physicalTop = rotatedPhysicalDisplay.top;
6817 v.physicalRight = rotatedPhysicalDisplay.right;
6818 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6819
6820 v.deviceWidth = width;
6821 v.deviceHeight = height;
6822
6823 v.isActive = true;
6824 v.uniqueId = UNIQUE_ID;
6825 v.type = ViewportType::INTERNAL;
6826 mFakePolicy->updateViewport(v);
6827 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6828 }
6829
6830 void assertReceivedMove(const Point& point) {
6831 NotifyMotionArgs motionArgs;
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6834 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6836 1, 0, 0, 0, 0, 0, 0, 0));
6837 }
6838};
6839
6840TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6841 addConfigurationProperty("touch.deviceType", "touchScreen");
6842 prepareDisplay(DISPLAY_ORIENTATION_0);
6843
6844 prepareButtons();
6845 prepareAxes(POSITION);
6846 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6847
6848 NotifyMotionArgs motionArgs;
6849
6850 // Configure the DisplayViewport such that the logical display maps to a subsection of
6851 // the display panel called the physical display. Here, the physical display is bounded by the
6852 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6853 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6854 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6855 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6856
6857 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6858 DISPLAY_ORIENTATION_270}) {
6859 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6860
6861 // Touches outside the physical display should be ignored, and should not generate any
6862 // events. Ensure touches at the following points that lie outside of the physical display
6863 // area do not generate any events.
6864 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6865 processDown(mapper, toRawX(point.x), toRawY(point.y));
6866 processSync(mapper);
6867 processUp(mapper);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6870 << "Unexpected event generated for touch outside physical display at point: "
6871 << point.x << ", " << point.y;
6872 }
6873 }
6874}
6875
6876TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6877 addConfigurationProperty("touch.deviceType", "touchScreen");
6878 prepareDisplay(DISPLAY_ORIENTATION_0);
6879
6880 prepareButtons();
6881 prepareAxes(POSITION);
6882 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6883
6884 NotifyMotionArgs motionArgs;
6885
6886 // Configure the DisplayViewport such that the logical display maps to a subsection of
6887 // the display panel called the physical display. Here, the physical display is bounded by the
6888 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6889 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6890
6891 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6892 DISPLAY_ORIENTATION_270}) {
6893 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6894
6895 // Touches that start outside the physical display should be ignored until it enters the
6896 // physical display bounds, at which point it should generate a down event. Start a touch at
6897 // the point (5, 100), which is outside the physical display bounds.
6898 static const Point kOutsidePoint{5, 100};
6899 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6902
6903 // Move the touch into the physical display area. This should generate a pointer down.
6904 processMove(mapper, toRawX(11), toRawY(21));
6905 processSync(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6908 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6909 ASSERT_NO_FATAL_FAILURE(
6910 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6911
6912 // Move the touch inside the physical display area. This should generate a pointer move.
6913 processMove(mapper, toRawX(69), toRawY(159));
6914 processSync(mapper);
6915 assertReceivedMove({69, 159});
6916
6917 // Move outside the physical display area. Since the pointer is already down, this should
6918 // now continue generating events.
6919 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6920 processSync(mapper);
6921 assertReceivedMove(kOutsidePoint);
6922
6923 // Release. This should generate a pointer up.
6924 processUp(mapper);
6925 processSync(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6929 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6930
6931 // Ensure no more events were generated.
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6934 }
6935}
6936
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937// --- MultiTouchInputMapperTest ---
6938
6939class MultiTouchInputMapperTest : public TouchInputMapperTest {
6940protected:
6941 void prepareAxes(int axes);
6942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006943 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6944 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6945 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6946 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6947 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6948 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6949 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6950 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6951 void processId(MultiTouchInputMapper& mapper, int32_t id);
6952 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6953 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6954 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6955 void processMTSync(MultiTouchInputMapper& mapper);
6956 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957};
6958
6959void MultiTouchInputMapperTest::prepareAxes(int axes) {
6960 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006961 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963 }
6964 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006965 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6966 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6969 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970 }
6971 }
6972 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006973 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6974 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006976 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006977 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 }
6979 }
6980 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006981 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6982 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983 }
6984 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006985 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6986 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987 }
6988 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6990 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991 }
6992 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6994 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 }
6996 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6998 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006999 }
7000 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002 }
7003}
7004
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007005void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7006 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009}
7010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7012 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014}
7015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7017 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019}
7020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007021void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023}
7024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007025void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007027}
7028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007029void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7030 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032}
7033
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007034void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036}
7037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007038void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040}
7041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007042void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044}
7045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007046void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048}
7049
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007050void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052}
7053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7055 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057}
7058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007061}
7062
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007063void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065}
7066
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 addConfigurationProperty("touch.deviceType", "touchScreen");
7069 prepareDisplay(DISPLAY_ORIENTATION_0);
7070 prepareAxes(POSITION);
7071 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007072 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073
arthurhungdcef2dc2020-08-11 14:47:50 +08007074 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075
7076 NotifyMotionArgs motionArgs;
7077
7078 // Two fingers down at once.
7079 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7080 processPosition(mapper, x1, y1);
7081 processMTSync(mapper);
7082 processPosition(mapper, x2, y2);
7083 processMTSync(mapper);
7084 processSync(mapper);
7085
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7087 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7088 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7089 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7090 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7092 ASSERT_EQ(0, motionArgs.flags);
7093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7094 ASSERT_EQ(0, motionArgs.buttonState);
7095 ASSERT_EQ(0, motionArgs.edgeFlags);
7096 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7097 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7100 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7101 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7102 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7103 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7104
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7107 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7108 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7109 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007110 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 ASSERT_EQ(0, motionArgs.flags);
7112 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7113 ASSERT_EQ(0, motionArgs.buttonState);
7114 ASSERT_EQ(0, motionArgs.edgeFlags);
7115 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7116 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7118 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7121 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7123 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7124 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7125 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7126 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7127
7128 // Move.
7129 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7130 processPosition(mapper, x1, y1);
7131 processMTSync(mapper);
7132 processPosition(mapper, x2, y2);
7133 processMTSync(mapper);
7134 processSync(mapper);
7135
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7142 ASSERT_EQ(0, motionArgs.flags);
7143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7144 ASSERT_EQ(0, motionArgs.buttonState);
7145 ASSERT_EQ(0, motionArgs.edgeFlags);
7146 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7149 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7152 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7155 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7156 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7157 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7158
7159 // First finger up.
7160 x2 += 15; y2 -= 20;
7161 processPosition(mapper, x2, y2);
7162 processMTSync(mapper);
7163 processSync(mapper);
7164
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7167 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7168 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7169 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007170 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 ASSERT_EQ(0, motionArgs.flags);
7172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7173 ASSERT_EQ(0, motionArgs.buttonState);
7174 ASSERT_EQ(0, motionArgs.edgeFlags);
7175 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7178 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7181 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7184 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7185 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7191 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7192 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7194 ASSERT_EQ(0, motionArgs.flags);
7195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7196 ASSERT_EQ(0, motionArgs.buttonState);
7197 ASSERT_EQ(0, motionArgs.edgeFlags);
7198 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7199 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7206
7207 // Move.
7208 x2 += 20; y2 -= 25;
7209 processPosition(mapper, x2, y2);
7210 processMTSync(mapper);
7211 processSync(mapper);
7212
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7215 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7216 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7217 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7219 ASSERT_EQ(0, motionArgs.flags);
7220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7221 ASSERT_EQ(0, motionArgs.buttonState);
7222 ASSERT_EQ(0, motionArgs.edgeFlags);
7223 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7224 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7227 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7228 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7229 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7230 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7231
7232 // New finger down.
7233 int32_t x3 = 700, y3 = 300;
7234 processPosition(mapper, x2, y2);
7235 processMTSync(mapper);
7236 processPosition(mapper, x3, y3);
7237 processMTSync(mapper);
7238 processSync(mapper);
7239
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7243 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7244 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007245 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 ASSERT_EQ(0, motionArgs.flags);
7247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7248 ASSERT_EQ(0, motionArgs.buttonState);
7249 ASSERT_EQ(0, motionArgs.edgeFlags);
7250 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7253 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7256 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7258 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7262
7263 // Second finger up.
7264 x3 += 30; y3 -= 20;
7265 processPosition(mapper, x3, y3);
7266 processMTSync(mapper);
7267 processSync(mapper);
7268
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007274 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_EQ(0, motionArgs.flags);
7276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7277 ASSERT_EQ(0, motionArgs.buttonState);
7278 ASSERT_EQ(0, motionArgs.edgeFlags);
7279 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7282 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7287 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7288 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7289 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(0, motionArgs.flags);
7299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7300 ASSERT_EQ(0, motionArgs.buttonState);
7301 ASSERT_EQ(0, motionArgs.edgeFlags);
7302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7306 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7310
7311 // Last finger up.
7312 processMTSync(mapper);
7313 processSync(mapper);
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7321 ASSERT_EQ(0, motionArgs.flags);
7322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7323 ASSERT_EQ(0, motionArgs.buttonState);
7324 ASSERT_EQ(0, motionArgs.edgeFlags);
7325 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7329 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7330 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7331 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7333
7334 // Should not have sent any more keys or motions.
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7337}
7338
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007339TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7340 addConfigurationProperty("touch.deviceType", "touchScreen");
7341 prepareDisplay(DISPLAY_ORIENTATION_0);
7342
7343 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7344 /*fuzz*/ 0, /*resolution*/ 10);
7345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7346 /*fuzz*/ 0, /*resolution*/ 11);
7347 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7348 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7349 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7350 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7351 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7352 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7353 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7354 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7355
7356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7357
7358 // X and Y axes
7359 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7360 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7361 // Touch major and minor
7362 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7363 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7364 // Tool major and minor
7365 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7366 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7367}
7368
7369TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7370 addConfigurationProperty("touch.deviceType", "touchScreen");
7371 prepareDisplay(DISPLAY_ORIENTATION_0);
7372
7373 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7374 /*fuzz*/ 0, /*resolution*/ 10);
7375 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7376 /*fuzz*/ 0, /*resolution*/ 11);
7377
7378 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7379
7380 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7381
7382 // Touch major and minor
7383 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7384 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7385 // Tool major and minor
7386 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7387 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7388}
7389
Michael Wrightd02c5b62014-02-10 15:10:22 -08007390TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391 addConfigurationProperty("touch.deviceType", "touchScreen");
7392 prepareDisplay(DISPLAY_ORIENTATION_0);
7393 prepareAxes(POSITION | ID);
7394 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007395 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396
arthurhungdcef2dc2020-08-11 14:47:50 +08007397 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398
7399 NotifyMotionArgs motionArgs;
7400
7401 // Two fingers down at once.
7402 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7403 processPosition(mapper, x1, y1);
7404 processId(mapper, 1);
7405 processMTSync(mapper);
7406 processPosition(mapper, x2, y2);
7407 processId(mapper, 2);
7408 processMTSync(mapper);
7409 processSync(mapper);
7410
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7414 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7417 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007420 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7422 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7424 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7427 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7429 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7430
7431 // Move.
7432 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7433 processPosition(mapper, x1, y1);
7434 processId(mapper, 1);
7435 processMTSync(mapper);
7436 processPosition(mapper, x2, y2);
7437 processId(mapper, 2);
7438 processMTSync(mapper);
7439 processSync(mapper);
7440
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7443 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7444 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7446 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7451 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7452
7453 // First finger up.
7454 x2 += 15; y2 -= 20;
7455 processPosition(mapper, x2, y2);
7456 processId(mapper, 2);
7457 processMTSync(mapper);
7458 processSync(mapper);
7459
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007461 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7463 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7465 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7468 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7470 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7471
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7474 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7475 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7478 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7479
7480 // Move.
7481 x2 += 20; y2 -= 25;
7482 processPosition(mapper, x2, y2);
7483 processId(mapper, 2);
7484 processMTSync(mapper);
7485 processSync(mapper);
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7489 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7490 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7494
7495 // New finger down.
7496 int32_t x3 = 700, y3 = 300;
7497 processPosition(mapper, x2, y2);
7498 processId(mapper, 2);
7499 processMTSync(mapper);
7500 processPosition(mapper, x3, y3);
7501 processId(mapper, 3);
7502 processMTSync(mapper);
7503 processSync(mapper);
7504
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007506 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007507 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7510 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7513 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7515 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7516
7517 // Second finger up.
7518 x3 += 30; y3 -= 20;
7519 processPosition(mapper, x3, y3);
7520 processId(mapper, 3);
7521 processMTSync(mapper);
7522 processSync(mapper);
7523
7524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007525 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7527 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7529 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7532 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7534 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7535
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7542 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7543
7544 // Last finger up.
7545 processMTSync(mapper);
7546 processSync(mapper);
7547
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7550 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 // Should not have sent any more keys or motions.
7557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7559}
7560
7561TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 addConfigurationProperty("touch.deviceType", "touchScreen");
7563 prepareDisplay(DISPLAY_ORIENTATION_0);
7564 prepareAxes(POSITION | ID | SLOT);
7565 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007566 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007567
arthurhungdcef2dc2020-08-11 14:47:50 +08007568 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569
7570 NotifyMotionArgs motionArgs;
7571
7572 // Two fingers down at once.
7573 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7574 processPosition(mapper, x1, y1);
7575 processId(mapper, 1);
7576 processSlot(mapper, 1);
7577 processPosition(mapper, x2, y2);
7578 processId(mapper, 2);
7579 processSync(mapper);
7580
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7583 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7584 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7587 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7588
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007590 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7592 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7594 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7597 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7600
7601 // Move.
7602 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7603 processSlot(mapper, 0);
7604 processPosition(mapper, x1, y1);
7605 processSlot(mapper, 1);
7606 processPosition(mapper, x2, y2);
7607 processSync(mapper);
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7611 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7620
7621 // First finger up.
7622 x2 += 15; y2 -= 20;
7623 processSlot(mapper, 0);
7624 processId(mapper, -1);
7625 processSlot(mapper, 1);
7626 processPosition(mapper, x2, y2);
7627 processSync(mapper);
7628
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007630 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7632 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7634 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7637 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7639 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7640
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7643 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7644 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7647 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7648
7649 // Move.
7650 x2 += 20; y2 -= 25;
7651 processPosition(mapper, x2, y2);
7652 processSync(mapper);
7653
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7657 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // New finger down.
7663 int32_t x3 = 700, y3 = 300;
7664 processPosition(mapper, x2, y2);
7665 processSlot(mapper, 0);
7666 processId(mapper, 3);
7667 processPosition(mapper, x3, y3);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007671 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7675 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7680 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7681
7682 // Second finger up.
7683 x3 += 30; y3 -= 20;
7684 processSlot(mapper, 1);
7685 processId(mapper, -1);
7686 processSlot(mapper, 0);
7687 processPosition(mapper, x3, y3);
7688 processSync(mapper);
7689
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007691 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7695 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7698 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7700 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7701
7702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7703 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7704 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7705 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7706 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7708 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7709
7710 // Last finger up.
7711 processId(mapper, -1);
7712 processSync(mapper);
7713
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7715 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7716 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7720 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7721
7722 // Should not have sent any more keys or motions.
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7725}
7726
7727TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728 addConfigurationProperty("touch.deviceType", "touchScreen");
7729 prepareDisplay(DISPLAY_ORIENTATION_0);
7730 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732
7733 // These calculations are based on the input device calibration documentation.
7734 int32_t rawX = 100;
7735 int32_t rawY = 200;
7736 int32_t rawTouchMajor = 7;
7737 int32_t rawTouchMinor = 6;
7738 int32_t rawToolMajor = 9;
7739 int32_t rawToolMinor = 8;
7740 int32_t rawPressure = 11;
7741 int32_t rawDistance = 0;
7742 int32_t rawOrientation = 3;
7743 int32_t id = 5;
7744
7745 float x = toDisplayX(rawX);
7746 float y = toDisplayY(rawY);
7747 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7748 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7749 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7750 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7751 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7752 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7753 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7754 float distance = float(rawDistance);
7755
7756 processPosition(mapper, rawX, rawY);
7757 processTouchMajor(mapper, rawTouchMajor);
7758 processTouchMinor(mapper, rawTouchMinor);
7759 processToolMajor(mapper, rawToolMajor);
7760 processToolMinor(mapper, rawToolMinor);
7761 processPressure(mapper, rawPressure);
7762 processOrientation(mapper, rawOrientation);
7763 processDistance(mapper, rawDistance);
7764 processId(mapper, id);
7765 processMTSync(mapper);
7766 processSync(mapper);
7767
7768 NotifyMotionArgs args;
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7770 ASSERT_EQ(0, args.pointerProperties[0].id);
7771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7772 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7773 orientation, distance));
7774}
7775
7776TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 addConfigurationProperty("touch.deviceType", "touchScreen");
7778 prepareDisplay(DISPLAY_ORIENTATION_0);
7779 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7780 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782
7783 // These calculations are based on the input device calibration documentation.
7784 int32_t rawX = 100;
7785 int32_t rawY = 200;
7786 int32_t rawTouchMajor = 140;
7787 int32_t rawTouchMinor = 120;
7788 int32_t rawToolMajor = 180;
7789 int32_t rawToolMinor = 160;
7790
7791 float x = toDisplayX(rawX);
7792 float y = toDisplayY(rawY);
7793 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7794 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7795 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7796 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7797 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7798
7799 processPosition(mapper, rawX, rawY);
7800 processTouchMajor(mapper, rawTouchMajor);
7801 processTouchMinor(mapper, rawTouchMinor);
7802 processToolMajor(mapper, rawToolMajor);
7803 processToolMinor(mapper, rawToolMinor);
7804 processMTSync(mapper);
7805 processSync(mapper);
7806
7807 NotifyMotionArgs args;
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7810 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7811}
7812
7813TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 addConfigurationProperty("touch.deviceType", "touchScreen");
7815 prepareDisplay(DISPLAY_ORIENTATION_0);
7816 prepareAxes(POSITION | TOUCH | TOOL);
7817 addConfigurationProperty("touch.size.calibration", "diameter");
7818 addConfigurationProperty("touch.size.scale", "10");
7819 addConfigurationProperty("touch.size.bias", "160");
7820 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007821 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822
7823 // These calculations are based on the input device calibration documentation.
7824 // Note: We only provide a single common touch/tool value because the device is assumed
7825 // not to emit separate values for each pointer (isSummed = 1).
7826 int32_t rawX = 100;
7827 int32_t rawY = 200;
7828 int32_t rawX2 = 150;
7829 int32_t rawY2 = 250;
7830 int32_t rawTouchMajor = 5;
7831 int32_t rawToolMajor = 8;
7832
7833 float x = toDisplayX(rawX);
7834 float y = toDisplayY(rawY);
7835 float x2 = toDisplayX(rawX2);
7836 float y2 = toDisplayY(rawY2);
7837 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7838 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7839 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7840
7841 processPosition(mapper, rawX, rawY);
7842 processTouchMajor(mapper, rawTouchMajor);
7843 processToolMajor(mapper, rawToolMajor);
7844 processMTSync(mapper);
7845 processPosition(mapper, rawX2, rawY2);
7846 processTouchMajor(mapper, rawTouchMajor);
7847 processToolMajor(mapper, rawToolMajor);
7848 processMTSync(mapper);
7849 processSync(mapper);
7850
7851 NotifyMotionArgs args;
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7854
7855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007856 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 ASSERT_EQ(size_t(2), args.pointerCount);
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7859 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7861 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7862}
7863
7864TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 addConfigurationProperty("touch.deviceType", "touchScreen");
7866 prepareDisplay(DISPLAY_ORIENTATION_0);
7867 prepareAxes(POSITION | TOUCH | TOOL);
7868 addConfigurationProperty("touch.size.calibration", "area");
7869 addConfigurationProperty("touch.size.scale", "43");
7870 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007871 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872
7873 // These calculations are based on the input device calibration documentation.
7874 int32_t rawX = 100;
7875 int32_t rawY = 200;
7876 int32_t rawTouchMajor = 5;
7877 int32_t rawToolMajor = 8;
7878
7879 float x = toDisplayX(rawX);
7880 float y = toDisplayY(rawY);
7881 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7882 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7883 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7884
7885 processPosition(mapper, rawX, rawY);
7886 processTouchMajor(mapper, rawTouchMajor);
7887 processToolMajor(mapper, rawToolMajor);
7888 processMTSync(mapper);
7889 processSync(mapper);
7890
7891 NotifyMotionArgs args;
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7894 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7895}
7896
7897TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898 addConfigurationProperty("touch.deviceType", "touchScreen");
7899 prepareDisplay(DISPLAY_ORIENTATION_0);
7900 prepareAxes(POSITION | PRESSURE);
7901 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7902 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904
Michael Wrightaa449c92017-12-13 21:21:43 +00007905 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007906 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7908 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7909 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7910
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 // These calculations are based on the input device calibration documentation.
7912 int32_t rawX = 100;
7913 int32_t rawY = 200;
7914 int32_t rawPressure = 60;
7915
7916 float x = toDisplayX(rawX);
7917 float y = toDisplayY(rawY);
7918 float pressure = float(rawPressure) * 0.01f;
7919
7920 processPosition(mapper, rawX, rawY);
7921 processPressure(mapper, rawPressure);
7922 processMTSync(mapper);
7923 processSync(mapper);
7924
7925 NotifyMotionArgs args;
7926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7928 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7929}
7930
7931TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007932 addConfigurationProperty("touch.deviceType", "touchScreen");
7933 prepareDisplay(DISPLAY_ORIENTATION_0);
7934 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007935 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936
7937 NotifyMotionArgs motionArgs;
7938 NotifyKeyArgs keyArgs;
7939
7940 processId(mapper, 1);
7941 processPosition(mapper, 100, 200);
7942 processSync(mapper);
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7945 ASSERT_EQ(0, motionArgs.buttonState);
7946
7947 // press BTN_LEFT, release BTN_LEFT
7948 processKey(mapper, BTN_LEFT, 1);
7949 processSync(mapper);
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7952 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7953
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7956 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7957
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 processKey(mapper, BTN_LEFT, 0);
7959 processSync(mapper);
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007963
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007966 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967
7968 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7969 processKey(mapper, BTN_RIGHT, 1);
7970 processKey(mapper, BTN_MIDDLE, 1);
7971 processSync(mapper);
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7974 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7975 motionArgs.buttonState);
7976
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7979 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7980
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7983 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7984 motionArgs.buttonState);
7985
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 processKey(mapper, BTN_RIGHT, 0);
7987 processSync(mapper);
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007991
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995
7996 processKey(mapper, BTN_MIDDLE, 0);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008001
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008004 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005
8006 // press BTN_BACK, release BTN_BACK
8007 processKey(mapper, BTN_BACK, 1);
8008 processSync(mapper);
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8010 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8011 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008012
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008015 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8016
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8019 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020
8021 processKey(mapper, BTN_BACK, 0);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008029 ASSERT_EQ(0, motionArgs.buttonState);
8030
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8032 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8033 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8034
8035 // press BTN_SIDE, release BTN_SIDE
8036 processKey(mapper, BTN_SIDE, 1);
8037 processSync(mapper);
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8040 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008044 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8048 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049
8050 processKey(mapper, BTN_SIDE, 0);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(0, motionArgs.buttonState);
8059
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8061 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8062 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8063
8064 // press BTN_FORWARD, release BTN_FORWARD
8065 processKey(mapper, BTN_FORWARD, 1);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8068 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8069 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8074
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8077 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078
8079 processKey(mapper, BTN_FORWARD, 0);
8080 processSync(mapper);
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008087 ASSERT_EQ(0, motionArgs.buttonState);
8088
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8091 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8092
8093 // press BTN_EXTRA, release BTN_EXTRA
8094 processKey(mapper, BTN_EXTRA, 1);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8097 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8098 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8103
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8106 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107
8108 processKey(mapper, BTN_EXTRA, 0);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116 ASSERT_EQ(0, motionArgs.buttonState);
8117
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8119 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8120 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8121
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8123
Michael Wrightd02c5b62014-02-10 15:10:22 -08008124 // press BTN_STYLUS, release BTN_STYLUS
8125 processKey(mapper, BTN_STYLUS, 1);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8130
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8133 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134
8135 processKey(mapper, BTN_STYLUS, 0);
8136 processSync(mapper);
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008143 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144
8145 // press BTN_STYLUS2, release BTN_STYLUS2
8146 processKey(mapper, BTN_STYLUS2, 1);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8154 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 processKey(mapper, BTN_STYLUS2, 0);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165
8166 // release touch
8167 processId(mapper, -1);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8171 ASSERT_EQ(0, motionArgs.buttonState);
8172}
8173
8174TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 addConfigurationProperty("touch.deviceType", "touchScreen");
8176 prepareDisplay(DISPLAY_ORIENTATION_0);
8177 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008178 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179
8180 NotifyMotionArgs motionArgs;
8181
8182 // default tool type is finger
8183 processId(mapper, 1);
8184 processPosition(mapper, 100, 200);
8185 processSync(mapper);
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8189
8190 // eraser
8191 processKey(mapper, BTN_TOOL_RUBBER, 1);
8192 processSync(mapper);
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8196
8197 // stylus
8198 processKey(mapper, BTN_TOOL_RUBBER, 0);
8199 processKey(mapper, BTN_TOOL_PEN, 1);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8204
8205 // brush
8206 processKey(mapper, BTN_TOOL_PEN, 0);
8207 processKey(mapper, BTN_TOOL_BRUSH, 1);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8212
8213 // pencil
8214 processKey(mapper, BTN_TOOL_BRUSH, 0);
8215 processKey(mapper, BTN_TOOL_PENCIL, 1);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8220
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008221 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 processKey(mapper, BTN_TOOL_PENCIL, 0);
8223 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8224 processSync(mapper);
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8228
8229 // mouse
8230 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8231 processKey(mapper, BTN_TOOL_MOUSE, 1);
8232 processSync(mapper);
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8236
8237 // lens
8238 processKey(mapper, BTN_TOOL_MOUSE, 0);
8239 processKey(mapper, BTN_TOOL_LENS, 1);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8244
8245 // double-tap
8246 processKey(mapper, BTN_TOOL_LENS, 0);
8247 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8252
8253 // triple-tap
8254 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8255 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8260
8261 // quad-tap
8262 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8263 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8268
8269 // finger
8270 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8271 processKey(mapper, BTN_TOOL_FINGER, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8276
8277 // stylus trumps finger
8278 processKey(mapper, BTN_TOOL_PEN, 1);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8283
8284 // eraser trumps stylus
8285 processKey(mapper, BTN_TOOL_RUBBER, 1);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8290
8291 // mouse trumps eraser
8292 processKey(mapper, BTN_TOOL_MOUSE, 1);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8297
8298 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8299 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8300 processSync(mapper);
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8304
8305 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8306 processToolType(mapper, MT_TOOL_PEN);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8311
8312 // back to default tool type
8313 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8314 processKey(mapper, BTN_TOOL_MOUSE, 0);
8315 processKey(mapper, BTN_TOOL_RUBBER, 0);
8316 processKey(mapper, BTN_TOOL_PEN, 0);
8317 processKey(mapper, BTN_TOOL_FINGER, 0);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8322}
8323
8324TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325 addConfigurationProperty("touch.deviceType", "touchScreen");
8326 prepareDisplay(DISPLAY_ORIENTATION_0);
8327 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008328 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008329 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330
8331 NotifyMotionArgs motionArgs;
8332
8333 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8334 processId(mapper, 1);
8335 processPosition(mapper, 100, 200);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8341
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8345 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8346
8347 // move a little
8348 processPosition(mapper, 150, 250);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8353 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8354
8355 // down when BTN_TOUCH is pressed, pressure defaults to 1
8356 processKey(mapper, BTN_TOUCH, 1);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8361 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8362
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8364 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8366 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8367
8368 // up when BTN_TOUCH is released, hover restored
8369 processKey(mapper, BTN_TOUCH, 0);
8370 processSync(mapper);
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8375
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8385
8386 // exit hover when pointer goes away
8387 processId(mapper, -1);
8388 processSync(mapper);
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8392 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8393}
8394
8395TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 addConfigurationProperty("touch.deviceType", "touchScreen");
8397 prepareDisplay(DISPLAY_ORIENTATION_0);
8398 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008399 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008400
8401 NotifyMotionArgs motionArgs;
8402
8403 // initially hovering because pressure is 0
8404 processId(mapper, 1);
8405 processPosition(mapper, 100, 200);
8406 processPressure(mapper, 0);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8411 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8412
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8416 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8417
8418 // move a little
8419 processPosition(mapper, 150, 250);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8424 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8425
8426 // down when pressure becomes non-zero
8427 processPressure(mapper, RAW_PRESSURE_MAX);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8432 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8433
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8438
8439 // up when pressure becomes 0, hover restored
8440 processPressure(mapper, 0);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8445 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8453 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8456
8457 // exit hover when pointer goes away
8458 processId(mapper, -1);
8459 processSync(mapper);
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8463 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8464}
8465
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008466/**
8467 * Set the input device port <--> display port associations, and check that the
8468 * events are routed to the display that matches the display port.
8469 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8470 */
8471TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008472 const std::string usb2 = "USB2";
8473 const uint8_t hdmi1 = 0;
8474 const uint8_t hdmi2 = 1;
8475 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008476 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008477
8478 addConfigurationProperty("touch.deviceType", "touchScreen");
8479 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008481
8482 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8483 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8484
8485 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8486 // for this input device is specified, and the matching viewport is not present,
8487 // the input device should be disabled (at the mapper level).
8488
8489 // Add viewport for display 2 on hdmi2
8490 prepareSecondaryDisplay(type, hdmi2);
8491 // Send a touch event
8492 processPosition(mapper, 100, 100);
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8495
8496 // Add viewport for display 1 on hdmi1
8497 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8498 // Send a touch event again
8499 processPosition(mapper, 100, 100);
8500 processSync(mapper);
8501
8502 NotifyMotionArgs args;
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8504 ASSERT_EQ(DISPLAY_ID, args.displayId);
8505}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008507TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8508 addConfigurationProperty("touch.deviceType", "touchScreen");
8509 prepareAxes(POSITION);
8510 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8511
8512 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8513
8514 prepareDisplay(DISPLAY_ORIENTATION_0);
8515 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8516
8517 // Send a touch event
8518 processPosition(mapper, 100, 100);
8519 processSync(mapper);
8520
8521 NotifyMotionArgs args;
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8523 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8524}
8525
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008526TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008527 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008528 std::shared_ptr<FakePointerController> fakePointerController =
8529 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008530 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008531 fakePointerController->setPosition(100, 200);
8532 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008533 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008534
Garfield Tan888a6a42020-01-09 11:39:16 -08008535 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008536 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008537
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008538 prepareDisplay(DISPLAY_ORIENTATION_0);
8539 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008541
8542 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008543 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008544
8545 NotifyMotionArgs motionArgs;
8546 processPosition(mapper, 100, 100);
8547 processSync(mapper);
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8551 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8552}
8553
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008554/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008555 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8556 */
8557TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8558 addConfigurationProperty("touch.deviceType", "touchScreen");
8559 prepareAxes(POSITION);
8560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8561
8562 prepareDisplay(DISPLAY_ORIENTATION_0);
8563 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8564 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8565 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8566 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8567
8568 NotifyMotionArgs args;
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8570 ASSERT_EQ(26, args.readTime);
8571
8572 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8573 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8574 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8575
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8577 ASSERT_EQ(33, args.readTime);
8578}
8579
8580/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008581 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8582 * events should not be delivered to the listener.
8583 */
8584TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8585 addConfigurationProperty("touch.deviceType", "touchScreen");
8586 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8587 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8588 ViewportType::INTERNAL);
8589 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8590 prepareAxes(POSITION);
8591 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8592
8593 NotifyMotionArgs motionArgs;
8594 processPosition(mapper, 100, 100);
8595 processSync(mapper);
8596
8597 mFakeListener->assertNotifyMotionWasNotCalled();
8598}
8599
Garfield Tanc734e4f2021-01-15 20:01:39 -08008600TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8601 addConfigurationProperty("touch.deviceType", "touchScreen");
8602 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8603 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8604 ViewportType::INTERNAL);
8605 std::optional<DisplayViewport> optionalDisplayViewport =
8606 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8607 ASSERT_TRUE(optionalDisplayViewport.has_value());
8608 DisplayViewport displayViewport = *optionalDisplayViewport;
8609
8610 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8611 prepareAxes(POSITION);
8612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8613
8614 // Finger down
8615 int32_t x = 100, y = 100;
8616 processPosition(mapper, x, y);
8617 processSync(mapper);
8618
8619 NotifyMotionArgs motionArgs;
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8621 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8622
8623 // Deactivate display viewport
8624 displayViewport.isActive = false;
8625 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8626 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8627
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008628 // The ongoing touch should be canceled immediately
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8631
8632 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008633 x += 10, y += 10;
8634 processPosition(mapper, x, y);
8635 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008637
8638 // Reactivate display viewport
8639 displayViewport.isActive = true;
8640 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8641 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8642
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008643 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008644 x += 10, y += 10;
8645 processPosition(mapper, x, y);
8646 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008649}
8650
Arthur Hung7c645402019-01-25 17:45:42 +08008651TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8652 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008653 prepareAxes(POSITION | ID | SLOT);
8654 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008656
8657 // Create the second touch screen device, and enable multi fingers.
8658 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008659 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008660 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008661 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008662 std::shared_ptr<InputDevice> device2 =
8663 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008664 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008666 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8667 0 /*flat*/, 0 /*fuzz*/);
8668 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8669 0 /*flat*/, 0 /*fuzz*/);
8670 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8671 0 /*flat*/, 0 /*fuzz*/);
8672 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8673 0 /*flat*/, 0 /*fuzz*/);
8674 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8675 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8676 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008677
8678 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008679 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008680 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8681 device2->reset(ARBITRARY_TIME);
8682
8683 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008684 std::shared_ptr<FakePointerController> fakePointerController =
8685 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008686 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008687
8688 // Setup policy for associated displays and show touches.
8689 const uint8_t hdmi1 = 0;
8690 const uint8_t hdmi2 = 1;
8691 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8692 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8693 mFakePolicy->setShowTouches(true);
8694
8695 // Create displays.
8696 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008697 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008698
8699 // Default device will reconfigure above, need additional reconfiguration for another device.
8700 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008701 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8702 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008703
8704 // Two fingers down at default display.
8705 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8706 processPosition(mapper, x1, y1);
8707 processId(mapper, 1);
8708 processSlot(mapper, 1);
8709 processPosition(mapper, x2, y2);
8710 processId(mapper, 2);
8711 processSync(mapper);
8712
8713 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8714 fakePointerController->getSpots().find(DISPLAY_ID);
8715 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8716 ASSERT_EQ(size_t(2), iter->second.size());
8717
8718 // Two fingers down at second display.
8719 processPosition(mapper2, x1, y1);
8720 processId(mapper2, 1);
8721 processSlot(mapper2, 1);
8722 processPosition(mapper2, x2, y2);
8723 processId(mapper2, 2);
8724 processSync(mapper2);
8725
8726 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8727 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8728 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008729
8730 // Disable the show touches configuration and ensure the spots are cleared.
8731 mFakePolicy->setShowTouches(false);
8732 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8733 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8734
8735 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008736}
8737
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008738TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008739 prepareAxes(POSITION);
8740 addConfigurationProperty("touch.deviceType", "touchScreen");
8741 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008743
8744 NotifyMotionArgs motionArgs;
8745 // Unrotated video frame
8746 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8747 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008748 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008749 processPosition(mapper, 100, 200);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 ASSERT_EQ(frames, motionArgs.videoFrames);
8753
8754 // Subsequent touch events should not have any videoframes
8755 // This is implemented separately in FakeEventHub,
8756 // but that should match the behaviour of TouchVideoDevice.
8757 processPosition(mapper, 200, 200);
8758 processSync(mapper);
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8760 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8761}
8762
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008763TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008764 prepareAxes(POSITION);
8765 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008766 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008767 // Unrotated video frame
8768 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8769 NotifyMotionArgs motionArgs;
8770
8771 // Test all 4 orientations
8772 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008773 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8774 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8775 clearViewports();
8776 prepareDisplay(orientation);
8777 std::vector<TouchVideoFrame> frames{frame};
8778 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8779 processPosition(mapper, 100, 200);
8780 processSync(mapper);
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8782 ASSERT_EQ(frames, motionArgs.videoFrames);
8783 }
8784}
8785
8786TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8787 prepareAxes(POSITION);
8788 addConfigurationProperty("touch.deviceType", "touchScreen");
8789 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8790 // orientation-aware are affected by display rotation.
8791 addConfigurationProperty("touch.orientationAware", "0");
8792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8793 // 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,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008799 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8800 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8801 clearViewports();
8802 prepareDisplay(orientation);
8803 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008804 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008805 processPosition(mapper, 100, 200);
8806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008808 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8809 // compared to the display. This is so that when the window transform (which contains the
8810 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8811 // window's coordinate space.
8812 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008813 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008814
8815 // Release finger.
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008818 }
8819}
8820
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008821TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008822 prepareAxes(POSITION);
8823 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008824 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008825 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8826 // so mix these.
8827 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8828 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8829 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8830 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8831 NotifyMotionArgs motionArgs;
8832
8833 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008834 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008835 processPosition(mapper, 100, 200);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008838 ASSERT_EQ(frames, motionArgs.videoFrames);
8839}
8840
8841TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8842 prepareAxes(POSITION);
8843 addConfigurationProperty("touch.deviceType", "touchScreen");
8844 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8845 // orientation-aware are affected by display rotation.
8846 addConfigurationProperty("touch.orientationAware", "0");
8847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8848 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8849 // so mix these.
8850 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8851 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8852 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8853 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8854 NotifyMotionArgs motionArgs;
8855
8856 prepareDisplay(DISPLAY_ORIENTATION_90);
8857 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8858 processPosition(mapper, 100, 200);
8859 processSync(mapper);
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8861 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8862 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8863 // compared to the display. This is so that when the window transform (which contains the
8864 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8865 // window's coordinate space.
8866 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8867 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008868 ASSERT_EQ(frames, motionArgs.videoFrames);
8869}
8870
Arthur Hung9da14732019-09-02 16:16:58 +08008871/**
8872 * If we had defined port associations, but the viewport is not ready, the touch device would be
8873 * expected to be disabled, and it should be enabled after the viewport has found.
8874 */
8875TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008876 constexpr uint8_t hdmi2 = 1;
8877 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008878 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008879
8880 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8881
8882 addConfigurationProperty("touch.deviceType", "touchScreen");
8883 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008885
8886 ASSERT_EQ(mDevice->isEnabled(), false);
8887
8888 // Add display on hdmi2, the device should be enabled and can receive touch event.
8889 prepareSecondaryDisplay(type, hdmi2);
8890 ASSERT_EQ(mDevice->isEnabled(), true);
8891
8892 // Send a touch event.
8893 processPosition(mapper, 100, 100);
8894 processSync(mapper);
8895
8896 NotifyMotionArgs args;
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8898 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8899}
8900
Arthur Hung421eb1c2020-01-16 00:09:42 +08008901TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008902 addConfigurationProperty("touch.deviceType", "touchScreen");
8903 prepareDisplay(DISPLAY_ORIENTATION_0);
8904 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008905 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008906
8907 NotifyMotionArgs motionArgs;
8908
8909 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8910 // finger down
8911 processId(mapper, 1);
8912 processPosition(mapper, x1, y1);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8915 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8917
8918 // finger move
8919 processId(mapper, 1);
8920 processPosition(mapper, x2, y2);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8925
8926 // finger up.
8927 processId(mapper, -1);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8932
8933 // new finger down
8934 processId(mapper, 1);
8935 processPosition(mapper, x3, y3);
8936 processSync(mapper);
8937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8940}
8941
8942/**
arthurhungcc7f9802020-04-30 17:55:40 +08008943 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8944 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008945 */
arthurhungcc7f9802020-04-30 17:55:40 +08008946TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008947 addConfigurationProperty("touch.deviceType", "touchScreen");
8948 prepareDisplay(DISPLAY_ORIENTATION_0);
8949 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008951
8952 NotifyMotionArgs motionArgs;
8953
8954 // default tool type is finger
8955 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008956 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008957 processPosition(mapper, x1, y1);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8962
8963 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8964 processToolType(mapper, MT_TOOL_PALM);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8968
8969 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008970 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008971 processPosition(mapper, x2, y2);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8974
8975 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008976 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8979
8980 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008981 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008982 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008983 processPosition(mapper, x3, y3);
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
arthurhungbf89a482020-04-17 17:37:55 +08008990/**
arthurhungcc7f9802020-04-30 17:55:40 +08008991 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8992 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008993 */
arthurhungcc7f9802020-04-30 17:55:40 +08008994TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008995 addConfigurationProperty("touch.deviceType", "touchScreen");
8996 prepareDisplay(DISPLAY_ORIENTATION_0);
8997 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8998 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8999
9000 NotifyMotionArgs motionArgs;
9001
9002 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009003 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9004 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009005 processPosition(mapper, x1, y1);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9008 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9010
9011 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009012 processSlot(mapper, SECOND_SLOT);
9013 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009014 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009017 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9019
9020 // If the tool type of the first finger changes to MT_TOOL_PALM,
9021 // we expect to receive ACTION_POINTER_UP with cancel flag.
9022 processSlot(mapper, FIRST_SLOT);
9023 processId(mapper, FIRST_TRACKING_ID);
9024 processToolType(mapper, MT_TOOL_PALM);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009027 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009028 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9029
9030 // The following MOVE events of second finger should be processed.
9031 processSlot(mapper, SECOND_SLOT);
9032 processId(mapper, SECOND_TRACKING_ID);
9033 processPosition(mapper, x2 + 1, y2 + 1);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9037 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9038
9039 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9040 // it. Second finger receive move.
9041 processSlot(mapper, FIRST_SLOT);
9042 processId(mapper, INVALID_TRACKING_ID);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9046 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9047
9048 // Second finger keeps moving.
9049 processSlot(mapper, SECOND_SLOT);
9050 processId(mapper, SECOND_TRACKING_ID);
9051 processPosition(mapper, x2 + 2, y2 + 2);
9052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9055 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9056
9057 // Second finger up.
9058 processId(mapper, INVALID_TRACKING_ID);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9062 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9063}
9064
9065/**
9066 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9067 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9068 */
9069TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9070 addConfigurationProperty("touch.deviceType", "touchScreen");
9071 prepareDisplay(DISPLAY_ORIENTATION_0);
9072 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9073 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9074
9075 NotifyMotionArgs motionArgs;
9076
9077 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9078 // First finger down.
9079 processId(mapper, FIRST_TRACKING_ID);
9080 processPosition(mapper, x1, y1);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9085
9086 // Second finger down.
9087 processSlot(mapper, SECOND_SLOT);
9088 processId(mapper, SECOND_TRACKING_ID);
9089 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009092 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9094
arthurhungcc7f9802020-04-30 17:55:40 +08009095 // If the tool type of the first finger changes to MT_TOOL_PALM,
9096 // we expect to receive ACTION_POINTER_UP with cancel flag.
9097 processSlot(mapper, FIRST_SLOT);
9098 processId(mapper, FIRST_TRACKING_ID);
9099 processToolType(mapper, MT_TOOL_PALM);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009102 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009103 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9104
9105 // Second finger keeps moving.
9106 processSlot(mapper, SECOND_SLOT);
9107 processId(mapper, SECOND_TRACKING_ID);
9108 processPosition(mapper, x2 + 1, y2 + 1);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9112
9113 // second finger becomes palm, receive cancel due to only 1 finger is active.
9114 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009115 processToolType(mapper, MT_TOOL_PALM);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9119
arthurhungcc7f9802020-04-30 17:55:40 +08009120 // third finger down.
9121 processSlot(mapper, THIRD_SLOT);
9122 processId(mapper, THIRD_TRACKING_ID);
9123 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009124 processPosition(mapper, x3, y3);
9125 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9127 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9130
9131 // third finger move
9132 processId(mapper, THIRD_TRACKING_ID);
9133 processPosition(mapper, x3 + 1, y3 + 1);
9134 processSync(mapper);
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9137
9138 // first finger up, third finger receive move.
9139 processSlot(mapper, FIRST_SLOT);
9140 processId(mapper, INVALID_TRACKING_ID);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9144 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9145
9146 // second finger up, third finger receive move.
9147 processSlot(mapper, SECOND_SLOT);
9148 processId(mapper, INVALID_TRACKING_ID);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9152 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9153
9154 // third finger up.
9155 processSlot(mapper, THIRD_SLOT);
9156 processId(mapper, INVALID_TRACKING_ID);
9157 processSync(mapper);
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9159 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9160 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9161}
9162
9163/**
9164 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9165 * and the active finger could still be allowed to receive the events
9166 */
9167TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9168 addConfigurationProperty("touch.deviceType", "touchScreen");
9169 prepareDisplay(DISPLAY_ORIENTATION_0);
9170 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9172
9173 NotifyMotionArgs motionArgs;
9174
9175 // default tool type is finger
9176 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9177 processId(mapper, FIRST_TRACKING_ID);
9178 processPosition(mapper, x1, y1);
9179 processSync(mapper);
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9183
9184 // Second finger down.
9185 processSlot(mapper, SECOND_SLOT);
9186 processId(mapper, SECOND_TRACKING_ID);
9187 processPosition(mapper, x2, y2);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009190 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9192
9193 // If the tool type of the second finger changes to MT_TOOL_PALM,
9194 // we expect to receive ACTION_POINTER_UP with cancel flag.
9195 processId(mapper, SECOND_TRACKING_ID);
9196 processToolType(mapper, MT_TOOL_PALM);
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009199 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009200 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9201
9202 // The following MOVE event should be processed.
9203 processSlot(mapper, FIRST_SLOT);
9204 processId(mapper, FIRST_TRACKING_ID);
9205 processPosition(mapper, x1 + 1, y1 + 1);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9209 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9210
9211 // second finger up.
9212 processSlot(mapper, SECOND_SLOT);
9213 processId(mapper, INVALID_TRACKING_ID);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9217
9218 // first finger keep moving
9219 processSlot(mapper, FIRST_SLOT);
9220 processId(mapper, FIRST_TRACKING_ID);
9221 processPosition(mapper, x1 + 2, y1 + 2);
9222 processSync(mapper);
9223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9225
9226 // first finger up.
9227 processId(mapper, INVALID_TRACKING_ID);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9231 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009232}
9233
Arthur Hung9ad18942021-06-19 02:04:46 +00009234/**
9235 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9236 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9237 * cause slot be valid again.
9238 */
9239TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9240 addConfigurationProperty("touch.deviceType", "touchScreen");
9241 prepareDisplay(DISPLAY_ORIENTATION_0);
9242 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9243 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9244
9245 NotifyMotionArgs motionArgs;
9246
9247 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9248 // First finger down.
9249 processId(mapper, FIRST_TRACKING_ID);
9250 processPosition(mapper, x1, y1);
9251 processPressure(mapper, RAW_PRESSURE_MAX);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9255 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9256
9257 // First finger move.
9258 processId(mapper, FIRST_TRACKING_ID);
9259 processPosition(mapper, x1 + 1, y1 + 1);
9260 processPressure(mapper, RAW_PRESSURE_MAX);
9261 processSync(mapper);
9262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9264 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9265
9266 // Second finger down.
9267 processSlot(mapper, SECOND_SLOT);
9268 processId(mapper, SECOND_TRACKING_ID);
9269 processPosition(mapper, x2, y2);
9270 processPressure(mapper, RAW_PRESSURE_MAX);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009273 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009274 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9275
9276 // second finger up with some unexpected data.
9277 processSlot(mapper, SECOND_SLOT);
9278 processId(mapper, INVALID_TRACKING_ID);
9279 processPosition(mapper, x2, y2);
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009282 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009283 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9284
9285 // first finger up with some unexpected data.
9286 processSlot(mapper, FIRST_SLOT);
9287 processId(mapper, INVALID_TRACKING_ID);
9288 processPosition(mapper, x2, y2);
9289 processPressure(mapper, RAW_PRESSURE_MAX);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9293 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9294}
9295
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009296// --- MultiTouchInputMapperTest_ExternalDevice ---
9297
9298class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9299protected:
Chris Yea52ade12020-08-27 16:49:20 -07009300 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009301};
9302
9303/**
9304 * Expect fallback to internal viewport if device is external and external viewport is not present.
9305 */
9306TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9307 prepareAxes(POSITION);
9308 addConfigurationProperty("touch.deviceType", "touchScreen");
9309 prepareDisplay(DISPLAY_ORIENTATION_0);
9310 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9311
9312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9313
9314 NotifyMotionArgs motionArgs;
9315
9316 // Expect the event to be sent to the internal viewport,
9317 // because an external viewport is not present.
9318 processPosition(mapper, 100, 100);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9322
9323 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009324 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009325 processPosition(mapper, 100, 100);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9329}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009330
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009331TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9332 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9333 std::shared_ptr<FakePointerController> fakePointerController =
9334 std::make_shared<FakePointerController>();
9335 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9336 fakePointerController->setPosition(0, 0);
9337 fakePointerController->setButtonState(0);
9338
9339 // prepare device and capture
9340 prepareDisplay(DISPLAY_ORIENTATION_0);
9341 prepareAxes(POSITION | ID | SLOT);
9342 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9343 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9344 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009345 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009346 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9347
9348 // captured touchpad should be a touchpad source
9349 NotifyDeviceResetArgs resetArgs;
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9351 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9352
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009353 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009354
9355 const InputDeviceInfo::MotionRange* relRangeX =
9356 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9357 ASSERT_NE(relRangeX, nullptr);
9358 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9359 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9360 const InputDeviceInfo::MotionRange* relRangeY =
9361 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9362 ASSERT_NE(relRangeY, nullptr);
9363 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9364 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9365
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009366 // run captured pointer tests - note that this is unscaled, so input listener events should be
9367 // identical to what the hardware sends (accounting for any
9368 // calibration).
9369 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009370 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009371 processId(mapper, 1);
9372 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9373 processKey(mapper, BTN_TOUCH, 1);
9374 processSync(mapper);
9375
9376 // expect coord[0] to contain initial location of touch 0
9377 NotifyMotionArgs args;
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9380 ASSERT_EQ(1U, args.pointerCount);
9381 ASSERT_EQ(0, args.pointerProperties[0].id);
9382 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9383 ASSERT_NO_FATAL_FAILURE(
9384 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9385
9386 // FINGER 1 DOWN
9387 processSlot(mapper, 1);
9388 processId(mapper, 2);
9389 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9390 processSync(mapper);
9391
9392 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009394 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009395 ASSERT_EQ(2U, args.pointerCount);
9396 ASSERT_EQ(0, args.pointerProperties[0].id);
9397 ASSERT_EQ(1, args.pointerProperties[1].id);
9398 ASSERT_NO_FATAL_FAILURE(
9399 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9400 ASSERT_NO_FATAL_FAILURE(
9401 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9402
9403 // FINGER 1 MOVE
9404 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9405 processSync(mapper);
9406
9407 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9408 // from move
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9411 ASSERT_NO_FATAL_FAILURE(
9412 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9413 ASSERT_NO_FATAL_FAILURE(
9414 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9415
9416 // FINGER 0 MOVE
9417 processSlot(mapper, 0);
9418 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9419 processSync(mapper);
9420
9421 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9424 ASSERT_NO_FATAL_FAILURE(
9425 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9426 ASSERT_NO_FATAL_FAILURE(
9427 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9428
9429 // BUTTON DOWN
9430 processKey(mapper, BTN_LEFT, 1);
9431 processSync(mapper);
9432
9433 // touchinputmapper design sends a move before button press
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9438
9439 // BUTTON UP
9440 processKey(mapper, BTN_LEFT, 0);
9441 processSync(mapper);
9442
9443 // touchinputmapper design sends a move after button release
9444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9445 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9448
9449 // FINGER 0 UP
9450 processId(mapper, -1);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9454
9455 // FINGER 1 MOVE
9456 processSlot(mapper, 1);
9457 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9458 processSync(mapper);
9459
9460 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9463 ASSERT_EQ(1U, args.pointerCount);
9464 ASSERT_EQ(1, args.pointerProperties[0].id);
9465 ASSERT_NO_FATAL_FAILURE(
9466 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9467
9468 // FINGER 1 UP
9469 processId(mapper, -1);
9470 processKey(mapper, BTN_TOUCH, 0);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9474
9475 // non captured touchpad should be a mouse source
9476 mFakePolicy->setPointerCapture(false);
9477 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9479 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9480}
9481
9482TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9483 std::shared_ptr<FakePointerController> fakePointerController =
9484 std::make_shared<FakePointerController>();
9485 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9486 fakePointerController->setPosition(0, 0);
9487 fakePointerController->setButtonState(0);
9488
9489 // prepare device and capture
9490 prepareDisplay(DISPLAY_ORIENTATION_0);
9491 prepareAxes(POSITION | ID | SLOT);
9492 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009494 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009495 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9496 // run uncaptured pointer tests - pushes out generic events
9497 // FINGER 0 DOWN
9498 processId(mapper, 3);
9499 processPosition(mapper, 100, 100);
9500 processKey(mapper, BTN_TOUCH, 1);
9501 processSync(mapper);
9502
9503 // start at (100,100), cursor should be at (0,0) * scale
9504 NotifyMotionArgs args;
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9507 ASSERT_NO_FATAL_FAILURE(
9508 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9509
9510 // FINGER 0 MOVE
9511 processPosition(mapper, 200, 200);
9512 processSync(mapper);
9513
9514 // compute scaling to help with touch position checking
9515 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9516 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9517 float scale =
9518 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9519
9520 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9524 0, 0, 0, 0, 0, 0, 0));
9525}
9526
9527TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9528 std::shared_ptr<FakePointerController> fakePointerController =
9529 std::make_shared<FakePointerController>();
9530
9531 prepareDisplay(DISPLAY_ORIENTATION_0);
9532 prepareAxes(POSITION | ID | SLOT);
9533 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009534 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009535 mFakePolicy->setPointerCapture(false);
9536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9537
9538 // uncaptured touchpad should be a pointer device
9539 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9540
9541 // captured touchpad should be a touchpad device
9542 mFakePolicy->setPointerCapture(true);
9543 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9544 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9545}
9546
HQ Liue6983c72022-04-19 22:14:56 +00009547class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9548protected:
9549 float mPointerMovementScale;
9550 float mPointerXZoomScale;
9551 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9552 addConfigurationProperty("touch.deviceType", "pointer");
9553 std::shared_ptr<FakePointerController> fakePointerController =
9554 std::make_shared<FakePointerController>();
9555 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9556 fakePointerController->setPosition(0, 0);
9557 fakePointerController->setButtonState(0);
9558 prepareDisplay(DISPLAY_ORIENTATION_0);
9559
9560 prepareAxes(POSITION);
9561 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9562 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9563 // needs to be disabled, and the pointer gesture needs to be enabled.
9564 mFakePolicy->setPointerCapture(false);
9565 mFakePolicy->setPointerGestureEnabled(true);
9566 mFakePolicy->setPointerController(fakePointerController);
9567
9568 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9569 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9570 mPointerMovementScale =
9571 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9572 mPointerXZoomScale =
9573 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9574 }
9575
9576 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9577 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9578 /*flat*/ 0,
9579 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9580 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9581 /*flat*/ 0,
9582 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9583 }
9584};
9585
9586/**
9587 * Two fingers down on a pointer mode touch pad. The width
9588 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9589 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9590 * be greater than the both value to be freeform gesture, so that after two
9591 * fingers start to move downwards, the gesture should be swipe.
9592 */
9593TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9594 // The min freeform gesture width is 25units/mm x 30mm = 750
9595 // which is greater than fraction of the diagnal length of the touchpad (349).
9596 // Thus, MaxSwipWidth is 750.
9597 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9598 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9599 NotifyMotionArgs motionArgs;
9600
9601 // Two fingers down at once.
9602 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9603 // Pointer's initial position is used the [0,0] coordinate.
9604 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9605
9606 processId(mapper, FIRST_TRACKING_ID);
9607 processPosition(mapper, x1, y1);
9608 processMTSync(mapper);
9609 processId(mapper, SECOND_TRACKING_ID);
9610 processPosition(mapper, x2, y2);
9611 processMTSync(mapper);
9612 processSync(mapper);
9613
9614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9615 ASSERT_EQ(1U, motionArgs.pointerCount);
9616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9618 ASSERT_NO_FATAL_FAILURE(
9619 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9620
9621 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9622 // that there should be 1 pointer.
9623 int32_t movingDistance = 200;
9624 y1 += movingDistance;
9625 y2 += movingDistance;
9626
9627 processId(mapper, FIRST_TRACKING_ID);
9628 processPosition(mapper, x1, y1);
9629 processMTSync(mapper);
9630 processId(mapper, SECOND_TRACKING_ID);
9631 processPosition(mapper, x2, y2);
9632 processMTSync(mapper);
9633 processSync(mapper);
9634
9635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9636 ASSERT_EQ(1U, motionArgs.pointerCount);
9637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9640 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9641 0, 0, 0, 0));
9642}
9643
9644/**
9645 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9646 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9647 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9648 * value to be freeform gesture, so that after two fingers start to move downwards,
9649 * the gesture should be swipe.
9650 */
9651TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9652 // The min freeform gesture width is 5units/mm x 30mm = 150
9653 // which is greater than fraction of the diagnal length of the touchpad (349).
9654 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9655 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9656 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9657 NotifyMotionArgs motionArgs;
9658
9659 // Two fingers down at once.
9660 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9661 // Pointer's initial position is used the [0,0] coordinate.
9662 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9663
9664 processId(mapper, FIRST_TRACKING_ID);
9665 processPosition(mapper, x1, y1);
9666 processMTSync(mapper);
9667 processId(mapper, SECOND_TRACKING_ID);
9668 processPosition(mapper, x2, y2);
9669 processMTSync(mapper);
9670 processSync(mapper);
9671
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9673 ASSERT_EQ(1U, motionArgs.pointerCount);
9674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9676 ASSERT_NO_FATAL_FAILURE(
9677 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9678
9679 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9680 // and there should be 1 pointer.
9681 int32_t movingDistance = 200;
9682 y1 += movingDistance;
9683 y2 += movingDistance;
9684
9685 processId(mapper, FIRST_TRACKING_ID);
9686 processPosition(mapper, x1, y1);
9687 processMTSync(mapper);
9688 processId(mapper, SECOND_TRACKING_ID);
9689 processPosition(mapper, x2, y2);
9690 processMTSync(mapper);
9691 processSync(mapper);
9692
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 ASSERT_EQ(1U, motionArgs.pointerCount);
9695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9697 // New coordinate is the scaled relative coordinate from the initial coordinate.
9698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9699 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9700 0, 0, 0, 0));
9701}
9702
9703/**
9704 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9705 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9706 * freeform gestures after two fingers start to move downwards.
9707 */
9708TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9709 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9711
9712 NotifyMotionArgs motionArgs;
9713
9714 // Two fingers down at once. Wider than the max swipe width.
9715 // The gesture is expected to be PRESS, then transformed to FREEFORM
9716 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9717
9718 processId(mapper, FIRST_TRACKING_ID);
9719 processPosition(mapper, x1, y1);
9720 processMTSync(mapper);
9721 processId(mapper, SECOND_TRACKING_ID);
9722 processPosition(mapper, x2, y2);
9723 processMTSync(mapper);
9724 processSync(mapper);
9725
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(1U, motionArgs.pointerCount);
9728 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9730 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9731 ASSERT_NO_FATAL_FAILURE(
9732 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9733
9734 int32_t movingDistance = 200;
9735
9736 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9737 // then two down events for two pointers.
9738 y1 += movingDistance;
9739 y2 += movingDistance;
9740
9741 processId(mapper, FIRST_TRACKING_ID);
9742 processPosition(mapper, x1, y1);
9743 processMTSync(mapper);
9744 processId(mapper, SECOND_TRACKING_ID);
9745 processPosition(mapper, x2, y2);
9746 processMTSync(mapper);
9747 processSync(mapper);
9748
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9750 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9751 ASSERT_EQ(1U, motionArgs.pointerCount);
9752 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9755 ASSERT_EQ(1U, motionArgs.pointerCount);
9756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9759 ASSERT_EQ(2U, motionArgs.pointerCount);
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9762 // Two pointers' scaled relative coordinates from their initial centroid.
9763 // Initial y coordinates are 0 as y1 and y2 have the same value.
9764 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9765 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9766 // When pointers move, the new coordinates equal to the initial coordinates plus
9767 // scaled moving distance.
9768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9769 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9770 0, 0, 0, 0));
9771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9772 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9773 0, 0, 0, 0));
9774
9775 // Move two fingers down again, expect one MOVE motion event.
9776 y1 += movingDistance;
9777 y2 += movingDistance;
9778
9779 processId(mapper, FIRST_TRACKING_ID);
9780 processPosition(mapper, x1, y1);
9781 processMTSync(mapper);
9782 processId(mapper, SECOND_TRACKING_ID);
9783 processPosition(mapper, x2, y2);
9784 processMTSync(mapper);
9785 processSync(mapper);
9786
9787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9788 ASSERT_EQ(2U, motionArgs.pointerCount);
9789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9792 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9793 0, 0, 0, 0, 0));
9794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9795 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9796 0, 0, 0, 0, 0));
9797}
9798
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009799// --- JoystickInputMapperTest ---
9800
9801class JoystickInputMapperTest : public InputMapperTest {
9802protected:
9803 static const int32_t RAW_X_MIN;
9804 static const int32_t RAW_X_MAX;
9805 static const int32_t RAW_Y_MIN;
9806 static const int32_t RAW_Y_MAX;
9807
9808 void SetUp() override {
9809 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9810 }
9811 void prepareAxes() {
9812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9814 }
9815
9816 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9818 }
9819
9820 void processSync(JoystickInputMapper& mapper) {
9821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9822 }
9823
9824 void prepareVirtualDisplay(int32_t orientation) {
9825 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9826 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9827 NO_PORT, ViewportType::VIRTUAL);
9828 }
9829};
9830
9831const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9832const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9833const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9834const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9835
9836TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9837 prepareAxes();
9838 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9839
9840 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9841
9842 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9843
9844 // Send an axis event
9845 processAxis(mapper, ABS_X, 100);
9846 processSync(mapper);
9847
9848 NotifyMotionArgs args;
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9850 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9851
9852 // Send another axis event
9853 processAxis(mapper, ABS_Y, 100);
9854 processSync(mapper);
9855
9856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9857 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9858}
9859
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009860// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009861
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009862class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009863protected:
9864 static const char* DEVICE_NAME;
9865 static const char* DEVICE_LOCATION;
9866 static const int32_t DEVICE_ID;
9867 static const int32_t DEVICE_GENERATION;
9868 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009869 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009870 static const int32_t EVENTHUB_ID;
9871
9872 std::shared_ptr<FakeEventHub> mFakeEventHub;
9873 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009874 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009875 std::unique_ptr<InstrumentedInputReader> mReader;
9876 std::shared_ptr<InputDevice> mDevice;
9877
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009878 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009879 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009880 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009881 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009882 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009883 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009884 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9885 }
9886
9887 void SetUp() override { SetUp(DEVICE_CLASSES); }
9888
9889 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009890 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009891 mFakePolicy.clear();
9892 }
9893
9894 void configureDevice(uint32_t changes) {
9895 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9896 mReader->requestRefreshConfiguration(changes);
9897 mReader->loopOnce();
9898 }
9899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9900 }
9901
9902 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9903 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009904 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009905 InputDeviceIdentifier identifier;
9906 identifier.name = name;
9907 identifier.location = location;
9908 std::shared_ptr<InputDevice> device =
9909 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9910 identifier);
9911 mReader->pushNextDevice(device);
9912 mFakeEventHub->addDevice(eventHubId, name, classes);
9913 mReader->loopOnce();
9914 return device;
9915 }
9916
9917 template <class T, typename... Args>
9918 T& addControllerAndConfigure(Args... args) {
9919 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9920
9921 return controller;
9922 }
9923};
9924
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009925const char* PeripheralControllerTest::DEVICE_NAME = "device";
9926const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9927const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9928const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9929const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009930const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9931 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009932const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009933
9934// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009935class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009936protected:
9937 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009938 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009939 }
9940};
9941
9942TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009943 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009944
9945 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9946 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9947}
9948
9949TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009950 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009951
9952 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9953 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9954}
9955
9956// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009957class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009958protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009959 void SetUp() override {
9960 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9961 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962};
9963
Chris Ye85758332021-05-16 23:05:17 -07009964TEST_F(LightControllerTest, MonoLight) {
9965 RawLightInfo infoMono = {.id = 1,
9966 .name = "Mono",
9967 .maxBrightness = 255,
9968 .flags = InputLightClass::BRIGHTNESS,
9969 .path = ""};
9970 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009971
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009972 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009973 InputDeviceInfo info;
9974 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009975 std::vector<InputDeviceLightInfo> lights = info.getLights();
9976 ASSERT_EQ(1U, lights.size());
9977 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009978
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009979 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9980 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009981}
9982
9983TEST_F(LightControllerTest, RGBLight) {
9984 RawLightInfo infoRed = {.id = 1,
9985 .name = "red",
9986 .maxBrightness = 255,
9987 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9988 .path = ""};
9989 RawLightInfo infoGreen = {.id = 2,
9990 .name = "green",
9991 .maxBrightness = 255,
9992 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9993 .path = ""};
9994 RawLightInfo infoBlue = {.id = 3,
9995 .name = "blue",
9996 .maxBrightness = 255,
9997 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9998 .path = ""};
9999 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10000 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10001 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10002
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010003 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010004 InputDeviceInfo info;
10005 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010006 std::vector<InputDeviceLightInfo> lights = info.getLights();
10007 ASSERT_EQ(1U, lights.size());
10008 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010009
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010010 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10011 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010012}
10013
10014TEST_F(LightControllerTest, MultiColorRGBLight) {
10015 RawLightInfo infoColor = {.id = 1,
10016 .name = "red",
10017 .maxBrightness = 255,
10018 .flags = InputLightClass::BRIGHTNESS |
10019 InputLightClass::MULTI_INTENSITY |
10020 InputLightClass::MULTI_INDEX,
10021 .path = ""};
10022
10023 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10024
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010025 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010026 InputDeviceInfo info;
10027 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010028 std::vector<InputDeviceLightInfo> lights = info.getLights();
10029 ASSERT_EQ(1U, lights.size());
10030 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010031
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010032 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10033 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010034}
10035
10036TEST_F(LightControllerTest, PlayerIdLight) {
10037 RawLightInfo info1 = {.id = 1,
10038 .name = "player1",
10039 .maxBrightness = 255,
10040 .flags = InputLightClass::BRIGHTNESS,
10041 .path = ""};
10042 RawLightInfo info2 = {.id = 2,
10043 .name = "player2",
10044 .maxBrightness = 255,
10045 .flags = InputLightClass::BRIGHTNESS,
10046 .path = ""};
10047 RawLightInfo info3 = {.id = 3,
10048 .name = "player3",
10049 .maxBrightness = 255,
10050 .flags = InputLightClass::BRIGHTNESS,
10051 .path = ""};
10052 RawLightInfo info4 = {.id = 4,
10053 .name = "player4",
10054 .maxBrightness = 255,
10055 .flags = InputLightClass::BRIGHTNESS,
10056 .path = ""};
10057 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10058 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10059 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10060 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10061
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010062 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010063 InputDeviceInfo info;
10064 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010065 std::vector<InputDeviceLightInfo> lights = info.getLights();
10066 ASSERT_EQ(1U, lights.size());
10067 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010068
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010069 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10070 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10071 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010072}
10073
Michael Wrightd02c5b62014-02-10 15:10:22 -080010074} // namespace android