blob: dcd8521250389713c748d116997e104097416b61 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
HQ Liue6983c72022-04-19 22:14:56 +0000376 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
377
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800378 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
379
HQ Liue6983c72022-04-19 22:14:56 +0000380 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
381
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000382 void setVelocityControlParams(const VelocityControlParameters& params) {
383 mConfig.pointerVelocityControlParameters = params;
384 mConfig.wheelVelocityControlParameters = params;
385 }
386
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000388 uint32_t mNextPointerCaptureSequenceNumber = 0;
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 *outConfig = mConfig;
392 }
393
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000394 std::shared_ptr<PointerControllerInterface> obtainPointerController(
395 int32_t /*deviceId*/) override {
396 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398
Chris Yea52ade12020-08-27 16:49:20 -0700399 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 mInputDevicesChanged = true;
403 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
407 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700408 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 }
410
Chris Yea52ade12020-08-27 16:49:20 -0700411 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800412
413 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
414 std::unique_lock<std::mutex> lock(mLock);
415 base::ScopedLockAssertion assumeLocked(mLock);
416
417 const bool devicesChanged =
418 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
419 return mInputDevicesChanged;
420 });
421 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
422 mInputDevicesChanged = false;
423 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424};
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426// --- FakeEventHub ---
427
428class FakeEventHub : public EventHubInterface {
429 struct KeyInfo {
430 int32_t keyCode;
431 uint32_t flags;
432 };
433
Chris Yef59a2f42020-10-16 12:55:26 -0700434 struct SensorInfo {
435 InputDeviceSensorType sensorType;
436 int32_t sensorDataIndex;
437 };
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 struct Device {
440 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700441 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 PropertyMap configuration;
443 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
444 KeyedVector<int, bool> relativeAxes;
445 KeyedVector<int32_t, int32_t> keyCodeStates;
446 KeyedVector<int32_t, int32_t> scanCodeStates;
447 KeyedVector<int32_t, int32_t> switchStates;
448 KeyedVector<int32_t, int32_t> absoluteAxisValue;
449 KeyedVector<int32_t, KeyInfo> keysByScanCode;
450 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
451 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100452 // fake mapping which would normally come from keyCharacterMap
453 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700454 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
455 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800456 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700457 bool enabled;
458
459 status_t enable() {
460 enabled = true;
461 return OK;
462 }
463
464 status_t disable() {
465 enabled = false;
466 return OK;
467 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700469 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 };
471
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700472 std::mutex mLock;
473 std::condition_variable mEventsCondition;
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100476 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000477 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600478 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000479 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800480 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
481 // Simulates a device light brightness, from light id to light brightness.
482 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
483 // Simulates a device light intensities, from light id to light intensities map.
484 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
485 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700487public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 virtual ~FakeEventHub() {
489 for (size_t i = 0; i < mDevices.size(); i++) {
490 delete mDevices.valueAt(i);
491 }
492 }
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 FakeEventHub() { }
495
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700496 void addDevice(int32_t deviceId, const std::string& name,
497 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 Device* device = new Device(classes);
499 device->identifier.name = name;
500 mDevices.add(deviceId, device);
501
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000502 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
505 void removeDevice(int32_t deviceId) {
506 delete mDevices.valueFor(deviceId);
507 mDevices.removeItem(deviceId);
508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000509 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 }
511
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 bool isDeviceEnabled(int32_t deviceId) {
513 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700514 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700515 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
516 return false;
517 }
518 return device->enabled;
519 }
520
521 status_t enableDevice(int32_t deviceId) {
522 status_t result;
523 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700524 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700525 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
526 return BAD_VALUE;
527 }
528 if (device->enabled) {
529 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
530 return OK;
531 }
532 result = device->enable();
533 return result;
534 }
535
536 status_t disableDevice(int32_t deviceId) {
537 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700538 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
540 return BAD_VALUE;
541 }
542 if (!device->enabled) {
543 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
544 return OK;
545 }
546 return device->disable();
547 }
548
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000550 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 }
552
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700553 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 Device* device = getDevice(deviceId);
555 device->configuration.addProperty(key, value);
556 }
557
558 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
559 Device* device = getDevice(deviceId);
560 device->configuration.addAll(configuration);
561 }
562
563 void addAbsoluteAxis(int32_t deviceId, int axis,
564 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
565 Device* device = getDevice(deviceId);
566
567 RawAbsoluteAxisInfo info;
568 info.valid = true;
569 info.minValue = minValue;
570 info.maxValue = maxValue;
571 info.flat = flat;
572 info.fuzz = fuzz;
573 info.resolution = resolution;
574 device->absoluteAxes.add(axis, info);
575 }
576
577 void addRelativeAxis(int32_t deviceId, int32_t axis) {
578 Device* device = getDevice(deviceId);
579 device->relativeAxes.add(axis, true);
580 }
581
582 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
583 Device* device = getDevice(deviceId);
584 device->keyCodeStates.replaceValueFor(keyCode, state);
585 }
586
587 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
588 Device* device = getDevice(deviceId);
589 device->scanCodeStates.replaceValueFor(scanCode, state);
590 }
591
592 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
593 Device* device = getDevice(deviceId);
594 device->switchStates.replaceValueFor(switchCode, state);
595 }
596
597 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
598 Device* device = getDevice(deviceId);
599 device->absoluteAxisValue.replaceValueFor(axis, value);
600 }
601
602 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
603 int32_t keyCode, uint32_t flags) {
604 Device* device = getDevice(deviceId);
605 KeyInfo info;
606 info.keyCode = keyCode;
607 info.flags = flags;
608 if (scanCode) {
609 device->keysByScanCode.add(scanCode, info);
610 }
611 if (usageCode) {
612 device->keysByUsageCode.add(usageCode, info);
613 }
614 }
615
Philip Junker4af3b3d2021-12-14 10:36:55 +0100616 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
617 Device* device = getDevice(deviceId);
618 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
619 }
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 void addLed(int32_t deviceId, int32_t led, bool initialState) {
622 Device* device = getDevice(deviceId);
623 device->leds.add(led, initialState);
624 }
625
Chris Yef59a2f42020-10-16 12:55:26 -0700626 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
627 int32_t sensorDataIndex) {
628 Device* device = getDevice(deviceId);
629 SensorInfo info;
630 info.sensorType = sensorType;
631 info.sensorDataIndex = sensorDataIndex;
632 device->sensorsByAbsCode.emplace(absCode, info);
633 }
634
635 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
636 Device* device = getDevice(deviceId);
637 typename BitArray<MSC_MAX>::Buffer buffer;
638 buffer[mscEvent / 32] = 1 << mscEvent % 32;
639 device->mscBitmask.loadFromBuffer(buffer);
640 }
641
Chris Ye3fdbfef2021-01-06 18:45:18 -0800642 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
643 mRawLightInfos.emplace(rawId, std::move(info));
644 }
645
646 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
647 mLightBrightness.emplace(rawId, brightness);
648 }
649
650 void fakeLightIntensities(int32_t rawId,
651 const std::unordered_map<LightColor, int32_t> intensities) {
652 mLightIntensities.emplace(rawId, std::move(intensities));
653 }
654
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 bool getLedState(int32_t deviceId, int32_t led) {
656 Device* device = getDevice(deviceId);
657 return device->leds.valueFor(led);
658 }
659
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 return mExcludedDevices;
662 }
663
664 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
665 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800666 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000669 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
670 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700671 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 RawEvent event;
673 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000674 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 event.deviceId = deviceId;
676 event.type = type;
677 event.code = code;
678 event.value = value;
679 mEvents.push_back(event);
680
681 if (type == EV_ABS) {
682 setAbsoluteAxisValue(deviceId, code, value);
683 }
684 }
685
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600686 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
687 std::vector<TouchVideoFrame>> videoFrames) {
688 mVideoFrames = std::move(videoFrames);
689 }
690
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700692 std::unique_lock<std::mutex> lock(mLock);
693 base::ScopedLockAssertion assumeLocked(mLock);
694 const bool queueIsEmpty =
695 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
696 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
697 if (!queueIsEmpty) {
698 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
702private:
703 Device* getDevice(int32_t deviceId) const {
704 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100705 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 }
707
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700708 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700710 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Chris Yea52ade12020-08-27 16:49:20 -0700713 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 Device* device = getDevice(deviceId);
715 return device ? device->identifier : InputDeviceIdentifier();
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
Chris Yea52ade12020-08-27 16:49:20 -0700720 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 *outConfiguration = device->configuration;
724 }
725 }
726
Chris Yea52ade12020-08-27 16:49:20 -0700727 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
728 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800730 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 ssize_t index = device->absoluteAxes.indexOfKey(axis);
732 if (index >= 0) {
733 *outAxisInfo = device->absoluteAxes.valueAt(index);
734 return OK;
735 }
736 }
737 outAxisInfo->clear();
738 return -1;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
743 if (device) {
744 return device->relativeAxes.indexOfKey(axis) >= 0;
745 }
746 return false;
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yef59a2f42020-10-16 12:55:26 -0700751 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
752 Device* device = getDevice(deviceId);
753 if (device) {
754 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
755 }
756 return false;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
760 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 Device* device = getDevice(deviceId);
762 if (device) {
763 const KeyInfo* key = getKey(device, scanCode, usageCode);
764 if (key) {
765 if (outKeycode) {
766 *outKeycode = key->keyCode;
767 }
768 if (outFlags) {
769 *outFlags = key->flags;
770 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700771 if (outMetaState) {
772 *outMetaState = metaState;
773 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 return OK;
775 }
776 }
777 return NAME_NOT_FOUND;
778 }
779
780 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
781 if (usageCode) {
782 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
783 if (index >= 0) {
784 return &device->keysByUsageCode.valueAt(index);
785 }
786 }
787 if (scanCode) {
788 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
789 if (index >= 0) {
790 return &device->keysByScanCode.valueAt(index);
791 }
792 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700793 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
Chris Yef59a2f42020-10-16 12:55:26 -0700798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
799 int32_t absCode) {
800 Device* device = getDevice(deviceId);
801 if (!device) {
802 return Errorf("Sensor device not found.");
803 }
804 auto it = device->sensorsByAbsCode.find(absCode);
805 if (it == device->sensorsByAbsCode.end()) {
806 return Errorf("Sensor map not found.");
807 }
808 const SensorInfo& info = it->second;
809 return std::make_pair(info.sensorType, info.sensorDataIndex);
810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 mExcludedDevices = devices;
814 }
815
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000816 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
817 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000819 const size_t filledSize = std::min(mEvents.size(), bufferSize);
820 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
821
822 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700823 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000824 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600828 auto it = mVideoFrames.find(deviceId);
829 if (it != mVideoFrames.end()) {
830 std::vector<TouchVideoFrame> frames = std::move(it->second);
831 mVideoFrames.erase(deviceId);
832 return frames;
833 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800834 return {};
835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 Device* device = getDevice(deviceId);
839 if (device) {
840 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
841 if (index >= 0) {
842 return device->scanCodeStates.valueAt(index);
843 }
844 }
845 return AKEY_STATE_UNKNOWN;
846 }
847
Chris Yea52ade12020-08-27 16:49:20 -0700848 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 Device* device = getDevice(deviceId);
850 if (device) {
851 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
852 if (index >= 0) {
853 return device->keyCodeStates.valueAt(index);
854 }
855 }
856 return AKEY_STATE_UNKNOWN;
857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 Device* device = getDevice(deviceId);
861 if (device) {
862 ssize_t index = device->switchStates.indexOfKey(sw);
863 if (index >= 0) {
864 return device->switchStates.valueAt(index);
865 }
866 }
867 return AKEY_STATE_UNKNOWN;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
871 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 Device* device = getDevice(deviceId);
873 if (device) {
874 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
875 if (index >= 0) {
876 *outValue = device->absoluteAxisValue.valueAt(index);
877 return OK;
878 }
879 }
880 *outValue = 0;
881 return -1;
882 }
883
Philip Junker4af3b3d2021-12-14 10:36:55 +0100884 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
885 Device* device = getDevice(deviceId);
886 if (!device) {
887 return AKEYCODE_UNKNOWN;
888 }
889 auto it = device->keyCodeMapping.find(locationKeyCode);
890 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700894 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700895 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 bool result = false;
897 Device* device = getDevice(deviceId);
898 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700899 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700900 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
902 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
903 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905 }
906 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
907 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
908 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 }
910 }
911 }
912 }
913 return result;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 if (device) {
919 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
920 return index >= 0;
921 }
922 return false;
923 }
924
Arthur Hungcb40a002021-08-03 14:31:01 +0000925 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
926 Device* device = getDevice(deviceId);
927 if (!device) {
928 return false;
929 }
930 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
931 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
932 return true;
933 }
934 }
935 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
936 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
937 return true;
938 }
939 }
940 return false;
941 }
942
Chris Yea52ade12020-08-27 16:49:20 -0700943 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 Device* device = getDevice(deviceId);
945 return device && device->leds.indexOfKey(led) >= 0;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 Device* device = getDevice(deviceId);
950 if (device) {
951 ssize_t index = device->leds.indexOfKey(led);
952 if (index >= 0) {
953 device->leds.replaceValueAt(led, on);
954 } else {
955 ADD_FAILURE()
956 << "Attempted to set the state of an LED that the EventHub declared "
957 "was not present. led=" << led;
958 }
959 }
960 }
961
Chris Yea52ade12020-08-27 16:49:20 -0700962 void getVirtualKeyDefinitions(
963 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 outVirtualKeys.clear();
965
966 Device* device = getDevice(deviceId);
967 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800968 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 }
970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700973 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 return false;
978 }
979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Chris Ye87143712020-11-10 05:05:58 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
987 return BATTERY_CAPACITY;
988 }
Kim Low03ea0352020-11-06 12:45:07 -0800989
Chris Yee2b1e5c2021-03-10 22:45:12 -0800990 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
991 return BATTERY_STATUS;
992 }
993
994 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
995
996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
997 return std::nullopt;
998 }
Kim Low03ea0352020-11-06 12:45:07 -0800999
Chris Ye3fdbfef2021-01-06 18:45:18 -08001000 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1001 std::vector<int32_t> ids;
1002 for (const auto& [rawId, info] : mRawLightInfos) {
1003 ids.push_back(rawId);
1004 }
1005 return ids;
1006 }
1007
1008 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1009 auto it = mRawLightInfos.find(lightId);
1010 if (it == mRawLightInfos.end()) {
1011 return std::nullopt;
1012 }
1013 return it->second;
1014 }
1015
1016 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1017 mLightBrightness.emplace(lightId, brightness);
1018 }
1019
1020 void setLightIntensities(int32_t deviceId, int32_t lightId,
1021 std::unordered_map<LightColor, int32_t> intensities) override {
1022 mLightIntensities.emplace(lightId, intensities);
1023 };
1024
1025 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightBrightness.find(lightId);
1027 if (lightIt == mLightBrightness.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 }
1032
1033 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1034 int32_t deviceId, int32_t lightId) override {
1035 auto lightIt = mLightIntensities.find(lightId);
1036 if (lightIt == mLightIntensities.end()) {
1037 return std::nullopt;
1038 }
1039 return lightIt->second;
1040 };
1041
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001042 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 return false;
1044 }
1045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051
Chris Yea52ade12020-08-27 16:49:20 -07001052 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053};
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055// --- FakeInputMapper ---
1056
1057class FakeInputMapper : public InputMapper {
1058 uint32_t mSources;
1059 int32_t mKeyboardType;
1060 int32_t mMetaState;
1061 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1062 KeyedVector<int32_t, int32_t> mScanCodeStates;
1063 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001064 // fake mapping which would normally come from keyCharacterMap
1065 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001066 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::mutex mLock;
1069 std::condition_variable mStateChangedCondition;
1070 bool mConfigureWasCalled GUARDED_BY(mLock);
1071 bool mResetWasCalled GUARDED_BY(mLock);
1072 bool mProcessWasCalled GUARDED_BY(mLock);
1073 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Arthur Hungc23540e2018-11-29 20:42:11 +08001075 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001077 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1078 : InputMapper(deviceContext),
1079 mSources(sources),
1080 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001082 mConfigureWasCalled(false),
1083 mResetWasCalled(false),
1084 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Chris Yea52ade12020-08-27 16:49:20 -07001086 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087
1088 void setKeyboardType(int32_t keyboardType) {
1089 mKeyboardType = keyboardType;
1090 }
1091
1092 void setMetaState(int32_t metaState) {
1093 mMetaState = metaState;
1094 }
1095
1096 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::unique_lock<std::mutex> lock(mLock);
1098 base::ScopedLockAssertion assumeLocked(mLock);
1099 const bool configureCalled =
1100 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1101 return mConfigureWasCalled;
1102 });
1103 if (!configureCalled) {
1104 FAIL() << "Expected configure() to have been called.";
1105 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mConfigureWasCalled = false;
1107 }
1108
1109 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::unique_lock<std::mutex> lock(mLock);
1111 base::ScopedLockAssertion assumeLocked(mLock);
1112 const bool resetCalled =
1113 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1114 return mResetWasCalled;
1115 });
1116 if (!resetCalled) {
1117 FAIL() << "Expected reset() to have been called.";
1118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 mResetWasCalled = false;
1120 }
1121
Yi Kong9b14ac62018-07-17 13:48:38 -07001122 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001123 std::unique_lock<std::mutex> lock(mLock);
1124 base::ScopedLockAssertion assumeLocked(mLock);
1125 const bool processCalled =
1126 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1127 return mProcessWasCalled;
1128 });
1129 if (!processCalled) {
1130 FAIL() << "Expected process() to have been called.";
1131 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 if (outLastEvent) {
1133 *outLastEvent = mLastEvent;
1134 }
1135 mProcessWasCalled = false;
1136 }
1137
1138 void setKeyCodeState(int32_t keyCode, int32_t state) {
1139 mKeyCodeStates.replaceValueFor(keyCode, state);
1140 }
1141
1142 void setScanCodeState(int32_t scanCode, int32_t state) {
1143 mScanCodeStates.replaceValueFor(scanCode, state);
1144 }
1145
1146 void setSwitchState(int32_t switchCode, int32_t state) {
1147 mSwitchStates.replaceValueFor(switchCode, state);
1148 }
1149
1150 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001151 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 }
1153
Philip Junker4af3b3d2021-12-14 10:36:55 +01001154 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1155 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1156 }
1157
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001159 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Chris Yea52ade12020-08-27 16:49:20 -07001161 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 InputMapper::populateDeviceInfo(deviceInfo);
1163
1164 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1165 deviceInfo->setKeyboardType(mKeyboardType);
1166 }
1167 }
1168
Chris Yea52ade12020-08-27 16:49:20 -07001169 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001170 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001172
1173 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001174 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001175 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1176 mViewport = config->getDisplayViewportByPort(*displayPort);
1177 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001178
1179 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 }
1181
Chris Yea52ade12020-08-27 16:49:20 -07001182 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Chris Yea52ade12020-08-27 16:49:20 -07001188 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 mLastEvent = *rawEvent;
1191 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 }
1194
Chris Yea52ade12020-08-27 16:49:20 -07001195 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1197 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1198 }
1199
Philip Junker4af3b3d2021-12-14 10:36:55 +01001200 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1201 auto it = mKeyCodeMapping.find(locationKeyCode);
1202 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1207 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1208 }
1209
Chris Yea52ade12020-08-27 16:49:20 -07001210 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1212 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1213 }
1214
Chris Yea52ade12020-08-27 16:49:20 -07001215 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001216 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001217 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001218 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1220 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1221 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 }
1223 }
1224 }
Chris Yea52ade12020-08-27 16:49:20 -07001225 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 return result;
1227 }
1228
1229 virtual int32_t getMetaState() {
1230 return mMetaState;
1231 }
1232
1233 virtual void fadePointer() {
1234 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001235
1236 virtual std::optional<int32_t> getAssociatedDisplay() {
1237 if (mViewport) {
1238 return std::make_optional(mViewport->displayId);
1239 }
1240 return std::nullopt;
1241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242};
1243
1244
1245// --- InstrumentedInputReader ---
1246
1247class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001248 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001251 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1252 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001253 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001254 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001256 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001258 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001260 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001261 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 InputDeviceIdentifier identifier;
1263 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001264 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001266 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 }
1268
Prabir Pradhan28efc192019-11-05 01:10:04 +00001269 // Make the protected loopOnce method accessible to tests.
1270 using InputReader::loopOnce;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001273 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1274 const InputDeviceIdentifier& identifier)
1275 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 if (!mNextDevices.empty()) {
1277 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1278 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 return device;
1280 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001281 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282 }
1283
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 // --- FakeInputReaderContext ---
1285 class FakeInputReaderContext : public ContextImpl {
1286 int32_t mGlobalMetaState;
1287 bool mUpdateGlobalMetaStateWasCalled;
1288 int32_t mGeneration;
1289
1290 public:
1291 FakeInputReaderContext(InputReader* reader)
1292 : ContextImpl(reader),
1293 mGlobalMetaState(0),
1294 mUpdateGlobalMetaStateWasCalled(false),
1295 mGeneration(1) {}
1296
1297 virtual ~FakeInputReaderContext() {}
1298
1299 void assertUpdateGlobalMetaStateWasCalled() {
1300 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1301 << "Expected updateGlobalMetaState() to have been called.";
1302 mUpdateGlobalMetaStateWasCalled = false;
1303 }
1304
1305 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1306
1307 uint32_t getGeneration() { return mGeneration; }
1308
1309 void updateGlobalMetaState() override {
1310 mUpdateGlobalMetaStateWasCalled = true;
1311 ContextImpl::updateGlobalMetaState();
1312 }
1313
1314 int32_t getGlobalMetaState() override {
1315 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1316 }
1317
1318 int32_t bumpGeneration() override {
1319 mGeneration = ContextImpl::bumpGeneration();
1320 return mGeneration;
1321 }
1322 } mFakeContext;
1323
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001325
1326public:
1327 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328};
1329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330// --- InputReaderPolicyTest ---
1331class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001332protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 sp<FakeInputReaderPolicy> mFakePolicy;
1334
Chris Yea52ade12020-08-27 16:49:20 -07001335 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1336 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337};
1338
1339/**
1340 * Check that empty set of viewports is an acceptable configuration.
1341 * Also try to get internal viewport two different ways - by type and by uniqueId.
1342 *
1343 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1344 * Such configuration is not currently allowed.
1345 */
1346TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001347 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348
1349 // We didn't add any viewports yet, so there shouldn't be any.
1350 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_FALSE(internalViewport);
1353
1354 // Add an internal viewport, then clear it
1355 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001357 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358
1359 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368
1369 mFakePolicy->clearViewports();
1370 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001371 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_FALSE(internalViewport);
1375}
1376
1377TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1378 const std::string internalUniqueId = "local:0";
1379 const std::string externalUniqueId = "local:1";
1380 const std::string virtualUniqueId1 = "virtual:2";
1381 const std::string virtualUniqueId2 = "virtual:3";
1382 constexpr int32_t virtualDisplayId1 = 2;
1383 constexpr int32_t virtualDisplayId2 = 3;
1384
1385 // Add an internal viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1388 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an external viewport
1390 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1392 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add an virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 // Add another virtual viewport
1398 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001399 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1400 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401
1402 // Check matching by type for internal
1403 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001404 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 ASSERT_TRUE(internalViewport);
1406 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1407
1408 // Check matching by type for external
1409 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001410 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_TRUE(externalViewport);
1412 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1413
1414 // Check matching by uniqueId for virtual viewport #1
1415 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001418 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1420 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1421
1422 // Check matching by uniqueId for virtual viewport #2
1423 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001424 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1428 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1429}
1430
1431
1432/**
1433 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1434 * that lookup works by checking display id.
1435 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1436 */
1437TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1438 const std::string uniqueId1 = "uniqueId1";
1439 const std::string uniqueId2 = "uniqueId2";
1440 constexpr int32_t displayId1 = 2;
1441 constexpr int32_t displayId2 = 3;
1442
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1444 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 for (const ViewportType& type : types) {
1446 mFakePolicy->clearViewports();
1447 // Add a viewport
1448 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 // Add another viewport
1452 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001453 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1454 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455
1456 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001457 std::optional<DisplayViewport> viewport1 =
1458 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001459 ASSERT_TRUE(viewport1);
1460 ASSERT_EQ(displayId1, viewport1->displayId);
1461 ASSERT_EQ(type, viewport1->type);
1462
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 std::optional<DisplayViewport> viewport2 =
1464 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_TRUE(viewport2);
1466 ASSERT_EQ(displayId2, viewport2->displayId);
1467 ASSERT_EQ(type, viewport2->type);
1468
1469 // When there are multiple viewports of the same kind, and uniqueId is not specified
1470 // in the call to getDisplayViewport, then that situation is not supported.
1471 // The viewports can be stored in any order, so we cannot rely on the order, since that
1472 // is just implementation detail.
1473 // However, we can check that it still returns *a* viewport, we just cannot assert
1474 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 ASSERT_TRUE(someViewport);
1477 }
1478}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001481 * When we have multiple internal displays make sure we always return the default display when
1482 * querying by type.
1483 */
1484TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1485 const std::string uniqueId1 = "uniqueId1";
1486 const std::string uniqueId2 = "uniqueId2";
1487 constexpr int32_t nonDefaultDisplayId = 2;
1488 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1489 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1490
1491 // Add the default display first and ensure it gets returned.
1492 mFakePolicy->clearViewports();
1493 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001497 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001498 ViewportType::INTERNAL);
1499
1500 std::optional<DisplayViewport> viewport =
1501 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1502 ASSERT_TRUE(viewport);
1503 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1504 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1505
1506 // Add the default display second to make sure order doesn't matter.
1507 mFakePolicy->clearViewports();
1508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514
1515 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1516 ASSERT_TRUE(viewport);
1517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1519}
1520
1521/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 * Check getDisplayViewportByPort
1523 */
1524TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526 const std::string uniqueId1 = "uniqueId1";
1527 const std::string uniqueId2 = "uniqueId2";
1528 constexpr int32_t displayId1 = 1;
1529 constexpr int32_t displayId2 = 2;
1530 const uint8_t hdmi1 = 0;
1531 const uint8_t hdmi2 = 1;
1532 const uint8_t hdmi3 = 2;
1533
1534 mFakePolicy->clearViewports();
1535 // Add a viewport that's associated with some display port that's not of interest.
1536 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 // Add another viewport, connected to HDMI1 port
1540 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001541 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1542 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001543
1544 // Check that correct display viewport was returned by comparing the display ports.
1545 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1546 ASSERT_TRUE(hdmi1Viewport);
1547 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1548 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1549
1550 // Check that we can still get the same viewport using the uniqueId
1551 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1552 ASSERT_TRUE(hdmi1Viewport);
1553 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1554 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1555 ASSERT_EQ(type, hdmi1Viewport->type);
1556
1557 // Check that we cannot find a port with "HDMI2", because we never added one
1558 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1559 ASSERT_FALSE(hdmi2Viewport);
1560}
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562// --- InputReaderTest ---
1563
1564class InputReaderTest : public testing::Test {
1565protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001566 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001569 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001572 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
Prabir Pradhan28efc192019-11-05 01:10:04 +00001576 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 }
1579
Chris Yea52ade12020-08-27 16:49:20 -07001580 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001581 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 }
1584
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001585 void addDevice(int32_t eventHubId, const std::string& name,
1586 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 }
1592 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593 mReader->loopOnce();
1594 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001595 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1596 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001599 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 }
1603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001606 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 }
1608
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001610 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001611 ftl::Flags<InputDeviceClass> classes,
1612 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1615 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001616 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 return mapper;
1619 }
1620};
1621
Chris Ye98d3f532020-10-01 21:48:59 -07001622TEST_F(InputReaderTest, PolicyGetInputDevices) {
1623 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001624 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001625 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626
1627 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001628 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001631 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001634 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635}
1636
Chris Yee7310032020-09-22 15:36:28 -07001637TEST_F(InputReaderTest, GetMergedInputDevices) {
1638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1639 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1640 // Add two subdevices to device
1641 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1642 // Must add at least one mapper or the device will be ignored!
1643 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1644 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1645
1646 // Push same device instance for next device to be added, so they'll have same identifier.
1647 mReader->pushNextDevice(device);
1648 mReader->pushNextDevice(device);
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1651 ASSERT_NO_FATAL_FAILURE(
1652 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1653
1654 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001655 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001656}
1657
Chris Yee14523a2020-12-19 13:46:00 -08001658TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1660 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1661 // Add two subdevices to device
1662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1663 // Must add at least one mapper or the device will be ignored!
1664 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1665 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1666
1667 // Push same device instance for next device to be added, so they'll have same identifier.
1668 mReader->pushNextDevice(device);
1669 mReader->pushNextDevice(device);
1670 // Sensor device is initially disabled
1671 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1672 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1673 nullptr));
1674 // Device is disabled because the only sub device is a sensor device and disabled initially.
1675 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1676 ASSERT_FALSE(device->isEnabled());
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1679 // The merged device is enabled if any sub device is enabled
1680 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1681 ASSERT_TRUE(device->isEnabled());
1682}
1683
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001686 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 constexpr int32_t eventHubId = 1;
1688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001691 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
Yi Kong9b14ac62018-07-17 13:48:38 -07001694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695
1696 NotifyDeviceResetArgs resetArgs;
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001698 ASSERT_EQ(deviceId, resetArgs.deviceId);
1699
1700 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001702 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 ASSERT_EQ(deviceId, resetArgs.deviceId);
1706 ASSERT_EQ(device->isEnabled(), false);
1707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001709 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 ASSERT_EQ(device->isEnabled(), false);
1713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 ASSERT_EQ(deviceId, resetArgs.deviceId);
1718 ASSERT_EQ(device->isEnabled(), true);
1719}
1720
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001723 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 AINPUT_SOURCE_KEYBOARD, nullptr);
1728 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1731 AINPUT_SOURCE_ANY, AKEYCODE_A))
1732 << "Should return unknown when the device id is >= 0 but unknown.";
1733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1735 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown when the device id is valid but the sources are not "
1737 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 ASSERT_EQ(AKEY_STATE_DOWN,
1740 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1741 AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is valid and the device "
1743 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1748
1749 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1750 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1751 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1752}
1753
Philip Junker4af3b3d2021-12-14 10:36:55 +01001754TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1755 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1756 constexpr int32_t eventHubId = 1;
1757 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1758 InputDeviceClass::KEYBOARD,
1759 AINPUT_SOURCE_KEYBOARD, nullptr);
1760 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1761
1762 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1763 << "Should return unknown when the device with the specified id is not found.";
1764
1765 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1766 << "Should return correct mapping when device id is valid and mapping exists.";
1767
1768 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1769 << "Should return the location key code when device id is valid and there's no "
1770 "mapping.";
1771}
1772
1773TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1775 constexpr int32_t eventHubId = 1;
1776 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1777 InputDeviceClass::JOYSTICK,
1778 AINPUT_SOURCE_GAMEPAD, nullptr);
1779 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1780
1781 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1783}
1784
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001787 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001790 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 AINPUT_SOURCE_KEYBOARD, nullptr);
1792 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
1794 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1795 AINPUT_SOURCE_ANY, KEY_A))
1796 << "Should return unknown when the device id is >= 0 but unknown.";
1797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1799 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1800 << "Should return unknown when the device id is valid but the sources are not "
1801 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_EQ(AKEY_STATE_DOWN,
1804 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1805 KEY_A))
1806 << "Should return value provided by mapper when device id is valid and the device "
1807 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808
1809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1812
1813 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1814 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1815 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1816}
1817
1818TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001820 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 AINPUT_SOURCE_KEYBOARD, nullptr);
1825 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1828 AINPUT_SOURCE_ANY, SW_LID))
1829 << "Should return unknown when the device id is >= 0 but unknown.";
1830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1832 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1833 << "Should return unknown when the device id is valid but the sources are not "
1834 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 ASSERT_EQ(AKEY_STATE_DOWN,
1837 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1838 SW_LID))
1839 << "Should return value provided by mapper when device id is valid and the device "
1840 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841
1842 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1845
1846 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1847 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1848 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1849}
1850
1851TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001859 mapper.addSupportedKeyCode(AKEYCODE_A);
1860 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 uint8_t flags[4] = { 0, 0, 0, 1 };
1864
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001865 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866 << "Should return false when device id is >= 0 but unknown.";
1867 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1868
1869 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001870 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001871 << "Should return false when device id is valid but the sources are not supported by "
1872 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1874
1875 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001876 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 keyCodes, flags))
1878 << "Should return value provided by mapper when device id is valid and the device "
1879 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1881
1882 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001883 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1884 << "Should return false when the device id is < 0 but the sources are not supported by "
1885 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1887
1888 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_TRUE(
1890 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1891 << "Should return value provided by mapper when device id is < 0 and one of the "
1892 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1894}
1895
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001896TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001898 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001899
1900 NotifyConfigurationChangedArgs args;
1901
1902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1903 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1904}
1905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001906TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001916 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001917 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1919
1920 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 ASSERT_EQ(when, event.when);
1923 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001925 ASSERT_EQ(EV_KEY, event.type);
1926 ASSERT_EQ(KEY_A, event.code);
1927 ASSERT_EQ(1, event.value);
1928}
1929
Garfield Tan1c7bc862020-01-28 13:24:04 -08001930TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 constexpr int32_t eventHubId = 1;
1934 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001937 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001939
1940 NotifyDeviceResetArgs resetArgs;
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001950 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001951 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001953 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001954 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961}
1962
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1964 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001965 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 constexpr int32_t eventHubId = 1;
1967 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1968 // Must add at least one mapper or the device will be ignored!
1969 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001970 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1972
1973 NotifyDeviceResetArgs resetArgs;
1974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1975 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1976}
1977
Arthur Hungc23540e2018-11-29 20:42:11 +08001978TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001981 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001982 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001983 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1984 FakeInputMapper& mapper =
1985 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001986 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001987
1988 const uint8_t hdmi1 = 1;
1989
1990 // Associated touch screen with second display.
1991 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1992
1993 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001994 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001999 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002000 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002002 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002003
2004 // Add the device, and make sure all of the callbacks are triggered.
2005 // The device is added after the input port associations are processed since
2006 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002010 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
Arthur Hung2c9a3342019-07-23 14:18:59 +08002012 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 ASSERT_EQ(deviceId, device->getId());
2014 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2015 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002016
2017 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002018 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002021}
2022
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002026 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2028 // Must add at least one mapper or the device will be ignored!
2029 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2030 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2031 mReader->pushNextDevice(device);
2032 mReader->pushNextDevice(device);
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2034 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2037
2038 NotifyDeviceResetArgs resetArgs;
2039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2040 ASSERT_EQ(deviceId, resetArgs.deviceId);
2041 ASSERT_TRUE(device->isEnabled());
2042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2043 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2044
2045 disableDevice(deviceId);
2046 mReader->loopOnce();
2047
2048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2049 ASSERT_EQ(deviceId, resetArgs.deviceId);
2050 ASSERT_FALSE(device->isEnabled());
2051 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2052 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2053
2054 enableDevice(deviceId);
2055 mReader->loopOnce();
2056
2057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2058 ASSERT_EQ(deviceId, resetArgs.deviceId);
2059 ASSERT_TRUE(device->isEnabled());
2060 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2061 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2062}
2063
2064TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2065 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002066 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002067 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2068 // Add two subdevices to device
2069 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2070 FakeInputMapper& mapperDevice1 =
2071 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2072 FakeInputMapper& mapperDevice2 =
2073 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2074 mReader->pushNextDevice(device);
2075 mReader->pushNextDevice(device);
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2077 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2078
2079 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2080 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2081
2082 ASSERT_EQ(AKEY_STATE_DOWN,
2083 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2084 ASSERT_EQ(AKEY_STATE_DOWN,
2085 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2086 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2087 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2088}
2089
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2091 NotifyPointerCaptureChangedArgs args;
2092
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002093 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2098 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099
2100 mFakePolicy->setPointerCapture(false);
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
2103 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002104 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 // does not change.
2108 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2109 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002111}
2112
Chris Ye87143712020-11-10 05:05:58 +00002113class FakeVibratorInputMapper : public FakeInputMapper {
2114public:
2115 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2116 : FakeInputMapper(deviceContext, sources) {}
2117
2118 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2119};
2120
2121TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2122 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002123 ftl::Flags<InputDeviceClass> deviceClass =
2124 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002125 constexpr int32_t eventHubId = 1;
2126 const char* DEVICE_LOCATION = "BLUETOOTH";
2127 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2128 FakeVibratorInputMapper& mapper =
2129 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2130 mReader->pushNextDevice(device);
2131
2132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2133 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2134
2135 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2136 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2137}
2138
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002139// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002140
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002141class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002142public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002143 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002145 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002146
2147 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2148
2149 void dump(std::string& dump) override {}
2150
2151 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2152 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002153 }
2154
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2156 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002157 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
2159 bool setLightColor(int32_t lightId, int32_t color) override {
2160 getDeviceContext().setLightBrightness(lightId, color >> 24);
2161 return true;
2162 }
2163
2164 std::optional<int32_t> getLightColor(int32_t lightId) override {
2165 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2166 if (!result.has_value()) {
2167 return std::nullopt;
2168 }
2169 return result.value() << 24;
2170 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
2172 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2173
2174 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2175
2176private:
2177 InputDeviceContext& mDeviceContext;
2178 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2179 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002180};
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182TEST_F(InputReaderTest, BatteryGetCapacity) {
2183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002184 ftl::Flags<InputDeviceClass> deviceClass =
2185 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186 constexpr int32_t eventHubId = 1;
2187 const char* DEVICE_LOCATION = "BLUETOOTH";
2188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController& controller =
2190 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 mReader->pushNextDevice(device);
2192
2193 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2194
2195 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2196 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2197}
2198
2199TEST_F(InputReaderTest, BatteryGetStatus) {
2200 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002201 ftl::Flags<InputDeviceClass> deviceClass =
2202 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 constexpr int32_t eventHubId = 1;
2204 const char* DEVICE_LOCATION = "BLUETOOTH";
2205 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002206 FakePeripheralController& controller =
2207 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002208 mReader->pushNextDevice(device);
2209
2210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2211
2212 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2213 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2214}
2215
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216TEST_F(InputReaderTest, LightGetColor) {
2217 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002218 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002224 mReader->pushNextDevice(device);
2225 RawLightInfo info = {.id = 1,
2226 .name = "Mono",
2227 .maxBrightness = 255,
2228 .flags = InputLightClass::BRIGHTNESS,
2229 .path = ""};
2230 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2231 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2232
2233 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2238 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2239}
2240
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241// --- InputReaderIntegrationTest ---
2242
2243// These tests create and interact with the InputReader only through its interface.
2244// The InputReader is started during SetUp(), which starts its processing in its own
2245// thread. The tests use linux uinput to emulate input devices.
2246// NOTE: Interacting with the physical device while these tests are running may cause
2247// the tests to fail.
2248class InputReaderIntegrationTest : public testing::Test {
2249protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002252 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002254 std::shared_ptr<FakePointerController> mFakePointerController;
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002258 mFakePointerController = std::make_shared<FakePointerController>();
2259 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2261 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002263 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2264 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 ASSERT_EQ(mReader->start(), OK);
2266
2267 // Since this test is run on a real device, all the input devices connected
2268 // to the test device will show up in mReader. We wait for those input devices to
2269 // show up before beginning the tests.
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2272 }
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mReader.reset();
2277 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278 mFakePolicy.clear();
2279 }
2280};
2281
2282TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2283 // An invalid input device that is only used for this test.
2284 class InvalidUinputDevice : public UinputDevice {
2285 public:
2286 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2287
2288 private:
2289 void configureDevice(int fd, uinput_user_dev* device) override {}
2290 };
2291
2292 const size_t numDevices = mFakePolicy->getInputDevices().size();
2293
2294 // UinputDevice does not set any event or key bits, so InputReader should not
2295 // consider it as a valid device.
2296 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2299 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2300
2301 invalidDevice.reset();
2302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2304 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2305}
2306
2307TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2308 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2309
2310 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2314
2315 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002317 const auto& it =
2318 std::find_if(inputDevices.begin(), inputDevices.end(),
2319 [&keyboard](const InputDeviceInfo& info) {
2320 return info.getIdentifier().name == keyboard->getName();
2321 });
2322
2323 ASSERT_NE(it, inputDevices.end());
2324 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2325 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2326 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002327
2328 keyboard.reset();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2332}
2333
2334TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2335 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2337
2338 NotifyConfigurationChangedArgs configChangedArgs;
2339 ASSERT_NO_FATAL_FAILURE(
2340 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002341 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002342 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2343
2344 NotifyKeyArgs keyArgs;
2345 keyboard->pressAndReleaseHomeKey();
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002348 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002349 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352 prevTimestamp = keyArgs.eventTime;
2353
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002356 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002358 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002361/**
2362 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2363 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2364 * are passed to the listener.
2365 */
2366static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2367TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2368 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2369 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2370 NotifyKeyArgs keyArgs;
2371
2372 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2375 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2376
2377 controller->pressAndReleaseKey(BTN_GEAR_UP);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2380 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2381}
2382
Arthur Hungaab25622020-01-16 11:22:11 +08002383// --- TouchProcessTest ---
2384class TouchIntegrationTest : public InputReaderIntegrationTest {
2385protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002386 const std::string UNIQUE_ID = "local:0";
2387
Chris Yea52ade12020-08-27 16:49:20 -07002388 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002389 InputReaderIntegrationTest::SetUp();
2390 // At least add an internal display.
2391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2392 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002393 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002394
2395 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2398 }
2399
2400 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2401 int32_t orientation, const std::string& uniqueId,
2402 std::optional<uint8_t> physicalPort,
2403 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002404 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2405 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002406 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2407 }
2408
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002409 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2410 NotifyMotionArgs args;
2411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2412 EXPECT_EQ(action, args.action);
2413 ASSERT_EQ(points.size(), args.pointerCount);
2414 for (size_t i = 0; i < args.pointerCount; i++) {
2415 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2416 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2417 }
2418 }
2419
Arthur Hungaab25622020-01-16 11:22:11 +08002420 std::unique_ptr<UinputTouchScreen> mDevice;
2421};
2422
2423TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2424 NotifyMotionArgs args;
2425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002428 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002429 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2433
2434 // ACTION_MOVE
2435 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002436 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2439
2440 // ACTION_UP
2441 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002442 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2445}
2446
2447TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2448 NotifyMotionArgs args;
2449 const Point centerPoint = mDevice->getCenterPoint();
2450
2451 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002452 mDevice->sendSlot(FIRST_SLOT);
2453 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002454 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2458
2459 // ACTION_POINTER_DOWN (Second slot)
2460 const Point secondPoint = centerPoint + Point(100, 100);
2461 mDevice->sendSlot(SECOND_SLOT);
2462 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002463 mDevice->sendDown(secondPoint);
2464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendMove(secondPoint + Point(1, 1));
2470 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2473
2474 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002475 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002478 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002479
2480 // ACTION_UP
2481 mDevice->sendSlot(FIRST_SLOT);
2482 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2486}
2487
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488/**
2489 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2490 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2491 * data?
2492 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2493 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2494 * for Pointer 0 only is generated after.
2495 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2496 * events, we will not miss any information.
2497 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2498 * event generated afterwards that contains the newest movement of pointer 0.
2499 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2500 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2501 * losing information about non-palm pointers.
2502 */
2503TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2504 NotifyMotionArgs args;
2505 const Point centerPoint = mDevice->getCenterPoint();
2506
2507 // ACTION_DOWN
2508 mDevice->sendSlot(FIRST_SLOT);
2509 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2510 mDevice->sendDown(centerPoint);
2511 mDevice->sendSync();
2512 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2513
2514 // ACTION_POINTER_DOWN (Second slot)
2515 const Point secondPoint = centerPoint + Point(100, 100);
2516 mDevice->sendSlot(SECOND_SLOT);
2517 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2518 mDevice->sendDown(secondPoint);
2519 mDevice->sendSync();
2520 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2521
2522 // ACTION_MOVE (First slot)
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendMove(centerPoint + Point(5, 5));
2525 // ACTION_POINTER_UP (Second slot)
2526 mDevice->sendSlot(SECOND_SLOT);
2527 mDevice->sendPointerUp();
2528 // Send a single sync for the above 2 pointer updates
2529 mDevice->sendSync();
2530
2531 // First, we should get POINTER_UP for the second pointer
2532 assertReceivedMotion(ACTION_POINTER_1_UP,
2533 {/*first pointer */ centerPoint + Point(5, 5),
2534 /*second pointer*/ secondPoint});
2535
2536 // Next, the MOVE event for the first pointer
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2538}
2539
2540/**
2541 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2542 * move, and then it will go up, all in the same frame.
2543 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2544 * gets sent to the listener.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendMove(secondPoint + Point(6, 6));
2571 mDevice->sendPointerUp();
2572 // Send a single sync for the above 2 pointer updates
2573 mDevice->sendSync();
2574
2575 // First, we should get POINTER_UP for the second pointer
2576 // The movement of the second pointer during the liftoff frame is ignored.
2577 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2578 assertReceivedMotion(ACTION_POINTER_1_UP,
2579 {/*first pointer */ centerPoint + Point(5, 5),
2580 /*second pointer*/ secondPoint});
2581
2582 // Next, the MOVE event for the first pointer
2583 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2584}
2585
Arthur Hungaab25622020-01-16 11:22:11 +08002586TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2587 NotifyMotionArgs args;
2588 const Point centerPoint = mDevice->getCenterPoint();
2589
2590 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002593 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2597
arthurhungcc7f9802020-04-30 17:55:40 +08002598 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002599 const Point secondPoint = centerPoint + Point(100, 100);
2600 mDevice->sendSlot(SECOND_SLOT);
2601 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2602 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002605 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002606
arthurhungcc7f9802020-04-30 17:55:40 +08002607 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002608 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2612
arthurhungcc7f9802020-04-30 17:55:40 +08002613 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2614 // a palm event.
2615 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002616 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002619 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002620 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002621
arthurhungcc7f9802020-04-30 17:55:40 +08002622 // Send up to second slot, expect first slot send moving.
2623 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002625 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002627
arthurhungcc7f9802020-04-30 17:55:40 +08002628 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002629 mDevice->sendSlot(FIRST_SLOT);
2630 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002632
arthurhungcc7f9802020-04-30 17:55:40 +08002633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2634 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002635}
2636
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638class InputDeviceTest : public testing::Test {
2639protected:
2640 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 static const int32_t DEVICE_ID;
2643 static const int32_t DEVICE_GENERATION;
2644 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002645 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002646 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002652 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Chris Yea52ade12020-08-27 16:49:20 -07002654 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002655 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 InputDeviceIdentifier identifier;
2661 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002662 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002666 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 }
2669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002671 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674};
2675
2676const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002677const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2680const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002682 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
2685TEST_F(InputDeviceTest, ImmutableProperties) {
2686 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002687 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689}
2690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2692 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002693}
2694
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2696 // Configuration.
2697 InputReaderConfiguration config;
2698 mDevice->configure(ARBITRARY_TIME, &config, 0);
2699
2700 // Reset.
2701 mDevice->reset(ARBITRARY_TIME);
2702
2703 NotifyDeviceResetArgs resetArgs;
2704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2705 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2706 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2707
2708 // Metadata.
2709 ASSERT_TRUE(mDevice->isIgnored());
2710 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2711
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002712 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002714 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2716 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2717
2718 // State queries.
2719 ASSERT_EQ(0, mDevice->getMetaState());
2720
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown key code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown scan code state.";
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2726 << "Ignored device should return unknown switch state.";
2727
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002728 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002730 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 << "Ignored device should never mark any key codes.";
2732 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2733 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2734}
2735
2736TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2737 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002738 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper1 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2743 mapper1.setMetaState(AMETA_ALT_ON);
2744 mapper1.addSupportedKeyCode(AKEYCODE_A);
2745 mapper1.addSupportedKeyCode(AKEYCODE_B);
2746 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2747 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2748 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2749 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2750 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002752 FakeInputMapper& mapper2 =
2753 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
2756 InputReaderConfiguration config;
2757 mDevice->configure(ARBITRARY_TIME, &config, 0);
2758
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 std::string propertyValue;
2760 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002762 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2765 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 // Reset
2768 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2770 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 NotifyDeviceResetArgs resetArgs;
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2774 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2775 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2776
2777 // Metadata.
2778 ASSERT_FALSE(mDevice->isIgnored());
2779 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2780
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002781 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002783 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2785 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2786
2787 // State queries.
2788 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2789 << "Should query mappers and combine meta states.";
2790
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown key code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown scan code state when source not supported.";
2795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2796 << "Should return unknown switch state when source not supported.";
2797
2798 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2801 << "Should query mapper when source is supported.";
2802 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2803 << "Should query mapper when source is supported.";
2804
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002805 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 << "Should do nothing when source is unsupported.";
2809 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2811 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2812 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2813
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002814 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2817 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2818 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2819 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2820
2821 // Event handling.
2822 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 mDevice->process(&event, 1);
2825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2827 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830// A single input device is associated with a specific display. Check that:
2831// 1. Device is disabled if the viewport corresponding to the associated display is not found
2832// 2. Device is disabled when setEnabled API is called
2833TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835
2836 // First Configuration.
2837 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2838
2839 // Device should be enabled by default.
2840 ASSERT_TRUE(mDevice->isEnabled());
2841
2842 // Prepare associated info.
2843 constexpr uint8_t hdmi = 1;
2844 const std::string UNIQUE_ID = "local:1";
2845
2846 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2847 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2848 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2849 // Device should be disabled because it is associated with a specific display via
2850 // input port <-> display port association, but the corresponding display is not found
2851 ASSERT_FALSE(mDevice->isEnabled());
2852
2853 // Prepare displays.
2854 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002855 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2856 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled after set disable.
2862 mFakePolicy->addDisabledDevice(mDevice->getId());
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2865 ASSERT_FALSE(mDevice->isEnabled());
2866
2867 // Device should still be disabled even found the associated display.
2868 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2869 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2870 ASSERT_FALSE(mDevice->isEnabled());
2871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2874 // Device should be enabled by default.
2875 mFakePolicy->clearViewports();
2876 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2878 ASSERT_TRUE(mDevice->isEnabled());
2879
2880 // Device should be disabled because it is associated with a specific display, but the
2881 // corresponding display is not found.
2882 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002883 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887
2888 // Device should be enabled when a display is found.
2889 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2890 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2891 NO_PORT, ViewportType::INTERNAL);
2892 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled after set disable.
2897 mFakePolicy->addDisabledDevice(mDevice->getId());
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2900 ASSERT_FALSE(mDevice->isEnabled());
2901
2902 // Device should still be disabled even found the associated display.
2903 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2905 ASSERT_FALSE(mDevice->isEnabled());
2906}
2907
Christine Franks2a2293c2022-01-18 11:51:16 -08002908TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2909 mFakePolicy->clearViewports();
2910 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2912
2913 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2915 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2916 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2917 NO_PORT, ViewportType::INTERNAL);
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2921}
2922
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923// --- InputMapperTest ---
2924
2925class InputMapperTest : public testing::Test {
2926protected:
2927 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 static const int32_t DEVICE_ID;
2930 static const int32_t DEVICE_GENERATION;
2931 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002932 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 std::unique_ptr<InstrumentedInputReader> mReader;
2939 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002941 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002942 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002946 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 }
2949
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002950 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002951 SetUp(DEVICE_CLASSES);
2952 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953
Chris Yea52ade12020-08-27 16:49:20 -07002954 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002955 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 }
2958
2959 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002960 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 }
2962
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002963 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002964 if (!changes ||
2965 (changes &
2966 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2967 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002968 mReader->requestRefreshConfiguration(changes);
2969 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002970 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002971 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2972 }
2973
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2975 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002976 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002977 InputDeviceIdentifier identifier;
2978 identifier.name = name;
2979 identifier.location = location;
2980 std::shared_ptr<InputDevice> device =
2981 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2982 identifier);
2983 mReader->pushNextDevice(device);
2984 mFakeEventHub->addDevice(eventHubId, name, classes);
2985 mReader->loopOnce();
2986 return device;
2987 }
2988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002989 template <class T, typename... Args>
2990 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002992 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002994 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002995 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 }
2997
2998 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002999 int32_t orientation, const std::string& uniqueId,
3000 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003001 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3002 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003003 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3004 }
3005
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003006 void clearViewports() {
3007 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 }
3009
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3011 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 RawEvent event;
3013 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003014 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003015 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 event.type = type;
3017 event.code = code;
3018 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003019 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003020 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 }
3022
3023 static void assertMotionRange(const InputDeviceInfo& info,
3024 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3025 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003026 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3031 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3032 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3033 }
3034
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003035 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3036 float size, float touchMajor, float touchMinor, float toolMajor,
3037 float toolMinor, float orientation, float distance,
3038 float scaledAxisEpsilon = 1.f) {
3039 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3040 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3042 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003043 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3046 scaledAxisEpsilon);
3047 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3048 scaledAxisEpsilon);
3049 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3050 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3052 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3053 }
3054
Michael Wright17db18e2020-06-26 20:51:44 +01003055 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003057 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NEAR(x, actualX, 1);
3059 ASSERT_NEAR(y, actualY, 1);
3060 }
3061};
3062
3063const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003064const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003065const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3067const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003068const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3069 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003070const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072// --- SwitchInputMapperTest ---
3073
3074class SwitchInputMapperTest : public InputMapperTest {
3075protected:
3076};
3077
3078TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082}
3083
3084TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
3102 NotifySwitchArgs args;
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3104 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003105 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3106 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 args.switchMask);
3108 ASSERT_EQ(uint32_t(0), args.policyFlags);
3109}
3110
Chris Ye87143712020-11-10 05:05:58 +00003111// --- VibratorInputMapperTest ---
3112class VibratorInputMapperTest : public InputMapperTest {
3113protected:
3114 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3115};
3116
3117TEST_F(VibratorInputMapperTest, GetSources) {
3118 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3119
3120 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3121}
3122
3123TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3124 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3125
3126 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3127}
3128
3129TEST_F(VibratorInputMapperTest, Vibrate) {
3130 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003131 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003132 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3133
3134 VibrationElement pattern(2);
3135 VibrationSequence sequence(2);
3136 pattern.duration = std::chrono::milliseconds(200);
3137 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3138 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3139 sequence.addElement(pattern);
3140 pattern.duration = std::chrono::milliseconds(500);
3141 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3142 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3143 sequence.addElement(pattern);
3144
3145 std::vector<int64_t> timings = {0, 1};
3146 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3147
3148 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003149 // Start vibrating
3150 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003151 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003152 // Verify vibrator state listener was notified.
3153 mReader->loopOnce();
3154 NotifyVibratorStateArgs args;
3155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_TRUE(args.isOn);
3158 // Stop vibrating
3159 mapper.cancelVibrate(VIBRATION_TOKEN);
3160 ASSERT_FALSE(mapper.isVibrating());
3161 // Verify vibrator state listener was notified.
3162 mReader->loopOnce();
3163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003166}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167
Chris Yef59a2f42020-10-16 12:55:26 -07003168// --- SensorInputMapperTest ---
3169
3170class SensorInputMapperTest : public InputMapperTest {
3171protected:
3172 static const int32_t ACCEL_RAW_MIN;
3173 static const int32_t ACCEL_RAW_MAX;
3174 static const int32_t ACCEL_RAW_FUZZ;
3175 static const int32_t ACCEL_RAW_FLAT;
3176 static const int32_t ACCEL_RAW_RESOLUTION;
3177
3178 static const int32_t GYRO_RAW_MIN;
3179 static const int32_t GYRO_RAW_MAX;
3180 static const int32_t GYRO_RAW_FUZZ;
3181 static const int32_t GYRO_RAW_FLAT;
3182 static const int32_t GYRO_RAW_RESOLUTION;
3183
3184 static const float GRAVITY_MS2_UNIT;
3185 static const float DEGREE_RADIAN_UNIT;
3186
3187 void prepareAccelAxes();
3188 void prepareGyroAxes();
3189 void setAccelProperties();
3190 void setGyroProperties();
3191 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3192};
3193
3194const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3196const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3197const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3198const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3199
3200const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3201const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3202const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3203const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3204const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3205
3206const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3207const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3208
3209void SensorInputMapperTest::prepareAccelAxes() {
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3213 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3215 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3216}
3217
3218void SensorInputMapperTest::prepareGyroAxes() {
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3222 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3224 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3225}
3226
3227void SensorInputMapperTest::setAccelProperties() {
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 0);
3230 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3231 /* sensorDataIndex */ 1);
3232 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3233 /* sensorDataIndex */ 2);
3234 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3235 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3236 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3237 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3238 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3239}
3240
3241void SensorInputMapperTest::setGyroProperties() {
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 0);
3244 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3245 /* sensorDataIndex */ 1);
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3247 /* sensorDataIndex */ 2);
3248 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3249 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3250 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3251 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3252 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3253}
3254
3255TEST_F(SensorInputMapperTest, GetSources) {
3256 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3257
3258 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3259}
3260
3261TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3262 setAccelProperties();
3263 prepareAccelAxes();
3264 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3265
3266 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3267 std::chrono::microseconds(10000),
3268 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003269 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003275
3276 NotifySensorArgs args;
3277 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3278 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3279 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3280
3281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3282 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3283 ASSERT_EQ(args.deviceId, DEVICE_ID);
3284 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3285 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3286 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3287 ASSERT_EQ(args.values, values);
3288 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3289}
3290
3291TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3292 setGyroProperties();
3293 prepareGyroAxes();
3294 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3295
3296 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3297 std::chrono::microseconds(10000),
3298 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003299 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003305
3306 NotifySensorArgs args;
3307 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3308 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3309 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3310
3311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3312 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3313 ASSERT_EQ(args.deviceId, DEVICE_ID);
3314 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3315 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3316 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3317 ASSERT_EQ(args.values, values);
3318 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3319}
3320
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321// --- KeyboardInputMapperTest ---
3322
3323class KeyboardInputMapperTest : public InputMapperTest {
3324protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325 const std::string UNIQUE_ID = "local:0";
3326
3327 void prepareDisplay(int32_t orientation);
3328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330 int32_t originalKeyCode, int32_t rotatedKeyCode,
3331 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332};
3333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3335 * orientation.
3336 */
3337void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003338 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3339 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003340}
3341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 int32_t originalScanCode, int32_t originalKeyCode,
3344 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 NotifyKeyArgs args;
3346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3357 ASSERT_EQ(originalScanCode, args.scanCode);
3358 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368}
3369
3370TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3371 const int32_t USAGE_A = 0x070004;
3372 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003373 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003375 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3376 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3377 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 KeyboardInputMapper& mapper =
3380 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3381 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003382 // Initial metastate is AMETA_NONE.
3383 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
3385 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 NotifyKeyArgs args;
3388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3389 ASSERT_EQ(DEVICE_ID, args.deviceId);
3390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3391 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3393 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3394 ASSERT_EQ(KEY_HOME, args.scanCode);
3395 ASSERT_EQ(AMETA_NONE, args.metaState);
3396 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3397 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3399
3400 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(DEVICE_ID, args.deviceId);
3404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3405 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3407 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3408 ASSERT_EQ(KEY_HOME, args.scanCode);
3409 ASSERT_EQ(AMETA_NONE, args.metaState);
3410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3412 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3413
3414 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(DEVICE_ID, args.deviceId);
3419 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3420 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3422 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3423 ASSERT_EQ(0, args.scanCode);
3424 ASSERT_EQ(AMETA_NONE, args.metaState);
3425 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3426 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3427 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3428
3429 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3431 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(DEVICE_ID, args.deviceId);
3434 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3435 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3437 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3438 ASSERT_EQ(0, args.scanCode);
3439 ASSERT_EQ(AMETA_NONE, args.metaState);
3440 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3441 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3442 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3443
3444 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3452 ASSERT_EQ(0, args.keyCode);
3453 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(0U, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3461 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3463 ASSERT_EQ(DEVICE_ID, args.deviceId);
3464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3465 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3467 ASSERT_EQ(0, args.keyCode);
3468 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3469 ASSERT_EQ(AMETA_NONE, args.metaState);
3470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3471 ASSERT_EQ(0U, args.policyFlags);
3472 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3473}
3474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475/**
3476 * Ensure that the readTime is set to the time when the EV_KEY is received.
3477 */
3478TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3480
3481 KeyboardInputMapper& mapper =
3482 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3483 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3484 NotifyKeyArgs args;
3485
3486 // Key down
3487 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3489 ASSERT_EQ(12, args.readTime);
3490
3491 // Key up
3492 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3494 ASSERT_EQ(15, args.readTime);
3495}
3496
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3501 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3502 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 KeyboardInputMapper& mapper =
3505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Arthur Hung95f68612022-04-07 14:08:22 +08003508 // Initial metastate is AMETA_NONE.
3509 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
3511 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 NotifyKeyArgs args;
3514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
3531 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3534 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003536 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537}
3538
3539TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 KeyboardInputMapper& mapper =
3546 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3547 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003549 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3557 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3558}
3559
3560TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003567 KeyboardInputMapper& mapper =
3568 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3569 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003571 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003572 ASSERT_NO_FATAL_FAILURE(
3573 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3577 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3579 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003581 clearViewports();
3582 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583 ASSERT_NO_FATAL_FAILURE(
3584 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3588 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3590 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 clearViewports();
3593 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594 ASSERT_NO_FATAL_FAILURE(
3595 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3599 AKEYCODE_DPAD_UP, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3601 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 clearViewports();
3604 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605 ASSERT_NO_FATAL_FAILURE(
3606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3608 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3610 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3612 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
3614 // Special case: if orientation changes while key is down, we still emit the same keycode
3615 // in the key up as we did in the key down.
3616 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
3618 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3621 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3622 ASSERT_EQ(KEY_UP, args.scanCode);
3623 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3624
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003625 clearViewports();
3626 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3630 ASSERT_EQ(KEY_UP, args.scanCode);
3631 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3632}
3633
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3635 // If the keyboard is not orientation aware,
3636 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 NotifyKeyArgs args;
3643
3644 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3649 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3650
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3657}
3658
3659TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3660 // If the keyboard is orientation aware,
3661 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 KeyboardInputMapper& mapper =
3666 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3667 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 NotifyKeyArgs args;
3669
3670 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3671 // ^--- already checked by the previous test
3672
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003673 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003674 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3679 ASSERT_EQ(DISPLAY_ID, args.displayId);
3680
3681 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003682 clearViewports();
3683 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003684 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3689 ASSERT_EQ(newDisplayId, args.displayId);
3690}
3691
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 KeyboardInputMapper& mapper =
3694 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003697 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702}
3703
Philip Junker4af3b3d2021-12-14 10:36:55 +01003704TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3705 KeyboardInputMapper& mapper =
3706 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3707 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3708
3709 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3710 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3711 << "If a mapping is available, the result is equal to the mapping";
3712
3713 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3714 << "If no mapping is available, the result is the key location";
3715}
3716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 KeyboardInputMapper& mapper =
3719 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3720 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727}
3728
3729TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 KeyboardInputMapper& mapper =
3731 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3732 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003737 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 ASSERT_TRUE(flags[0]);
3739 ASSERT_FALSE(flags[1]);
3740}
3741
3742TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3744 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3745 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 KeyboardInputMapper& mapper =
3751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003753 // Initial metastate is AMETA_NONE.
3754 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
3761 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003767 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
3769 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
3777 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3781 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
3785 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3789 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3790 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800
3801 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808}
3809
Chris Yea52ade12020-08-27 16:49:20 -07003810TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3814 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3815
3816 KeyboardInputMapper& mapper =
3817 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3818 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3819
Chris Yea52ade12020-08-27 16:49:20 -07003820 // Meta state should be AMETA_NONE after reset
3821 mapper.reset(ARBITRARY_TIME);
3822 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3823 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3824 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3825 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3826
3827 NotifyKeyArgs args;
3828 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3831 ASSERT_EQ(AMETA_NONE, args.metaState);
3832 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3833 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3834 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3835
3836 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(AMETA_NONE, args.metaState);
3840 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3841 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3842 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3843}
3844
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3846 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003851
3852 // keyboard 2.
3853 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003854 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003856 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 std::shared_ptr<InputDevice> device2 =
3858 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003859 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3864 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 KeyboardInputMapper& mapper =
3867 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3868 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003871 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003873 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3874 device2->reset(ARBITRARY_TIME);
3875
3876 // Prepared displays and associated info.
3877 constexpr uint8_t hdmi1 = 0;
3878 constexpr uint8_t hdmi2 = 1;
3879 const std::string SECONDARY_UNIQUE_ID = "local:1";
3880
3881 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3882 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3883
3884 // No associated display viewport found, should disable the device.
3885 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3887 ASSERT_FALSE(device2->isEnabled());
3888
3889 // Prepare second display.
3890 constexpr int32_t newDisplayId = 2;
3891 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003892 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003893 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003894 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003895 // Default device will reconfigure above, need additional reconfiguration for another device.
3896 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3898
3899 // Device should be enabled after the associated display is found.
3900 ASSERT_TRUE(mDevice->isEnabled());
3901 ASSERT_TRUE(device2->isEnabled());
3902
3903 // Test pad key events
3904 ASSERT_NO_FATAL_FAILURE(
3905 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3907 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3908 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3909 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3911 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3912
3913 ASSERT_NO_FATAL_FAILURE(
3914 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3916 AKEYCODE_DPAD_RIGHT, newDisplayId));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3918 AKEYCODE_DPAD_DOWN, newDisplayId));
3919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3920 AKEYCODE_DPAD_LEFT, newDisplayId));
3921}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922
arthurhungc903df12020-08-11 15:08:42 +08003923TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3924 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3926 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3929 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3930
3931 KeyboardInputMapper& mapper =
3932 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3933 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003934 // Initial metastate is AMETA_NONE.
3935 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003936
3937 // Initialization should have turned all of the lights off.
3938 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3940 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3941
3942 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3946 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3947
3948 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3953
3954 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003957 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3959
3960 mFakeEventHub->removeDevice(EVENTHUB_ID);
3961 mReader->loopOnce();
3962
3963 // keyboard 2 should default toggle keys.
3964 const std::string USB2 = "USB2";
3965 const std::string DEVICE_NAME2 = "KEYBOARD2";
3966 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3967 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3968 std::shared_ptr<InputDevice> device2 =
3969 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003970 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003971 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3973 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3974 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3977
arthurhung6fe95782020-10-05 22:41:16 +08003978 KeyboardInputMapper& mapper2 =
3979 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003981 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3982 device2->reset(ARBITRARY_TIME);
3983
3984 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3986 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003987 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3988 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003989}
3990
Arthur Hungcb40a002021-08-03 14:31:01 +00003991TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3995
3996 // Suppose we have two mappers. (DPAD + KEYBOARD)
3997 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3998 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3999 KeyboardInputMapper& mapper =
4000 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4001 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004002 // Initial metastate is AMETA_NONE.
4003 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004004
4005 mReader->toggleCapsLockState(DEVICE_ID);
4006 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4007}
4008
Arthur Hungfb3cc112022-04-13 07:39:50 +00004009TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4010 // keyboard 1.
4011 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4012 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4013 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4016 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4017
4018 KeyboardInputMapper& mapper1 =
4019 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4020 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4021
4022 // keyboard 2.
4023 const std::string USB2 = "USB2";
4024 const std::string DEVICE_NAME2 = "KEYBOARD2";
4025 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4026 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4027 std::shared_ptr<InputDevice> device2 =
4028 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4029 ftl::Flags<InputDeviceClass>(0));
4030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4036
4037 KeyboardInputMapper& mapper2 =
4038 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4039 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4040 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4041 device2->reset(ARBITRARY_TIME);
4042
Arthur Hung95f68612022-04-07 14:08:22 +08004043 // Initial metastate is AMETA_NONE.
4044 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4045 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4046
4047 // Toggle num lock on and off.
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004050 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4051 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4053
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4056 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4057 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4058 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4059
4060 // Toggle caps lock on and off.
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4063 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4064 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4066
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4069 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4070 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4072
4073 // Toggle scroll lock on and off.
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4076 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4077 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4079
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4082 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4083 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4085}
4086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004087// --- KeyboardInputMapperTest_ExternalDevice ---
4088
4089class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4090protected:
Chris Yea52ade12020-08-27 16:49:20 -07004091 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092};
4093
4094TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004095 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4096 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004097
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4100 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4101 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004103 KeyboardInputMapper& mapper =
4104 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4105 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004108 NotifyKeyArgs args;
4109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4110 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4114 ASSERT_EQ(uint32_t(0), args.policyFlags);
4115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4118 ASSERT_EQ(uint32_t(0), args.policyFlags);
4119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4122 ASSERT_EQ(uint32_t(0), args.policyFlags);
4123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4126 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4127
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004128 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4131}
4132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004135
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141 KeyboardInputMapper& mapper =
4142 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4143 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004144
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146 NotifyKeyArgs args;
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4149
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(uint32_t(0), args.policyFlags);
4161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4169}
4170
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171// --- CursorInputMapperTest ---
4172
4173class CursorInputMapperTest : public InputMapperTest {
4174protected:
4175 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4176
Michael Wright17db18e2020-06-26 20:51:44 +01004177 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178
Chris Yea52ade12020-08-27 16:49:20 -07004179 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 InputMapperTest::SetUp();
4181
Michael Wright17db18e2020-06-26 20:51:44 +01004182 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004183 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 }
4185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4187 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188
4189 void prepareDisplay(int32_t orientation) {
4190 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004191 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004192 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4193 orientation, uniqueId, NO_PORT, viewportType);
4194 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004195
4196 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4197 float pressure) {
4198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4199 0.0f, 0.0f, 0.0f, EPSILON));
4200 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201};
4202
4203const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4206 int32_t originalY, int32_t rotatedX,
4207 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 NotifyMotionArgs args;
4209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(
4216 assertCursorPointerCoords(args.pointerCoords[0],
4217 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4218 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219}
4220
4221TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004225 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
4228TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
4235TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
4239 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004243 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4244 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4246 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4247
4248 // When the bounds are set, then there should be a valid motion range.
4249 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4250
4251 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
4254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4255 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4256 1, 800 - 1, 0.0f, 0.0f));
4257 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4258 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4259 2, 480 - 1, 0.0f, 0.0f));
4260 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4261 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4262 0.0f, 1.0f, 0.0f, 0.0f));
4263}
4264
4265TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
4269 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4273 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4274 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4275 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4276 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4277 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4278 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4279 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4280 0.0f, 1.0f, 0.0f, 0.0f));
4281}
4282
4283TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004285 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286
arthurhungdcef2dc2020-08-11 14:47:50 +08004287 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
4289 NotifyMotionArgs args;
4290
4291 // Button press.
4292 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4296 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4297 ASSERT_EQ(DEVICE_ID, args.deviceId);
4298 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4299 ASSERT_EQ(uint32_t(0), args.policyFlags);
4300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4301 ASSERT_EQ(0, args.flags);
4302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4304 ASSERT_EQ(0, args.edgeFlags);
4305 ASSERT_EQ(uint32_t(1), args.pointerCount);
4306 ASSERT_EQ(0, args.pointerProperties[0].id);
4307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4310 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4312
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4314 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4315 ASSERT_EQ(DEVICE_ID, args.deviceId);
4316 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4317 ASSERT_EQ(uint32_t(0), args.policyFlags);
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4319 ASSERT_EQ(0, args.flags);
4320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4322 ASSERT_EQ(0, args.edgeFlags);
4323 ASSERT_EQ(uint32_t(1), args.pointerCount);
4324 ASSERT_EQ(0, args.pointerProperties[0].id);
4325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4328 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4329 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4330
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004332 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4333 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4335 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4336 ASSERT_EQ(DEVICE_ID, args.deviceId);
4337 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4338 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4340 ASSERT_EQ(0, args.flags);
4341 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4342 ASSERT_EQ(0, args.buttonState);
4343 ASSERT_EQ(0, args.edgeFlags);
4344 ASSERT_EQ(uint32_t(1), args.pointerCount);
4345 ASSERT_EQ(0, args.pointerProperties[0].id);
4346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4349 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4350 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4351
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4353 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4354 ASSERT_EQ(DEVICE_ID, args.deviceId);
4355 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4356 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4358 ASSERT_EQ(0, args.flags);
4359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4360 ASSERT_EQ(0, args.buttonState);
4361 ASSERT_EQ(0, args.edgeFlags);
4362 ASSERT_EQ(uint32_t(1), args.pointerCount);
4363 ASSERT_EQ(0, args.pointerProperties[0].id);
4364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4367 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4368 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4369}
4370
4371TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004373 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374
4375 NotifyMotionArgs args;
4376
4377 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4383 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4384 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
4386 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
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], 0.0f,
4392 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393}
4394
4395TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398
4399 NotifyMotionArgs args;
4400
4401 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422}
4423
4424TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004426 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
4428 NotifyMotionArgs args;
4429
4430 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4438 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4439 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4444 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4445 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4454 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4455 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456
4457 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463
4464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467}
4468
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004469TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004471 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4472 // need to be rotated.
4473 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004474 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004476 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4485}
4486
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004487TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004489 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4490 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004491 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004493 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4502
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004503 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004513 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4522
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004523 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532}
4533
4534TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004536 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537
4538 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4539 mFakePointerController->setPosition(100, 200);
4540 mFakePointerController->setButtonState(0);
4541
4542 NotifyMotionArgs motionArgs;
4543 NotifyKeyArgs keyArgs;
4544
4545 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004552 ASSERT_NO_FATAL_FAILURE(
4553 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004559 ASSERT_NO_FATAL_FAILURE(
4560 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_EQ(0, motionArgs.buttonState);
4567 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580 ASSERT_EQ(0, motionArgs.buttonState);
4581 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
4585 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4591 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4592 motionArgs.buttonState);
4593 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4594 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004595 ASSERT_NO_FATAL_FAILURE(
4596 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4601 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4602 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(
4604 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4609 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004628 ASSERT_NO_FATAL_FAILURE(
4629 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4635 ASSERT_EQ(0, motionArgs.buttonState);
4636 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004637 ASSERT_NO_FATAL_FAILURE(
4638 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_EQ(0, motionArgs.buttonState);
4644 ASSERT_EQ(0, mFakePointerController->getButtonState());
4645 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, mFakePointerController->getButtonState());
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655
4656 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4660 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4661 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004667 ASSERT_NO_FATAL_FAILURE(
4668 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(0, motionArgs.buttonState);
4682 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_EQ(0, motionArgs.buttonState);
4689 ASSERT_EQ(0, mFakePointerController->getButtonState());
4690
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(
4692 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4694 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4695 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4696
4697 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4701 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4702 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004703
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004708 ASSERT_NO_FATAL_FAILURE(
4709 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_EQ(0, motionArgs.buttonState);
4723 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4728 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4729 ASSERT_EQ(0, motionArgs.buttonState);
4730 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004731 ASSERT_NO_FATAL_FAILURE(
4732 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4735 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4736 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4737
4738 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4743 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004749 ASSERT_NO_FATAL_FAILURE(
4750 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_EQ(0, motionArgs.buttonState);
4764 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4770 ASSERT_EQ(0, motionArgs.buttonState);
4771 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(
4773 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4776 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4777 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4778
4779 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4783 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004790 ASSERT_NO_FATAL_FAILURE(
4791 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004813 ASSERT_NO_FATAL_FAILURE(
4814 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4817 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4818 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4819}
4820
4821TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004823 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824
4825 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4826 mFakePointerController->setPosition(100, 200);
4827 mFakePointerController->setButtonState(0);
4828
4829 NotifyMotionArgs args;
4830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004835 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4838 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 +01004839 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004840}
4841
4842TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004843 addConfigurationProperty("cursor.mode", "pointer");
4844 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004846
4847 NotifyDeviceResetArgs resetArgs;
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4849 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4850 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4851
4852 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4853 mFakePointerController->setPosition(100, 200);
4854 mFakePointerController->setButtonState(0);
4855
4856 NotifyMotionArgs args;
4857
4858 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4863 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4866 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 +01004867 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004868
4869 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4873 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4876 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4878 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4881 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4882
4883 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4885 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4887 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4890 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4892 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4895 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4896
4897 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4902 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4905 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 +01004906 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004907
4908 // Disable pointer capture and check that the device generation got bumped
4909 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004910 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911 mFakePolicy->setPointerCapture(false);
4912 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004913 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4916 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4917 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4918
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4923 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4926 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 +01004927 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928}
4929
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004930/**
4931 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4932 * pointer acceleration or speed processing should not be applied.
4933 */
4934TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4935 addConfigurationProperty("cursor.mode", "pointer");
4936 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4937 100.f /*high threshold*/, 10.f /*acceleration*/);
4938 mFakePolicy->setVelocityControlParams(testParams);
4939 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4940
4941 NotifyDeviceResetArgs resetArgs;
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4943 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4944 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4945
4946 NotifyMotionArgs args;
4947
4948 // Move and verify scale is applied.
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4953 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4955 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4956 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4957 ASSERT_GT(relX, 10);
4958 ASSERT_GT(relY, 20);
4959
4960 // Enable Pointer Capture
4961 mFakePolicy->setPointerCapture(true);
4962 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4963 NotifyPointerCaptureChangedArgs captureArgs;
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4965 ASSERT_TRUE(captureArgs.request.enable);
4966
4967 // Move and verify scale is not applied.
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4972 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4974 ASSERT_EQ(10, args.pointerCoords[0].getX());
4975 ASSERT_EQ(20, args.pointerCoords[0].getY());
4976}
4977
Prabir Pradhan208360b2022-06-24 18:37:04 +00004978TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4979 addConfigurationProperty("cursor.mode", "pointer");
4980 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4981
4982 NotifyDeviceResetArgs resetArgs;
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4984 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4985 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4986
4987 // Ensure the display is rotated.
4988 prepareDisplay(DISPLAY_ORIENTATION_90);
4989
4990 NotifyMotionArgs args;
4991
4992 // Verify that the coordinates are rotated.
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4997 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4999 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5000 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5001
5002 // Enable Pointer Capture.
5003 mFakePolicy->setPointerCapture(true);
5004 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5005 NotifyPointerCaptureChangedArgs captureArgs;
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5007 ASSERT_TRUE(captureArgs.request.enable);
5008
5009 // Move and verify rotation is not applied.
5010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5014 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5016 ASSERT_EQ(10, args.pointerCoords[0].getX());
5017 ASSERT_EQ(20, args.pointerCoords[0].getY());
5018}
5019
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005020TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005022
Garfield Tan888a6a42020-01-09 11:39:16 -08005023 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005024 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005025 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5026 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005027 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5028 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005029 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5030 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5031
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005032 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5033 mFakePointerController->setPosition(100, 200);
5034 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005035
5036 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5044 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 +01005045 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005046 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5047}
5048
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049// --- TouchInputMapperTest ---
5050
5051class TouchInputMapperTest : public InputMapperTest {
5052protected:
5053 static const int32_t RAW_X_MIN;
5054 static const int32_t RAW_X_MAX;
5055 static const int32_t RAW_Y_MIN;
5056 static const int32_t RAW_Y_MAX;
5057 static const int32_t RAW_TOUCH_MIN;
5058 static const int32_t RAW_TOUCH_MAX;
5059 static const int32_t RAW_TOOL_MIN;
5060 static const int32_t RAW_TOOL_MAX;
5061 static const int32_t RAW_PRESSURE_MIN;
5062 static const int32_t RAW_PRESSURE_MAX;
5063 static const int32_t RAW_ORIENTATION_MIN;
5064 static const int32_t RAW_ORIENTATION_MAX;
5065 static const int32_t RAW_DISTANCE_MIN;
5066 static const int32_t RAW_DISTANCE_MAX;
5067 static const int32_t RAW_TILT_MIN;
5068 static const int32_t RAW_TILT_MAX;
5069 static const int32_t RAW_ID_MIN;
5070 static const int32_t RAW_ID_MAX;
5071 static const int32_t RAW_SLOT_MIN;
5072 static const int32_t RAW_SLOT_MAX;
5073 static const float X_PRECISION;
5074 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005075 static const float X_PRECISION_VIRTUAL;
5076 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005079 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080
5081 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5082
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005083 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005084 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005085
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 enum Axes {
5087 POSITION = 1 << 0,
5088 TOUCH = 1 << 1,
5089 TOOL = 1 << 2,
5090 PRESSURE = 1 << 3,
5091 ORIENTATION = 1 << 4,
5092 MINOR = 1 << 5,
5093 ID = 1 << 6,
5094 DISTANCE = 1 << 7,
5095 TILT = 1 << 8,
5096 SLOT = 1 << 9,
5097 TOOL_TYPE = 1 << 10,
5098 };
5099
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005100 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5101 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005102 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005104 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 int32_t toRawX(float displayX);
5106 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005107 int32_t toRotatedRawX(float displayX);
5108 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005109 float toCookedX(float rawX, float rawY);
5110 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005112 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005114 float toDisplayY(int32_t rawY, int32_t displayHeight);
5115
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116};
5117
5118const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5119const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5120const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5121const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5122const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5123const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5124const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5125const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005126const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5129const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5130const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5132const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5134const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5135const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5136const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5138const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5139const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005140const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5141 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5142const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5143 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005144const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5145 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146
5147const float TouchInputMapperTest::GEOMETRIC_SCALE =
5148 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5149 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5150
5151const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5152 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5153 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5154};
5155
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005156void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005157 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5158 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005159}
5160
5161void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5162 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5163 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164}
5165
Santos Cordonfa5cf462017-04-05 10:37:00 -07005166void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005167 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5168 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5169 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170}
5171
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005173 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5174 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
Jason Gerecke489fda82012-09-07 17:19:40 -07005179void TouchInputMapperTest::prepareLocationCalibration() {
5180 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5181}
5182
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183int32_t TouchInputMapperTest::toRawX(float displayX) {
5184 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5185}
5186
5187int32_t TouchInputMapperTest::toRawY(float displayY) {
5188 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5189}
5190
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005191int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5192 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5193}
5194
5195int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5196 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5197}
5198
Jason Gerecke489fda82012-09-07 17:19:40 -07005199float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5200 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5201 return rawX;
5202}
5203
5204float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5205 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5206 return rawY;
5207}
5208
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210 return toDisplayX(rawX, DISPLAY_WIDTH);
5211}
5212
5213float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5214 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
5217float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005218 return toDisplayY(rawY, DISPLAY_HEIGHT);
5219}
5220
5221float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5222 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
5225
5226// --- SingleTouchInputMapperTest ---
5227
5228class SingleTouchInputMapperTest : public TouchInputMapperTest {
5229protected:
5230 void prepareButtons();
5231 void prepareAxes(int axes);
5232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005233 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5234 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5235 void processUp(SingleTouchInputMapper& mappery);
5236 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5237 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5238 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5239 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5240 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5241 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242};
5243
5244void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005245 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246}
5247
5248void SingleTouchInputMapperTest::prepareAxes(int axes) {
5249 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 }
5253 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5255 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 }
5257 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5259 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 }
5261 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5263 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 }
5265 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5267 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 }
5269}
5270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275}
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005282void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284}
5285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288}
5289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5291 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297}
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5300 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5306 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005310void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 prepareButtons();
5316 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320}
5321
5322TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5324 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 prepareButtons();
5326 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
5332TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 prepareButtons();
5334 prepareAxes(POSITION);
5335 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
5341TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 prepareButtons();
5343 prepareAxes(POSITION);
5344 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348}
5349
5350TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 addConfigurationProperty("touch.deviceType", "touchScreen");
5352 prepareDisplay(DISPLAY_ORIENTATION_0);
5353 prepareButtons();
5354 prepareAxes(POSITION);
5355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
5358 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
5361 // Virtual key is down.
5362 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5363 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5364 processDown(mapper, x, y);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 // Virtual key is up.
5371 processUp(mapper);
5372 processSync(mapper);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
5378TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 addConfigurationProperty("touch.deviceType", "touchScreen");
5380 prepareDisplay(DISPLAY_ORIENTATION_0);
5381 prepareButtons();
5382 prepareAxes(POSITION);
5383 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
5386 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 // Virtual key is down.
5390 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5391 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5392 processDown(mapper, x, y);
5393 processSync(mapper);
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
5398 // Virtual key is up.
5399 processUp(mapper);
5400 processSync(mapper);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
5406TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 prepareDisplay(DISPLAY_ORIENTATION_0);
5409 prepareButtons();
5410 prepareAxes(POSITION);
5411 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005415 ASSERT_TRUE(
5416 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 ASSERT_TRUE(flags[0]);
5418 ASSERT_FALSE(flags[1]);
5419}
5420
5421TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
arthurhungdcef2dc2020-08-11 14:47:50 +08005429 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
5431 NotifyKeyArgs args;
5432
5433 // Press virtual key.
5434 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5435 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5436 processDown(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5440 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5441 ASSERT_EQ(DEVICE_ID, args.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5443 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5445 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5446 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5447 ASSERT_EQ(KEY_HOME, args.scanCode);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5450
5451 // Release virtual key.
5452 processUp(mapper);
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5457 ASSERT_EQ(DEVICE_ID, args.deviceId);
5458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5459 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5461 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5462 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5463 ASSERT_EQ(KEY_HOME, args.scanCode);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5466
5467 // Should not have sent any motions.
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5469}
5470
5471TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 addConfigurationProperty("touch.deviceType", "touchScreen");
5473 prepareDisplay(DISPLAY_ORIENTATION_0);
5474 prepareButtons();
5475 prepareAxes(POSITION);
5476 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478
arthurhungdcef2dc2020-08-11 14:47:50 +08005479 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480
5481 NotifyKeyArgs keyArgs;
5482
5483 // Press virtual key.
5484 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5485 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5486 processDown(mapper, x, y);
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5490 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5491 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5493 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5494 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5496 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5497 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5499 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5500
5501 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5502 // into the display area.
5503 y -= 100;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5511 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5514 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5515 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5516 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5518 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5519
5520 NotifyMotionArgs motionArgs;
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5527 ASSERT_EQ(0, motionArgs.flags);
5528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5529 ASSERT_EQ(0, motionArgs.buttonState);
5530 ASSERT_EQ(0, motionArgs.edgeFlags);
5531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5535 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5536 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5537 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5539
5540 // Keep moving out of bounds. Should generate a pointer move.
5541 y -= 50;
5542 processMove(mapper, x, y);
5543 processSync(mapper);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.flags);
5552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5553 ASSERT_EQ(0, motionArgs.buttonState);
5554 ASSERT_EQ(0, motionArgs.edgeFlags);
5555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Release out of bounds. Should generate a pointer up.
5565 processUp(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
5578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5584 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5586
5587 // Should not have sent any more keys or motions.
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5590}
5591
5592TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 addConfigurationProperty("touch.deviceType", "touchScreen");
5594 prepareDisplay(DISPLAY_ORIENTATION_0);
5595 prepareButtons();
5596 prepareAxes(POSITION);
5597 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
arthurhungdcef2dc2020-08-11 14:47:50 +08005600 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 NotifyMotionArgs motionArgs;
5603
5604 // Initially go down out of bounds.
5605 int32_t x = -10;
5606 int32_t y = -10;
5607 processDown(mapper, x, y);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5611
5612 // Move into the display area. Should generate a pointer down.
5613 x = 50;
5614 y = 75;
5615 processMove(mapper, x, y);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5621 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5622 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5623 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5624 ASSERT_EQ(0, motionArgs.flags);
5625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5626 ASSERT_EQ(0, motionArgs.buttonState);
5627 ASSERT_EQ(0, motionArgs.edgeFlags);
5628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5632 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5633 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5634 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5636
5637 // Release. Should generate a pointer up.
5638 processUp(mapper);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5647 ASSERT_EQ(0, motionArgs.flags);
5648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5649 ASSERT_EQ(0, motionArgs.buttonState);
5650 ASSERT_EQ(0, motionArgs.edgeFlags);
5651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Should not have sent any more keys or motions.
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5663}
5664
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005666 addConfigurationProperty("touch.deviceType", "touchScreen");
5667 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5668
5669 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5670 prepareButtons();
5671 prepareAxes(POSITION);
5672 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005674
arthurhungdcef2dc2020-08-11 14:47:50 +08005675 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005676
5677 NotifyMotionArgs motionArgs;
5678
5679 // Down.
5680 int32_t x = 100;
5681 int32_t y = 125;
5682 processDown(mapper, x, y);
5683 processSync(mapper);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5687 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5688 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5689 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5690 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5692 ASSERT_EQ(0, motionArgs.flags);
5693 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5694 ASSERT_EQ(0, motionArgs.buttonState);
5695 ASSERT_EQ(0, motionArgs.edgeFlags);
5696 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5700 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5701 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5703 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5705
5706 // Move.
5707 x += 50;
5708 y += 75;
5709 processMove(mapper, x, y);
5710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5715 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5719 ASSERT_EQ(0, motionArgs.flags);
5720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5721 ASSERT_EQ(0, motionArgs.buttonState);
5722 ASSERT_EQ(0, motionArgs.edgeFlags);
5723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5728 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Up.
5734 processUp(mapper);
5735 processSync(mapper);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5739 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5740 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5744 ASSERT_EQ(0, motionArgs.flags);
5745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5746 ASSERT_EQ(0, motionArgs.buttonState);
5747 ASSERT_EQ(0, motionArgs.edgeFlags);
5748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5752 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5753 1, 0, 0, 0, 0, 0, 0, 0));
5754 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5755 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5757
5758 // Should not have sent any more keys or motions.
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5761}
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 addConfigurationProperty("touch.deviceType", "touchScreen");
5765 prepareDisplay(DISPLAY_ORIENTATION_0);
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005769 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
arthurhungdcef2dc2020-08-11 14:47:50 +08005771 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 NotifyMotionArgs motionArgs;
5774
5775 // Down.
5776 int32_t x = 100;
5777 int32_t y = 125;
5778 processDown(mapper, x, y);
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.flags);
5788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790 ASSERT_EQ(0, motionArgs.edgeFlags);
5791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5795 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5799
5800 // Move.
5801 x += 50;
5802 y += 75;
5803 processMove(mapper, x, y);
5804 processSync(mapper);
5805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5809 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5810 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5812 ASSERT_EQ(0, motionArgs.flags);
5813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5814 ASSERT_EQ(0, motionArgs.buttonState);
5815 ASSERT_EQ(0, motionArgs.edgeFlags);
5816 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5820 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5821 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5822 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5824
5825 // Up.
5826 processUp(mapper);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5847
5848 // Should not have sent any more keys or motions.
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5851}
5852
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005853TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareButtons();
5856 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005857 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5858 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860
5861 NotifyMotionArgs args;
5862
5863 // Rotation 90.
5864 prepareDisplay(DISPLAY_ORIENTATION_90);
5865 processDown(mapper, toRawX(50), toRawY(75));
5866 processSync(mapper);
5867
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5869 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5870 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5871
5872 processUp(mapper);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5875}
5876
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005877TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878 addConfigurationProperty("touch.deviceType", "touchScreen");
5879 prepareButtons();
5880 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005881 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5882 // orientation-aware are affected by display rotation.
5883 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005884 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 NotifyMotionArgs args;
5887
5888 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005889 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 processDown(mapper, toRawX(50), toRawY(75));
5892 processSync(mapper);
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5895 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5896 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5897
5898 processUp(mapper);
5899 processSync(mapper);
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5901
5902 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005903 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005905 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 processSync(mapper);
5907
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5909 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5910 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5911
5912 processUp(mapper);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5915
5916 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005917 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 prepareDisplay(DISPLAY_ORIENTATION_180);
5919 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5920 processSync(mapper);
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5923 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5924 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5925
5926 processUp(mapper);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5929
5930 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005931 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005933 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 processSync(mapper);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5937 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5938 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5939
5940 processUp(mapper);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5943}
5944
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005945TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5946 addConfigurationProperty("touch.deviceType", "touchScreen");
5947 prepareButtons();
5948 prepareAxes(POSITION);
5949 addConfigurationProperty("touch.orientationAware", "1");
5950 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5951 clearViewports();
5952 prepareDisplay(DISPLAY_ORIENTATION_0);
5953 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5954 NotifyMotionArgs args;
5955
5956 // Orientation 0.
5957 processDown(mapper, toRawX(50), toRawY(75));
5958 processSync(mapper);
5959
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5961 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5962 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5963
5964 processUp(mapper);
5965 processSync(mapper);
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5967}
5968
5969TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5970 addConfigurationProperty("touch.deviceType", "touchScreen");
5971 prepareButtons();
5972 prepareAxes(POSITION);
5973 addConfigurationProperty("touch.orientationAware", "1");
5974 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5975 clearViewports();
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5978 NotifyMotionArgs args;
5979
5980 // Orientation 90.
5981 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5982 processSync(mapper);
5983
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991}
5992
5993TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareButtons();
5996 prepareAxes(POSITION);
5997 addConfigurationProperty("touch.orientationAware", "1");
5998 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5999 clearViewports();
6000 prepareDisplay(DISPLAY_ORIENTATION_0);
6001 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6002 NotifyMotionArgs args;
6003
6004 // Orientation 180.
6005 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6006 processSync(mapper);
6007
6008 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6009 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6010 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6011
6012 processUp(mapper);
6013 processSync(mapper);
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6015}
6016
6017TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6018 addConfigurationProperty("touch.deviceType", "touchScreen");
6019 prepareButtons();
6020 prepareAxes(POSITION);
6021 addConfigurationProperty("touch.orientationAware", "1");
6022 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6023 clearViewports();
6024 prepareDisplay(DISPLAY_ORIENTATION_0);
6025 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6026 NotifyMotionArgs args;
6027
6028 // Orientation 270.
6029 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6030 processSync(mapper);
6031
6032 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039}
6040
6041TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareButtons();
6044 prepareAxes(POSITION);
6045 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6046 // orientation-aware are affected by display rotation.
6047 addConfigurationProperty("touch.orientationAware", "0");
6048 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6049 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6050
6051 NotifyMotionArgs args;
6052
6053 // Orientation 90, Rotation 0.
6054 clearViewports();
6055 prepareDisplay(DISPLAY_ORIENTATION_0);
6056 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6057 processSync(mapper);
6058
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6061 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6062
6063 processUp(mapper);
6064 processSync(mapper);
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6066
6067 // Orientation 90, Rotation 90.
6068 clearViewports();
6069 prepareDisplay(DISPLAY_ORIENTATION_90);
6070 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6071 processSync(mapper);
6072
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6075 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6076
6077 processUp(mapper);
6078 processSync(mapper);
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6080
6081 // Orientation 90, Rotation 180.
6082 clearViewports();
6083 prepareDisplay(DISPLAY_ORIENTATION_180);
6084 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6085 processSync(mapper);
6086
6087 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6088 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6089 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6090
6091 processUp(mapper);
6092 processSync(mapper);
6093 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6094
6095 // Orientation 90, Rotation 270.
6096 clearViewports();
6097 prepareDisplay(DISPLAY_ORIENTATION_270);
6098 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6099 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6100 processSync(mapper);
6101
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6103 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6104 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6105
6106 processUp(mapper);
6107 processSync(mapper);
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6109}
6110
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareButtons();
6115 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117
6118 // These calculations are based on the input device calibration documentation.
6119 int32_t rawX = 100;
6120 int32_t rawY = 200;
6121 int32_t rawPressure = 10;
6122 int32_t rawToolMajor = 12;
6123 int32_t rawDistance = 2;
6124 int32_t rawTiltX = 30;
6125 int32_t rawTiltY = 110;
6126
6127 float x = toDisplayX(rawX);
6128 float y = toDisplayY(rawY);
6129 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6130 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6131 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6132 float distance = float(rawDistance);
6133
6134 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6135 float tiltScale = M_PI / 180;
6136 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6137 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6138 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6139 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6140
6141 processDown(mapper, rawX, rawY);
6142 processPressure(mapper, rawPressure);
6143 processToolMajor(mapper, rawToolMajor);
6144 processDistance(mapper, rawDistance);
6145 processTilt(mapper, rawTiltX, rawTiltY);
6146 processSync(mapper);
6147
6148 NotifyMotionArgs args;
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6151 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6152 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6153}
6154
Jason Gerecke489fda82012-09-07 17:19:40 -07006155TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareDisplay(DISPLAY_ORIENTATION_0);
6158 prepareLocationCalibration();
6159 prepareButtons();
6160 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006161 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006162
6163 int32_t rawX = 100;
6164 int32_t rawY = 200;
6165
6166 float x = toDisplayX(toCookedX(rawX, rawY));
6167 float y = toDisplayY(toCookedY(rawX, rawY));
6168
6169 processDown(mapper, rawX, rawY);
6170 processSync(mapper);
6171
6172 NotifyMotionArgs args;
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6175 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6176}
6177
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 addConfigurationProperty("touch.deviceType", "touchScreen");
6180 prepareDisplay(DISPLAY_ORIENTATION_0);
6181 prepareButtons();
6182 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 NotifyMotionArgs motionArgs;
6186 NotifyKeyArgs keyArgs;
6187
6188 processDown(mapper, 100, 200);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6192 ASSERT_EQ(0, motionArgs.buttonState);
6193
6194 // press BTN_LEFT, release BTN_LEFT
6195 processKey(mapper, BTN_LEFT, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6200
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 processKey(mapper, BTN_LEFT, 0);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6216 processKey(mapper, BTN_RIGHT, 1);
6217 processKey(mapper, BTN_MIDDLE, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6222 motionArgs.buttonState);
6223
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6231 motionArgs.buttonState);
6232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 processKey(mapper, BTN_RIGHT, 0);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 processKey(mapper, BTN_MIDDLE, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
6249 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 // press BTN_BACK, release BTN_BACK
6254 processKey(mapper, BTN_BACK, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6258 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 processKey(mapper, BTN_BACK, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
6277
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6280 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6281
6282 // press BTN_SIDE, release BTN_SIDE
6283 processKey(mapper, BTN_SIDE, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 processKey(mapper, BTN_SIDE, 0);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
6306
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6310
6311 // press BTN_FORWARD, release BTN_FORWARD
6312 processKey(mapper, BTN_FORWARD, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6316 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 processKey(mapper, BTN_FORWARD, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
6335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6338 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6339
6340 // press BTN_EXTRA, release BTN_EXTRA
6341 processKey(mapper, BTN_EXTRA, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6345 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_EXTRA, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6368
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 // press BTN_STYLUS, release BTN_STYLUS
6372 processKey(mapper, BTN_STYLUS, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6380 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 processKey(mapper, BTN_STYLUS, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // press BTN_STYLUS2, release BTN_STYLUS2
6393 processKey(mapper, BTN_STYLUS2, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6401 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 processKey(mapper, BTN_STYLUS2, 0);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 // release touch
6414 processUp(mapper);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6418 ASSERT_EQ(0, motionArgs.buttonState);
6419}
6420
6421TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 addConfigurationProperty("touch.deviceType", "touchScreen");
6423 prepareDisplay(DISPLAY_ORIENTATION_0);
6424 prepareButtons();
6425 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006426 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427
6428 NotifyMotionArgs motionArgs;
6429
6430 // default tool type is finger
6431 processDown(mapper, 100, 200);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436
6437 // eraser
6438 processKey(mapper, BTN_TOOL_RUBBER, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6443
6444 // stylus
6445 processKey(mapper, BTN_TOOL_RUBBER, 0);
6446 processKey(mapper, BTN_TOOL_PEN, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6451
6452 // brush
6453 processKey(mapper, BTN_TOOL_PEN, 0);
6454 processKey(mapper, BTN_TOOL_BRUSH, 1);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6459
6460 // pencil
6461 processKey(mapper, BTN_TOOL_BRUSH, 0);
6462 processKey(mapper, BTN_TOOL_PENCIL, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6467
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006468 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 processKey(mapper, BTN_TOOL_PENCIL, 0);
6470 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6475
6476 // mouse
6477 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6478 processKey(mapper, BTN_TOOL_MOUSE, 1);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6483
6484 // lens
6485 processKey(mapper, BTN_TOOL_MOUSE, 0);
6486 processKey(mapper, BTN_TOOL_LENS, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6491
6492 // double-tap
6493 processKey(mapper, BTN_TOOL_LENS, 0);
6494 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6499
6500 // triple-tap
6501 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6502 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507
6508 // quad-tap
6509 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6510 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6515
6516 // finger
6517 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6518 processKey(mapper, BTN_TOOL_FINGER, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6523
6524 // stylus trumps finger
6525 processKey(mapper, BTN_TOOL_PEN, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6530
6531 // eraser trumps stylus
6532 processKey(mapper, BTN_TOOL_RUBBER, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6537
6538 // mouse trumps eraser
6539 processKey(mapper, BTN_TOOL_MOUSE, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6544
6545 // back to default tool type
6546 processKey(mapper, BTN_TOOL_MOUSE, 0);
6547 processKey(mapper, BTN_TOOL_RUBBER, 0);
6548 processKey(mapper, BTN_TOOL_PEN, 0);
6549 processKey(mapper, BTN_TOOL_FINGER, 0);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554}
6555
6556TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 addConfigurationProperty("touch.deviceType", "touchScreen");
6558 prepareDisplay(DISPLAY_ORIENTATION_0);
6559 prepareButtons();
6560 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006561 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563
6564 NotifyMotionArgs motionArgs;
6565
6566 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6567 processKey(mapper, BTN_TOOL_FINGER, 1);
6568 processMove(mapper, 100, 200);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6573 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6579
6580 // move a little
6581 processMove(mapper, 150, 250);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6587
6588 // down when BTN_TOUCH is pressed, pressure defaults to 1
6589 processKey(mapper, BTN_TOUCH, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6595
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6599 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6600
6601 // up when BTN_TOUCH is released, hover restored
6602 processKey(mapper, BTN_TOUCH, 0);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6608
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6612 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6618
6619 // exit hover when pointer goes away
6620 processKey(mapper, BTN_TOOL_FINGER, 0);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6626}
6627
6628TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006629 addConfigurationProperty("touch.deviceType", "touchScreen");
6630 prepareDisplay(DISPLAY_ORIENTATION_0);
6631 prepareButtons();
6632 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006633 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 NotifyMotionArgs motionArgs;
6636
6637 // initially hovering because pressure is 0
6638 processDown(mapper, 100, 200);
6639 processPressure(mapper, 0);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 // move a little
6652 processMove(mapper, 150, 250);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // down when pressure is non-zero
6660 processPressure(mapper, RAW_PRESSURE_MAX);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // up when pressure becomes 0, hover restored
6673 processPressure(mapper, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // exit hover when pointer goes away
6691 processUp(mapper);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697}
6698
lilinnan687e58f2022-07-19 16:00:50 +08006699TEST_F(SingleTouchInputMapperTest,
6700 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6701 addConfigurationProperty("touch.deviceType", "touchScreen");
6702 prepareDisplay(DISPLAY_ORIENTATION_0);
6703 prepareButtons();
6704 prepareAxes(POSITION);
6705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6706 NotifyMotionArgs motionArgs;
6707
6708 // Down.
6709 int32_t x = 100;
6710 int32_t y = 200;
6711 processDown(mapper, x, y);
6712 processSync(mapper);
6713
6714 // We should receive a down event
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6716 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6717
6718 // Change display id
6719 clearViewports();
6720 prepareSecondaryDisplay(ViewportType::INTERNAL);
6721
6722 // We should receive a cancel event
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6724 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6725 // Then receive reset called
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6727}
6728
Prabir Pradhan5632d622021-09-06 07:57:20 -07006729// --- TouchDisplayProjectionTest ---
6730
6731class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6732public:
6733 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6734 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6735 // rotated equivalent of the given un-rotated physical display bounds.
6736 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6737 uint32_t inverseRotationFlags;
6738 auto width = DISPLAY_WIDTH;
6739 auto height = DISPLAY_HEIGHT;
6740 switch (orientation) {
6741 case DISPLAY_ORIENTATION_90:
6742 inverseRotationFlags = ui::Transform::ROT_270;
6743 std::swap(width, height);
6744 break;
6745 case DISPLAY_ORIENTATION_180:
6746 inverseRotationFlags = ui::Transform::ROT_180;
6747 break;
6748 case DISPLAY_ORIENTATION_270:
6749 inverseRotationFlags = ui::Transform::ROT_90;
6750 std::swap(width, height);
6751 break;
6752 case DISPLAY_ORIENTATION_0:
6753 inverseRotationFlags = ui::Transform::ROT_0;
6754 break;
6755 default:
6756 FAIL() << "Invalid orientation: " << orientation;
6757 }
6758
6759 const ui::Transform rotation(inverseRotationFlags, width, height);
6760 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6761
6762 std::optional<DisplayViewport> internalViewport =
6763 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6764 DisplayViewport& v = *internalViewport;
6765 v.displayId = DISPLAY_ID;
6766 v.orientation = orientation;
6767
6768 v.logicalLeft = 0;
6769 v.logicalTop = 0;
6770 v.logicalRight = 100;
6771 v.logicalBottom = 100;
6772
6773 v.physicalLeft = rotatedPhysicalDisplay.left;
6774 v.physicalTop = rotatedPhysicalDisplay.top;
6775 v.physicalRight = rotatedPhysicalDisplay.right;
6776 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6777
6778 v.deviceWidth = width;
6779 v.deviceHeight = height;
6780
6781 v.isActive = true;
6782 v.uniqueId = UNIQUE_ID;
6783 v.type = ViewportType::INTERNAL;
6784 mFakePolicy->updateViewport(v);
6785 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6786 }
6787
6788 void assertReceivedMove(const Point& point) {
6789 NotifyMotionArgs motionArgs;
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6792 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6794 1, 0, 0, 0, 0, 0, 0, 0));
6795 }
6796};
6797
6798TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6799 addConfigurationProperty("touch.deviceType", "touchScreen");
6800 prepareDisplay(DISPLAY_ORIENTATION_0);
6801
6802 prepareButtons();
6803 prepareAxes(POSITION);
6804 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6805
6806 NotifyMotionArgs motionArgs;
6807
6808 // Configure the DisplayViewport such that the logical display maps to a subsection of
6809 // the display panel called the physical display. Here, the physical display is bounded by the
6810 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6811 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6812 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6813 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6814
6815 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6816 DISPLAY_ORIENTATION_270}) {
6817 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6818
6819 // Touches outside the physical display should be ignored, and should not generate any
6820 // events. Ensure touches at the following points that lie outside of the physical display
6821 // area do not generate any events.
6822 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6823 processDown(mapper, toRawX(point.x), toRawY(point.y));
6824 processSync(mapper);
6825 processUp(mapper);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6828 << "Unexpected event generated for touch outside physical display at point: "
6829 << point.x << ", " << point.y;
6830 }
6831 }
6832}
6833
6834TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6835 addConfigurationProperty("touch.deviceType", "touchScreen");
6836 prepareDisplay(DISPLAY_ORIENTATION_0);
6837
6838 prepareButtons();
6839 prepareAxes(POSITION);
6840 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6841
6842 NotifyMotionArgs motionArgs;
6843
6844 // Configure the DisplayViewport such that the logical display maps to a subsection of
6845 // the display panel called the physical display. Here, the physical display is bounded by the
6846 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6847 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6848
6849 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6850 DISPLAY_ORIENTATION_270}) {
6851 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6852
6853 // Touches that start outside the physical display should be ignored until it enters the
6854 // physical display bounds, at which point it should generate a down event. Start a touch at
6855 // the point (5, 100), which is outside the physical display bounds.
6856 static const Point kOutsidePoint{5, 100};
6857 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6860
6861 // Move the touch into the physical display area. This should generate a pointer down.
6862 processMove(mapper, toRawX(11), toRawY(21));
6863 processSync(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6866 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6867 ASSERT_NO_FATAL_FAILURE(
6868 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6869
6870 // Move the touch inside the physical display area. This should generate a pointer move.
6871 processMove(mapper, toRawX(69), toRawY(159));
6872 processSync(mapper);
6873 assertReceivedMove({69, 159});
6874
6875 // Move outside the physical display area. Since the pointer is already down, this should
6876 // now continue generating events.
6877 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6878 processSync(mapper);
6879 assertReceivedMove(kOutsidePoint);
6880
6881 // Release. This should generate a pointer up.
6882 processUp(mapper);
6883 processSync(mapper);
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6887 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6888
6889 // Ensure no more events were generated.
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6892 }
6893}
6894
Michael Wrightd02c5b62014-02-10 15:10:22 -08006895// --- MultiTouchInputMapperTest ---
6896
6897class MultiTouchInputMapperTest : public TouchInputMapperTest {
6898protected:
6899 void prepareAxes(int axes);
6900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006901 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6902 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6903 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6904 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6905 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6906 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6907 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6908 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6909 void processId(MultiTouchInputMapper& mapper, int32_t id);
6910 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6911 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6912 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6913 void processMTSync(MultiTouchInputMapper& mapper);
6914 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915};
6916
6917void MultiTouchInputMapperTest::prepareAxes(int axes) {
6918 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 }
6922 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6924 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006926 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6927 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 }
6929 }
6930 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6932 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006935 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 }
6937 }
6938 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006939 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6940 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006941 }
6942 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6944 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945 }
6946 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006947 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6948 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 }
6950 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6952 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006953 }
6954 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6956 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957 }
6958 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960 }
6961}
6962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006963void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6964 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006967}
6968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6970 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972}
6973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6975 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977}
6978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006979void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981}
6982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006983void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985}
6986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6988 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990}
6991
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006992void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994}
6995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006996void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998}
6999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007000void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002}
7003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007004void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006}
7007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007008void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010}
7011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007012void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7013 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007015}
7016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007017void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019}
7020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007021void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023}
7024
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 addConfigurationProperty("touch.deviceType", "touchScreen");
7027 prepareDisplay(DISPLAY_ORIENTATION_0);
7028 prepareAxes(POSITION);
7029 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031
arthurhungdcef2dc2020-08-11 14:47:50 +08007032 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033
7034 NotifyMotionArgs motionArgs;
7035
7036 // Two fingers down at once.
7037 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7038 processPosition(mapper, x1, y1);
7039 processMTSync(mapper);
7040 processPosition(mapper, x2, y2);
7041 processMTSync(mapper);
7042 processSync(mapper);
7043
7044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7046 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7047 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7048 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7050 ASSERT_EQ(0, motionArgs.flags);
7051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7052 ASSERT_EQ(0, motionArgs.buttonState);
7053 ASSERT_EQ(0, motionArgs.edgeFlags);
7054 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7058 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7059 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7060 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7061 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7062
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7067 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007068 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069 ASSERT_EQ(0, motionArgs.flags);
7070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7071 ASSERT_EQ(0, motionArgs.buttonState);
7072 ASSERT_EQ(0, motionArgs.edgeFlags);
7073 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7074 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7076 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7079 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7081 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7082 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7083 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7084 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7085
7086 // Move.
7087 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7088 processPosition(mapper, x1, y1);
7089 processMTSync(mapper);
7090 processPosition(mapper, x2, y2);
7091 processMTSync(mapper);
7092 processSync(mapper);
7093
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7095 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7096 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7097 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7098 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7100 ASSERT_EQ(0, motionArgs.flags);
7101 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7102 ASSERT_EQ(0, motionArgs.buttonState);
7103 ASSERT_EQ(0, motionArgs.edgeFlags);
7104 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7107 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7110 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7113 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7114 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7115 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7116
7117 // First finger up.
7118 x2 += 15; y2 -= 20;
7119 processPosition(mapper, x2, y2);
7120 processMTSync(mapper);
7121 processSync(mapper);
7122
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7126 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7127 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007128 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(0, motionArgs.flags);
7130 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7131 ASSERT_EQ(0, motionArgs.buttonState);
7132 ASSERT_EQ(0, motionArgs.edgeFlags);
7133 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7134 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7136 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7139 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7140 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7141 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7142 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7143 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7145
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7147 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7148 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7149 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7150 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7152 ASSERT_EQ(0, motionArgs.flags);
7153 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7154 ASSERT_EQ(0, motionArgs.buttonState);
7155 ASSERT_EQ(0, motionArgs.edgeFlags);
7156 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7157 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7160 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7161 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7162 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7164
7165 // Move.
7166 x2 += 20; y2 -= 25;
7167 processPosition(mapper, x2, y2);
7168 processMTSync(mapper);
7169 processSync(mapper);
7170
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7173 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7174 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7175 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7177 ASSERT_EQ(0, motionArgs.flags);
7178 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7179 ASSERT_EQ(0, motionArgs.buttonState);
7180 ASSERT_EQ(0, motionArgs.edgeFlags);
7181 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7182 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7185 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7186 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7187 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7188 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7189
7190 // New finger down.
7191 int32_t x3 = 700, y3 = 300;
7192 processPosition(mapper, x2, y2);
7193 processMTSync(mapper);
7194 processPosition(mapper, x3, y3);
7195 processMTSync(mapper);
7196 processSync(mapper);
7197
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7200 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7201 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7202 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007203 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 ASSERT_EQ(0, motionArgs.flags);
7205 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7206 ASSERT_EQ(0, motionArgs.buttonState);
7207 ASSERT_EQ(0, motionArgs.edgeFlags);
7208 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7209 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7211 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7214 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7216 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7217 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7218 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7220
7221 // Second finger up.
7222 x3 += 30; y3 -= 20;
7223 processPosition(mapper, x3, y3);
7224 processMTSync(mapper);
7225 processSync(mapper);
7226
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007232 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_EQ(0, motionArgs.flags);
7234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7235 ASSERT_EQ(0, motionArgs.buttonState);
7236 ASSERT_EQ(0, motionArgs.edgeFlags);
7237 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7240 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7243 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7245 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7246 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7247 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7249
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7252 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7253 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7254 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7256 ASSERT_EQ(0, motionArgs.flags);
7257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7258 ASSERT_EQ(0, motionArgs.buttonState);
7259 ASSERT_EQ(0, motionArgs.edgeFlags);
7260 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7261 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7264 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7265 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7266 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7268
7269 // Last finger up.
7270 processMTSync(mapper);
7271 processSync(mapper);
7272
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7275 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7279 ASSERT_EQ(0, motionArgs.flags);
7280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7281 ASSERT_EQ(0, motionArgs.buttonState);
7282 ASSERT_EQ(0, motionArgs.edgeFlags);
7283 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(x3), toDisplayY(y3), 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 // Should not have sent any more keys or motions.
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7295}
7296
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007297TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7298 addConfigurationProperty("touch.deviceType", "touchScreen");
7299 prepareDisplay(DISPLAY_ORIENTATION_0);
7300
7301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7302 /*fuzz*/ 0, /*resolution*/ 10);
7303 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7304 /*fuzz*/ 0, /*resolution*/ 11);
7305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7306 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7307 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7308 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7310 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7312 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7313
7314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7315
7316 // X and Y axes
7317 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7318 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7319 // Touch major and minor
7320 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7321 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7322 // Tool major and minor
7323 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7324 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7325}
7326
7327TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7328 addConfigurationProperty("touch.deviceType", "touchScreen");
7329 prepareDisplay(DISPLAY_ORIENTATION_0);
7330
7331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7332 /*fuzz*/ 0, /*resolution*/ 10);
7333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7334 /*fuzz*/ 0, /*resolution*/ 11);
7335
7336 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7337
7338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7339
7340 // Touch major and minor
7341 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7342 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7343 // Tool major and minor
7344 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7345 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7346}
7347
Michael Wrightd02c5b62014-02-10 15:10:22 -08007348TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349 addConfigurationProperty("touch.deviceType", "touchScreen");
7350 prepareDisplay(DISPLAY_ORIENTATION_0);
7351 prepareAxes(POSITION | ID);
7352 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354
arthurhungdcef2dc2020-08-11 14:47:50 +08007355 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007356
7357 NotifyMotionArgs motionArgs;
7358
7359 // Two fingers down at once.
7360 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7361 processPosition(mapper, x1, y1);
7362 processId(mapper, 1);
7363 processMTSync(mapper);
7364 processPosition(mapper, x2, y2);
7365 processId(mapper, 2);
7366 processMTSync(mapper);
7367 processSync(mapper);
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7372 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7375 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7376
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007378 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7382 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7388
7389 // Move.
7390 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7391 processPosition(mapper, x1, y1);
7392 processId(mapper, 1);
7393 processMTSync(mapper);
7394 processPosition(mapper, x2, y2);
7395 processId(mapper, 2);
7396 processMTSync(mapper);
7397 processSync(mapper);
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7401 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7404 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7407 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7409 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7410
7411 // First finger up.
7412 x2 += 15; y2 -= 20;
7413 processPosition(mapper, x2, y2);
7414 processId(mapper, 2);
7415 processMTSync(mapper);
7416 processSync(mapper);
7417
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007419 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007420 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7423 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7426 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7428 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7429
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7433 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7436 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7437
7438 // Move.
7439 x2 += 20; y2 -= 25;
7440 processPosition(mapper, x2, y2);
7441 processId(mapper, 2);
7442 processMTSync(mapper);
7443 processSync(mapper);
7444
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7448 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7452
7453 // New finger down.
7454 int32_t x3 = 700, y3 = 300;
7455 processPosition(mapper, x2, y2);
7456 processId(mapper, 2);
7457 processMTSync(mapper);
7458 processPosition(mapper, x3, y3);
7459 processId(mapper, 3);
7460 processMTSync(mapper);
7461 processSync(mapper);
7462
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007464 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007465 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7468 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7471 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7473 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7474
7475 // Second finger up.
7476 x3 += 30; y3 -= 20;
7477 processPosition(mapper, x3, y3);
7478 processId(mapper, 3);
7479 processMTSync(mapper);
7480 processSync(mapper);
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007483 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7487 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7492 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7493
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7496 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7497 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7500 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7501
7502 // Last finger up.
7503 processMTSync(mapper);
7504 processSync(mapper);
7505
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7508 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7509 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7512 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7513
7514 // Should not have sent any more keys or motions.
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7517}
7518
7519TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007520 addConfigurationProperty("touch.deviceType", "touchScreen");
7521 prepareDisplay(DISPLAY_ORIENTATION_0);
7522 prepareAxes(POSITION | ID | SLOT);
7523 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007524 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525
arthurhungdcef2dc2020-08-11 14:47:50 +08007526 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527
7528 NotifyMotionArgs motionArgs;
7529
7530 // Two fingers down at once.
7531 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7532 processPosition(mapper, x1, y1);
7533 processId(mapper, 1);
7534 processSlot(mapper, 1);
7535 processPosition(mapper, x2, y2);
7536 processId(mapper, 2);
7537 processSync(mapper);
7538
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7545 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007548 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7552 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7555 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 // Move.
7560 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7561 processSlot(mapper, 0);
7562 processPosition(mapper, x1, y1);
7563 processSlot(mapper, 1);
7564 processPosition(mapper, x2, y2);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7578
7579 // First finger up.
7580 x2 += 15; y2 -= 20;
7581 processSlot(mapper, 0);
7582 processId(mapper, -1);
7583 processSlot(mapper, 1);
7584 processPosition(mapper, x2, y2);
7585 processSync(mapper);
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007588 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7598
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7601 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7602 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7605 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7606
7607 // Move.
7608 x2 += 20; y2 -= 25;
7609 processPosition(mapper, x2, y2);
7610 processSync(mapper);
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7614 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7615 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7619
7620 // New finger down.
7621 int32_t x3 = 700, y3 = 300;
7622 processPosition(mapper, x2, y2);
7623 processSlot(mapper, 0);
7624 processId(mapper, 3);
7625 processPosition(mapper, x3, y3);
7626 processSync(mapper);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007629 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7633 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7636 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7638 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7639
7640 // Second finger up.
7641 x3 += 30; y3 -= 20;
7642 processSlot(mapper, 1);
7643 processId(mapper, -1);
7644 processSlot(mapper, 0);
7645 processPosition(mapper, x3, y3);
7646 processSync(mapper);
7647
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007649 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7653 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7656 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7658 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7662 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7664 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7666 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7667
7668 // Last finger up.
7669 processId(mapper, -1);
7670 processSync(mapper);
7671
7672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7674 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // Should not have sent any more keys or motions.
7681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7683}
7684
7685TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 addConfigurationProperty("touch.deviceType", "touchScreen");
7687 prepareDisplay(DISPLAY_ORIENTATION_0);
7688 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007689 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690
7691 // These calculations are based on the input device calibration documentation.
7692 int32_t rawX = 100;
7693 int32_t rawY = 200;
7694 int32_t rawTouchMajor = 7;
7695 int32_t rawTouchMinor = 6;
7696 int32_t rawToolMajor = 9;
7697 int32_t rawToolMinor = 8;
7698 int32_t rawPressure = 11;
7699 int32_t rawDistance = 0;
7700 int32_t rawOrientation = 3;
7701 int32_t id = 5;
7702
7703 float x = toDisplayX(rawX);
7704 float y = toDisplayY(rawY);
7705 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7706 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7707 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7708 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7709 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7710 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7711 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7712 float distance = float(rawDistance);
7713
7714 processPosition(mapper, rawX, rawY);
7715 processTouchMajor(mapper, rawTouchMajor);
7716 processTouchMinor(mapper, rawTouchMinor);
7717 processToolMajor(mapper, rawToolMajor);
7718 processToolMinor(mapper, rawToolMinor);
7719 processPressure(mapper, rawPressure);
7720 processOrientation(mapper, rawOrientation);
7721 processDistance(mapper, rawDistance);
7722 processId(mapper, id);
7723 processMTSync(mapper);
7724 processSync(mapper);
7725
7726 NotifyMotionArgs args;
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7728 ASSERT_EQ(0, args.pointerProperties[0].id);
7729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7730 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7731 orientation, distance));
7732}
7733
7734TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 addConfigurationProperty("touch.deviceType", "touchScreen");
7736 prepareDisplay(DISPLAY_ORIENTATION_0);
7737 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7738 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007739 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740
7741 // These calculations are based on the input device calibration documentation.
7742 int32_t rawX = 100;
7743 int32_t rawY = 200;
7744 int32_t rawTouchMajor = 140;
7745 int32_t rawTouchMinor = 120;
7746 int32_t rawToolMajor = 180;
7747 int32_t rawToolMinor = 160;
7748
7749 float x = toDisplayX(rawX);
7750 float y = toDisplayY(rawY);
7751 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7752 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7753 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7754 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7755 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7756
7757 processPosition(mapper, rawX, rawY);
7758 processTouchMajor(mapper, rawTouchMajor);
7759 processTouchMinor(mapper, rawTouchMinor);
7760 processToolMajor(mapper, rawToolMajor);
7761 processToolMinor(mapper, rawToolMinor);
7762 processMTSync(mapper);
7763 processSync(mapper);
7764
7765 NotifyMotionArgs args;
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7768 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7769}
7770
7771TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 addConfigurationProperty("touch.deviceType", "touchScreen");
7773 prepareDisplay(DISPLAY_ORIENTATION_0);
7774 prepareAxes(POSITION | TOUCH | TOOL);
7775 addConfigurationProperty("touch.size.calibration", "diameter");
7776 addConfigurationProperty("touch.size.scale", "10");
7777 addConfigurationProperty("touch.size.bias", "160");
7778 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007779 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780
7781 // These calculations are based on the input device calibration documentation.
7782 // Note: We only provide a single common touch/tool value because the device is assumed
7783 // not to emit separate values for each pointer (isSummed = 1).
7784 int32_t rawX = 100;
7785 int32_t rawY = 200;
7786 int32_t rawX2 = 150;
7787 int32_t rawY2 = 250;
7788 int32_t rawTouchMajor = 5;
7789 int32_t rawToolMajor = 8;
7790
7791 float x = toDisplayX(rawX);
7792 float y = toDisplayY(rawY);
7793 float x2 = toDisplayX(rawX2);
7794 float y2 = toDisplayY(rawY2);
7795 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7796 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7797 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7798
7799 processPosition(mapper, rawX, rawY);
7800 processTouchMajor(mapper, rawTouchMajor);
7801 processToolMajor(mapper, rawToolMajor);
7802 processMTSync(mapper);
7803 processPosition(mapper, rawX2, rawY2);
7804 processTouchMajor(mapper, rawTouchMajor);
7805 processToolMajor(mapper, rawToolMajor);
7806 processMTSync(mapper);
7807 processSync(mapper);
7808
7809 NotifyMotionArgs args;
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7812
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007814 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_EQ(size_t(2), args.pointerCount);
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7817 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7819 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7820}
7821
7822TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007823 addConfigurationProperty("touch.deviceType", "touchScreen");
7824 prepareDisplay(DISPLAY_ORIENTATION_0);
7825 prepareAxes(POSITION | TOUCH | TOOL);
7826 addConfigurationProperty("touch.size.calibration", "area");
7827 addConfigurationProperty("touch.size.scale", "43");
7828 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830
7831 // These calculations are based on the input device calibration documentation.
7832 int32_t rawX = 100;
7833 int32_t rawY = 200;
7834 int32_t rawTouchMajor = 5;
7835 int32_t rawToolMajor = 8;
7836
7837 float x = toDisplayX(rawX);
7838 float y = toDisplayY(rawY);
7839 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7840 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7841 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7842
7843 processPosition(mapper, rawX, rawY);
7844 processTouchMajor(mapper, rawTouchMajor);
7845 processToolMajor(mapper, rawToolMajor);
7846 processMTSync(mapper);
7847 processSync(mapper);
7848
7849 NotifyMotionArgs args;
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7852 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7853}
7854
7855TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
7857 prepareDisplay(DISPLAY_ORIENTATION_0);
7858 prepareAxes(POSITION | PRESSURE);
7859 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7860 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862
Michael Wrightaa449c92017-12-13 21:21:43 +00007863 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007864 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007865 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7866 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7867 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7868
Michael Wrightd02c5b62014-02-10 15:10:22 -08007869 // These calculations are based on the input device calibration documentation.
7870 int32_t rawX = 100;
7871 int32_t rawY = 200;
7872 int32_t rawPressure = 60;
7873
7874 float x = toDisplayX(rawX);
7875 float y = toDisplayY(rawY);
7876 float pressure = float(rawPressure) * 0.01f;
7877
7878 processPosition(mapper, rawX, rawY);
7879 processPressure(mapper, rawPressure);
7880 processMTSync(mapper);
7881 processSync(mapper);
7882
7883 NotifyMotionArgs args;
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7886 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7887}
7888
7889TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890 addConfigurationProperty("touch.deviceType", "touchScreen");
7891 prepareDisplay(DISPLAY_ORIENTATION_0);
7892 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007893 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894
7895 NotifyMotionArgs motionArgs;
7896 NotifyKeyArgs keyArgs;
7897
7898 processId(mapper, 1);
7899 processPosition(mapper, 100, 200);
7900 processSync(mapper);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7903 ASSERT_EQ(0, motionArgs.buttonState);
7904
7905 // press BTN_LEFT, release BTN_LEFT
7906 processKey(mapper, BTN_LEFT, 1);
7907 processSync(mapper);
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7910 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7911
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7914 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7915
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 processKey(mapper, BTN_LEFT, 0);
7917 processSync(mapper);
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007919 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007921
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007924 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925
7926 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7927 processKey(mapper, BTN_RIGHT, 1);
7928 processKey(mapper, BTN_MIDDLE, 1);
7929 processSync(mapper);
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7932 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7933 motionArgs.buttonState);
7934
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7937 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7942 motionArgs.buttonState);
7943
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 processKey(mapper, BTN_RIGHT, 0);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007949
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007952 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953
7954 processKey(mapper, BTN_MIDDLE, 0);
7955 processSync(mapper);
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007959
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007962 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007963
7964 // press BTN_BACK, release BTN_BACK
7965 processKey(mapper, BTN_BACK, 1);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7968 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7969 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007970
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007973 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7974
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7977 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978
7979 processKey(mapper, BTN_BACK, 0);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007987 ASSERT_EQ(0, motionArgs.buttonState);
7988
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7990 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7991 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7992
7993 // press BTN_SIDE, release BTN_SIDE
7994 processKey(mapper, BTN_SIDE, 1);
7995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7997 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7998 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007999
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008002 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8003
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8006 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007
8008 processKey(mapper, BTN_SIDE, 0);
8009 processSync(mapper);
8010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008013
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008016 ASSERT_EQ(0, motionArgs.buttonState);
8017
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8019 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8020 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8021
8022 // press BTN_FORWARD, release BTN_FORWARD
8023 processKey(mapper, BTN_FORWARD, 1);
8024 processSync(mapper);
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8026 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8027 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008028
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008031 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8032
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8035 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036
8037 processKey(mapper, BTN_FORWARD, 0);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008042
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008045 ASSERT_EQ(0, motionArgs.buttonState);
8046
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8048 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8049 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8050
8051 // press BTN_EXTRA, release BTN_EXTRA
8052 processKey(mapper, BTN_EXTRA, 1);
8053 processSync(mapper);
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8055 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8056 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008057
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008060 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8061
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8064 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065
8066 processKey(mapper, BTN_EXTRA, 0);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(0, motionArgs.buttonState);
8075
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8077 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8078 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8079
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8081
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 // press BTN_STYLUS, release BTN_STYLUS
8083 processKey(mapper, BTN_STYLUS, 1);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008087 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8091 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 processKey(mapper, BTN_STYLUS, 0);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 // press BTN_STYLUS2, release BTN_STYLUS2
8104 processKey(mapper, BTN_STYLUS2, 1);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8109
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113
8114 processKey(mapper, BTN_STYLUS2, 0);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123
8124 // release touch
8125 processId(mapper, -1);
8126 processSync(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8129 ASSERT_EQ(0, motionArgs.buttonState);
8130}
8131
8132TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 addConfigurationProperty("touch.deviceType", "touchScreen");
8134 prepareDisplay(DISPLAY_ORIENTATION_0);
8135 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008136 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137
8138 NotifyMotionArgs motionArgs;
8139
8140 // default tool type is finger
8141 processId(mapper, 1);
8142 processPosition(mapper, 100, 200);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8147
8148 // eraser
8149 processKey(mapper, BTN_TOOL_RUBBER, 1);
8150 processSync(mapper);
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8154
8155 // stylus
8156 processKey(mapper, BTN_TOOL_RUBBER, 0);
8157 processKey(mapper, BTN_TOOL_PEN, 1);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8162
8163 // brush
8164 processKey(mapper, BTN_TOOL_PEN, 0);
8165 processKey(mapper, BTN_TOOL_BRUSH, 1);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8170
8171 // pencil
8172 processKey(mapper, BTN_TOOL_BRUSH, 0);
8173 processKey(mapper, BTN_TOOL_PENCIL, 1);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8178
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008179 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 processKey(mapper, BTN_TOOL_PENCIL, 0);
8181 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8186
8187 // mouse
8188 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8189 processKey(mapper, BTN_TOOL_MOUSE, 1);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8194
8195 // lens
8196 processKey(mapper, BTN_TOOL_MOUSE, 0);
8197 processKey(mapper, BTN_TOOL_LENS, 1);
8198 processSync(mapper);
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8202
8203 // double-tap
8204 processKey(mapper, BTN_TOOL_LENS, 0);
8205 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8210
8211 // triple-tap
8212 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8213 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8214 processSync(mapper);
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8218
8219 // quad-tap
8220 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8221 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8226
8227 // finger
8228 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8229 processKey(mapper, BTN_TOOL_FINGER, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8234
8235 // stylus trumps finger
8236 processKey(mapper, BTN_TOOL_PEN, 1);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8241
8242 // eraser trumps stylus
8243 processKey(mapper, BTN_TOOL_RUBBER, 1);
8244 processSync(mapper);
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8248
8249 // mouse trumps eraser
8250 processKey(mapper, BTN_TOOL_MOUSE, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8255
8256 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8257 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8258 processSync(mapper);
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8262
8263 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8264 processToolType(mapper, MT_TOOL_PEN);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8268 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8269
8270 // back to default tool type
8271 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8272 processKey(mapper, BTN_TOOL_MOUSE, 0);
8273 processKey(mapper, BTN_TOOL_RUBBER, 0);
8274 processKey(mapper, BTN_TOOL_PEN, 0);
8275 processKey(mapper, BTN_TOOL_FINGER, 0);
8276 processSync(mapper);
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8280}
8281
8282TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 addConfigurationProperty("touch.deviceType", "touchScreen");
8284 prepareDisplay(DISPLAY_ORIENTATION_0);
8285 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008286 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288
8289 NotifyMotionArgs motionArgs;
8290
8291 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8292 processId(mapper, 1);
8293 processPosition(mapper, 100, 200);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8299
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8303 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8304
8305 // move a little
8306 processPosition(mapper, 150, 250);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8311 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8312
8313 // down when BTN_TOUCH is pressed, pressure defaults to 1
8314 processKey(mapper, BTN_TOUCH, 1);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8319 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8320
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8324 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8325
8326 // up when BTN_TOUCH is released, hover restored
8327 processKey(mapper, BTN_TOUCH, 0);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8332 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8338
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8342 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8343
8344 // exit hover when pointer goes away
8345 processId(mapper, -1);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8350 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8351}
8352
8353TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 addConfigurationProperty("touch.deviceType", "touchScreen");
8355 prepareDisplay(DISPLAY_ORIENTATION_0);
8356 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008357 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358
8359 NotifyMotionArgs motionArgs;
8360
8361 // initially hovering because pressure is 0
8362 processId(mapper, 1);
8363 processPosition(mapper, 100, 200);
8364 processPressure(mapper, 0);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8370
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8375
8376 // move a little
8377 processPosition(mapper, 150, 250);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8382 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8383
8384 // down when pressure becomes non-zero
8385 processPressure(mapper, RAW_PRESSURE_MAX);
8386 processSync(mapper);
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8390 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8391
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8393 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8395 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8396
8397 // up when pressure becomes 0, hover restored
8398 processPressure(mapper, 0);
8399 processSync(mapper);
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8403 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8404
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8408 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8413 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8414
8415 // exit hover when pointer goes away
8416 processId(mapper, -1);
8417 processSync(mapper);
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8421 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8422}
8423
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008424/**
8425 * Set the input device port <--> display port associations, and check that the
8426 * events are routed to the display that matches the display port.
8427 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8428 */
8429TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008430 const std::string usb2 = "USB2";
8431 const uint8_t hdmi1 = 0;
8432 const uint8_t hdmi2 = 1;
8433 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008434 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008435
8436 addConfigurationProperty("touch.deviceType", "touchScreen");
8437 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008439
8440 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8441 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8442
8443 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8444 // for this input device is specified, and the matching viewport is not present,
8445 // the input device should be disabled (at the mapper level).
8446
8447 // Add viewport for display 2 on hdmi2
8448 prepareSecondaryDisplay(type, hdmi2);
8449 // Send a touch event
8450 processPosition(mapper, 100, 100);
8451 processSync(mapper);
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8453
8454 // Add viewport for display 1 on hdmi1
8455 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8456 // Send a touch event again
8457 processPosition(mapper, 100, 100);
8458 processSync(mapper);
8459
8460 NotifyMotionArgs args;
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8462 ASSERT_EQ(DISPLAY_ID, args.displayId);
8463}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008465TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8466 addConfigurationProperty("touch.deviceType", "touchScreen");
8467 prepareAxes(POSITION);
8468 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8469
8470 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8471
8472 prepareDisplay(DISPLAY_ORIENTATION_0);
8473 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8474
8475 // Send a touch event
8476 processPosition(mapper, 100, 100);
8477 processSync(mapper);
8478
8479 NotifyMotionArgs args;
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8481 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8482}
8483
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008484TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008485 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008486 std::shared_ptr<FakePointerController> fakePointerController =
8487 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008488 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008489 fakePointerController->setPosition(100, 200);
8490 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008491 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008492
Garfield Tan888a6a42020-01-09 11:39:16 -08008493 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008494 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008495
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008496 prepareDisplay(DISPLAY_ORIENTATION_0);
8497 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008499
8500 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008501 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008502
8503 NotifyMotionArgs motionArgs;
8504 processPosition(mapper, 100, 100);
8505 processSync(mapper);
8506
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8509 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8510}
8511
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008512/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008513 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8514 */
8515TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8516 addConfigurationProperty("touch.deviceType", "touchScreen");
8517 prepareAxes(POSITION);
8518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8519
8520 prepareDisplay(DISPLAY_ORIENTATION_0);
8521 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8522 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8523 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8524 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8525
8526 NotifyMotionArgs args;
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8528 ASSERT_EQ(26, args.readTime);
8529
8530 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8531 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8532 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8535 ASSERT_EQ(33, args.readTime);
8536}
8537
8538/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008539 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8540 * events should not be delivered to the listener.
8541 */
8542TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8543 addConfigurationProperty("touch.deviceType", "touchScreen");
8544 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8545 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8546 ViewportType::INTERNAL);
8547 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8548 prepareAxes(POSITION);
8549 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8550
8551 NotifyMotionArgs motionArgs;
8552 processPosition(mapper, 100, 100);
8553 processSync(mapper);
8554
8555 mFakeListener->assertNotifyMotionWasNotCalled();
8556}
8557
Garfield Tanc734e4f2021-01-15 20:01:39 -08008558TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8559 addConfigurationProperty("touch.deviceType", "touchScreen");
8560 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8561 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8562 ViewportType::INTERNAL);
8563 std::optional<DisplayViewport> optionalDisplayViewport =
8564 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8565 ASSERT_TRUE(optionalDisplayViewport.has_value());
8566 DisplayViewport displayViewport = *optionalDisplayViewport;
8567
8568 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8569 prepareAxes(POSITION);
8570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8571
8572 // Finger down
8573 int32_t x = 100, y = 100;
8574 processPosition(mapper, x, y);
8575 processSync(mapper);
8576
8577 NotifyMotionArgs motionArgs;
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8580
8581 // Deactivate display viewport
8582 displayViewport.isActive = false;
8583 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8584 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8585
8586 // Finger move
8587 x += 10, y += 10;
8588 processPosition(mapper, x, y);
8589 processSync(mapper);
8590
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8593
8594 // Reactivate display viewport
8595 displayViewport.isActive = true;
8596 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8597 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8598
8599 // Finger move again
8600 x += 10, y += 10;
8601 processPosition(mapper, x, y);
8602 processSync(mapper);
8603
8604 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8605 // no pointer on the touch device.
8606 mFakeListener->assertNotifyMotionWasNotCalled();
8607}
8608
Arthur Hung7c645402019-01-25 17:45:42 +08008609TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8610 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008611 prepareAxes(POSITION | ID | SLOT);
8612 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008614
8615 // Create the second touch screen device, and enable multi fingers.
8616 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008617 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008618 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008619 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008620 std::shared_ptr<InputDevice> device2 =
8621 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008622 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008623
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008624 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8625 0 /*flat*/, 0 /*fuzz*/);
8626 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8627 0 /*flat*/, 0 /*fuzz*/);
8628 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8629 0 /*flat*/, 0 /*fuzz*/);
8630 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8631 0 /*flat*/, 0 /*fuzz*/);
8632 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8633 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8634 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008635
8636 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008637 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008638 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8639 device2->reset(ARBITRARY_TIME);
8640
8641 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008642 std::shared_ptr<FakePointerController> fakePointerController =
8643 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008644 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008645
8646 // Setup policy for associated displays and show touches.
8647 const uint8_t hdmi1 = 0;
8648 const uint8_t hdmi2 = 1;
8649 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8650 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8651 mFakePolicy->setShowTouches(true);
8652
8653 // Create displays.
8654 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008655 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008656
8657 // Default device will reconfigure above, need additional reconfiguration for another device.
8658 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008659 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8660 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008661
8662 // Two fingers down at default display.
8663 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8664 processPosition(mapper, x1, y1);
8665 processId(mapper, 1);
8666 processSlot(mapper, 1);
8667 processPosition(mapper, x2, y2);
8668 processId(mapper, 2);
8669 processSync(mapper);
8670
8671 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8672 fakePointerController->getSpots().find(DISPLAY_ID);
8673 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8674 ASSERT_EQ(size_t(2), iter->second.size());
8675
8676 // Two fingers down at second display.
8677 processPosition(mapper2, x1, y1);
8678 processId(mapper2, 1);
8679 processSlot(mapper2, 1);
8680 processPosition(mapper2, x2, y2);
8681 processId(mapper2, 2);
8682 processSync(mapper2);
8683
8684 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8685 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8686 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008687
8688 // Disable the show touches configuration and ensure the spots are cleared.
8689 mFakePolicy->setShowTouches(false);
8690 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8691 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8692
8693 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008694}
8695
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008696TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008697 prepareAxes(POSITION);
8698 addConfigurationProperty("touch.deviceType", "touchScreen");
8699 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008700 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008701
8702 NotifyMotionArgs motionArgs;
8703 // Unrotated video frame
8704 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8705 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008706 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008707 processPosition(mapper, 100, 200);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8710 ASSERT_EQ(frames, motionArgs.videoFrames);
8711
8712 // Subsequent touch events should not have any videoframes
8713 // This is implemented separately in FakeEventHub,
8714 // but that should match the behaviour of TouchVideoDevice.
8715 processPosition(mapper, 200, 200);
8716 processSync(mapper);
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8718 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8719}
8720
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008721TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008722 prepareAxes(POSITION);
8723 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008725 // Unrotated video frame
8726 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8727 NotifyMotionArgs motionArgs;
8728
8729 // Test all 4 orientations
8730 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008731 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8732 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8733 clearViewports();
8734 prepareDisplay(orientation);
8735 std::vector<TouchVideoFrame> frames{frame};
8736 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8737 processPosition(mapper, 100, 200);
8738 processSync(mapper);
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(frames, motionArgs.videoFrames);
8741 }
8742}
8743
8744TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8745 prepareAxes(POSITION);
8746 addConfigurationProperty("touch.deviceType", "touchScreen");
8747 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8748 // orientation-aware are affected by display rotation.
8749 addConfigurationProperty("touch.orientationAware", "0");
8750 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8751 // Unrotated video frame
8752 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8753 NotifyMotionArgs motionArgs;
8754
8755 // Test all 4 orientations
8756 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008757 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8758 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8759 clearViewports();
8760 prepareDisplay(orientation);
8761 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008762 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008763 processPosition(mapper, 100, 200);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008766 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8767 // compared to the display. This is so that when the window transform (which contains the
8768 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8769 // window's coordinate space.
8770 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008771 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008772
8773 // Release finger.
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008776 }
8777}
8778
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008779TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008780 prepareAxes(POSITION);
8781 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008782 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008783 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8784 // so mix these.
8785 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8786 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8787 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8788 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8789 NotifyMotionArgs motionArgs;
8790
8791 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008792 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008793 processPosition(mapper, 100, 200);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008796 ASSERT_EQ(frames, motionArgs.videoFrames);
8797}
8798
8799TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8800 prepareAxes(POSITION);
8801 addConfigurationProperty("touch.deviceType", "touchScreen");
8802 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8803 // orientation-aware are affected by display rotation.
8804 addConfigurationProperty("touch.orientationAware", "0");
8805 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8806 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8807 // so mix these.
8808 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8809 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8810 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8811 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8812 NotifyMotionArgs motionArgs;
8813
8814 prepareDisplay(DISPLAY_ORIENTATION_90);
8815 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8816 processPosition(mapper, 100, 200);
8817 processSync(mapper);
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8819 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8820 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8821 // compared to the display. This is so that when the window transform (which contains the
8822 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8823 // window's coordinate space.
8824 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8825 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008826 ASSERT_EQ(frames, motionArgs.videoFrames);
8827}
8828
Arthur Hung9da14732019-09-02 16:16:58 +08008829/**
8830 * If we had defined port associations, but the viewport is not ready, the touch device would be
8831 * expected to be disabled, and it should be enabled after the viewport has found.
8832 */
8833TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008834 constexpr uint8_t hdmi2 = 1;
8835 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008836 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008837
8838 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8839
8840 addConfigurationProperty("touch.deviceType", "touchScreen");
8841 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008842 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008843
8844 ASSERT_EQ(mDevice->isEnabled(), false);
8845
8846 // Add display on hdmi2, the device should be enabled and can receive touch event.
8847 prepareSecondaryDisplay(type, hdmi2);
8848 ASSERT_EQ(mDevice->isEnabled(), true);
8849
8850 // Send a touch event.
8851 processPosition(mapper, 100, 100);
8852 processSync(mapper);
8853
8854 NotifyMotionArgs args;
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8856 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8857}
8858
Arthur Hung421eb1c2020-01-16 00:09:42 +08008859TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008860 addConfigurationProperty("touch.deviceType", "touchScreen");
8861 prepareDisplay(DISPLAY_ORIENTATION_0);
8862 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008864
8865 NotifyMotionArgs motionArgs;
8866
8867 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8868 // finger down
8869 processId(mapper, 1);
8870 processPosition(mapper, x1, y1);
8871 processSync(mapper);
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8874 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8875
8876 // finger move
8877 processId(mapper, 1);
8878 processPosition(mapper, x2, y2);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8883
8884 // finger up.
8885 processId(mapper, -1);
8886 processSync(mapper);
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8888 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8889 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8890
8891 // new finger down
8892 processId(mapper, 1);
8893 processPosition(mapper, x3, y3);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8898}
8899
8900/**
arthurhungcc7f9802020-04-30 17:55:40 +08008901 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8902 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008903 */
arthurhungcc7f9802020-04-30 17:55:40 +08008904TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008905 addConfigurationProperty("touch.deviceType", "touchScreen");
8906 prepareDisplay(DISPLAY_ORIENTATION_0);
8907 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008909
8910 NotifyMotionArgs motionArgs;
8911
8912 // default tool type is finger
8913 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008914 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008915 processPosition(mapper, x1, y1);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920
8921 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8922 processToolType(mapper, MT_TOOL_PALM);
8923 processSync(mapper);
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8926
8927 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008928 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008929 processPosition(mapper, x2, y2);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8932
8933 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008934 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8937
8938 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008939 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008940 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008941 processPosition(mapper, x3, y3);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8946}
8947
arthurhungbf89a482020-04-17 17:37:55 +08008948/**
arthurhungcc7f9802020-04-30 17:55:40 +08008949 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8950 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008951 */
arthurhungcc7f9802020-04-30 17:55:40 +08008952TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008953 addConfigurationProperty("touch.deviceType", "touchScreen");
8954 prepareDisplay(DISPLAY_ORIENTATION_0);
8955 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8957
8958 NotifyMotionArgs motionArgs;
8959
8960 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008961 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8962 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008963 processPosition(mapper, x1, y1);
8964 processSync(mapper);
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8968
8969 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008970 processSlot(mapper, SECOND_SLOT);
8971 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008972 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008973 processSync(mapper);
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008975 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8977
8978 // If the tool type of the first finger changes to MT_TOOL_PALM,
8979 // we expect to receive ACTION_POINTER_UP with cancel flag.
8980 processSlot(mapper, FIRST_SLOT);
8981 processId(mapper, FIRST_TRACKING_ID);
8982 processToolType(mapper, MT_TOOL_PALM);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008985 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008986 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8987
8988 // The following MOVE events of second finger should be processed.
8989 processSlot(mapper, SECOND_SLOT);
8990 processId(mapper, SECOND_TRACKING_ID);
8991 processPosition(mapper, x2 + 1, y2 + 1);
8992 processSync(mapper);
8993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8995 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8996
8997 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8998 // it. Second finger receive move.
8999 processSlot(mapper, FIRST_SLOT);
9000 processId(mapper, INVALID_TRACKING_ID);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9004 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9005
9006 // Second finger keeps moving.
9007 processSlot(mapper, SECOND_SLOT);
9008 processId(mapper, SECOND_TRACKING_ID);
9009 processPosition(mapper, x2 + 2, y2 + 2);
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9013 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9014
9015 // Second finger up.
9016 processId(mapper, INVALID_TRACKING_ID);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9020 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9021}
9022
9023/**
9024 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9025 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9026 */
9027TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9028 addConfigurationProperty("touch.deviceType", "touchScreen");
9029 prepareDisplay(DISPLAY_ORIENTATION_0);
9030 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9032
9033 NotifyMotionArgs motionArgs;
9034
9035 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9036 // First finger down.
9037 processId(mapper, FIRST_TRACKING_ID);
9038 processPosition(mapper, x1, y1);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9043
9044 // Second finger down.
9045 processSlot(mapper, SECOND_SLOT);
9046 processId(mapper, SECOND_TRACKING_ID);
9047 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009050 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9052
arthurhungcc7f9802020-04-30 17:55:40 +08009053 // If the tool type of the first finger changes to MT_TOOL_PALM,
9054 // we expect to receive ACTION_POINTER_UP with cancel flag.
9055 processSlot(mapper, FIRST_SLOT);
9056 processId(mapper, FIRST_TRACKING_ID);
9057 processToolType(mapper, MT_TOOL_PALM);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009060 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009061 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9062
9063 // Second finger keeps moving.
9064 processSlot(mapper, SECOND_SLOT);
9065 processId(mapper, SECOND_TRACKING_ID);
9066 processPosition(mapper, x2 + 1, y2 + 1);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9070
9071 // second finger becomes palm, receive cancel due to only 1 finger is active.
9072 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009073 processToolType(mapper, MT_TOOL_PALM);
9074 processSync(mapper);
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9077
arthurhungcc7f9802020-04-30 17:55:40 +08009078 // third finger down.
9079 processSlot(mapper, THIRD_SLOT);
9080 processId(mapper, THIRD_TRACKING_ID);
9081 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009082 processPosition(mapper, x3, y3);
9083 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009087 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9088
9089 // third finger move
9090 processId(mapper, THIRD_TRACKING_ID);
9091 processPosition(mapper, x3 + 1, y3 + 1);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9095
9096 // first finger up, third finger receive move.
9097 processSlot(mapper, FIRST_SLOT);
9098 processId(mapper, INVALID_TRACKING_ID);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9102 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9103
9104 // second finger up, third finger receive move.
9105 processSlot(mapper, SECOND_SLOT);
9106 processId(mapper, INVALID_TRACKING_ID);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9110 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9111
9112 // third finger up.
9113 processSlot(mapper, THIRD_SLOT);
9114 processId(mapper, INVALID_TRACKING_ID);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9118 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9119}
9120
9121/**
9122 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9123 * and the active finger could still be allowed to receive the events
9124 */
9125TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9126 addConfigurationProperty("touch.deviceType", "touchScreen");
9127 prepareDisplay(DISPLAY_ORIENTATION_0);
9128 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9130
9131 NotifyMotionArgs motionArgs;
9132
9133 // default tool type is finger
9134 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9135 processId(mapper, FIRST_TRACKING_ID);
9136 processPosition(mapper, x1, y1);
9137 processSync(mapper);
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9141
9142 // Second finger down.
9143 processSlot(mapper, SECOND_SLOT);
9144 processId(mapper, SECOND_TRACKING_ID);
9145 processPosition(mapper, x2, y2);
9146 processSync(mapper);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009148 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9150
9151 // If the tool type of the second finger changes to MT_TOOL_PALM,
9152 // we expect to receive ACTION_POINTER_UP with cancel flag.
9153 processId(mapper, SECOND_TRACKING_ID);
9154 processToolType(mapper, MT_TOOL_PALM);
9155 processSync(mapper);
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009157 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009158 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9159
9160 // The following MOVE event should be processed.
9161 processSlot(mapper, FIRST_SLOT);
9162 processId(mapper, FIRST_TRACKING_ID);
9163 processPosition(mapper, x1 + 1, y1 + 1);
9164 processSync(mapper);
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9167 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9168
9169 // second finger up.
9170 processSlot(mapper, SECOND_SLOT);
9171 processId(mapper, INVALID_TRACKING_ID);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9175
9176 // first finger keep moving
9177 processSlot(mapper, FIRST_SLOT);
9178 processId(mapper, FIRST_TRACKING_ID);
9179 processPosition(mapper, x1 + 2, y1 + 2);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9183
9184 // first finger up.
9185 processId(mapper, INVALID_TRACKING_ID);
9186 processSync(mapper);
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9189 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009190}
9191
Arthur Hung9ad18942021-06-19 02:04:46 +00009192/**
9193 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9194 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9195 * cause slot be valid again.
9196 */
9197TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9198 addConfigurationProperty("touch.deviceType", "touchScreen");
9199 prepareDisplay(DISPLAY_ORIENTATION_0);
9200 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9201 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9202
9203 NotifyMotionArgs motionArgs;
9204
9205 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9206 // First finger down.
9207 processId(mapper, FIRST_TRACKING_ID);
9208 processPosition(mapper, x1, y1);
9209 processPressure(mapper, RAW_PRESSURE_MAX);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9213 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9214
9215 // First finger move.
9216 processId(mapper, FIRST_TRACKING_ID);
9217 processPosition(mapper, x1 + 1, y1 + 1);
9218 processPressure(mapper, RAW_PRESSURE_MAX);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9222 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9223
9224 // Second finger down.
9225 processSlot(mapper, SECOND_SLOT);
9226 processId(mapper, SECOND_TRACKING_ID);
9227 processPosition(mapper, x2, y2);
9228 processPressure(mapper, RAW_PRESSURE_MAX);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009231 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009232 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9233
9234 // second finger up with some unexpected data.
9235 processSlot(mapper, SECOND_SLOT);
9236 processId(mapper, INVALID_TRACKING_ID);
9237 processPosition(mapper, x2, y2);
9238 processSync(mapper);
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009240 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009241 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9242
9243 // first finger up with some unexpected data.
9244 processSlot(mapper, FIRST_SLOT);
9245 processId(mapper, INVALID_TRACKING_ID);
9246 processPosition(mapper, x2, y2);
9247 processPressure(mapper, RAW_PRESSURE_MAX);
9248 processSync(mapper);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9251 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9252}
9253
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009254// --- MultiTouchInputMapperTest_ExternalDevice ---
9255
9256class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9257protected:
Chris Yea52ade12020-08-27 16:49:20 -07009258 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009259};
9260
9261/**
9262 * Expect fallback to internal viewport if device is external and external viewport is not present.
9263 */
9264TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9265 prepareAxes(POSITION);
9266 addConfigurationProperty("touch.deviceType", "touchScreen");
9267 prepareDisplay(DISPLAY_ORIENTATION_0);
9268 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9269
9270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9271
9272 NotifyMotionArgs motionArgs;
9273
9274 // Expect the event to be sent to the internal viewport,
9275 // because an external viewport is not present.
9276 processPosition(mapper, 100, 100);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9279 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9280
9281 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009282 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009283 processPosition(mapper, 100, 100);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9287}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009288
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009289TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9290 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9291 std::shared_ptr<FakePointerController> fakePointerController =
9292 std::make_shared<FakePointerController>();
9293 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9294 fakePointerController->setPosition(0, 0);
9295 fakePointerController->setButtonState(0);
9296
9297 // prepare device and capture
9298 prepareDisplay(DISPLAY_ORIENTATION_0);
9299 prepareAxes(POSITION | ID | SLOT);
9300 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9301 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9302 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009303 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9305
9306 // captured touchpad should be a touchpad source
9307 NotifyDeviceResetArgs resetArgs;
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9309 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9310
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009311 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009312
9313 const InputDeviceInfo::MotionRange* relRangeX =
9314 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9315 ASSERT_NE(relRangeX, nullptr);
9316 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9317 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9318 const InputDeviceInfo::MotionRange* relRangeY =
9319 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9320 ASSERT_NE(relRangeY, nullptr);
9321 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9322 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9323
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009324 // run captured pointer tests - note that this is unscaled, so input listener events should be
9325 // identical to what the hardware sends (accounting for any
9326 // calibration).
9327 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009328 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009329 processId(mapper, 1);
9330 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9331 processKey(mapper, BTN_TOUCH, 1);
9332 processSync(mapper);
9333
9334 // expect coord[0] to contain initial location of touch 0
9335 NotifyMotionArgs args;
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9338 ASSERT_EQ(1U, args.pointerCount);
9339 ASSERT_EQ(0, args.pointerProperties[0].id);
9340 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9341 ASSERT_NO_FATAL_FAILURE(
9342 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9343
9344 // FINGER 1 DOWN
9345 processSlot(mapper, 1);
9346 processId(mapper, 2);
9347 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9348 processSync(mapper);
9349
9350 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009352 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009353 ASSERT_EQ(2U, args.pointerCount);
9354 ASSERT_EQ(0, args.pointerProperties[0].id);
9355 ASSERT_EQ(1, args.pointerProperties[1].id);
9356 ASSERT_NO_FATAL_FAILURE(
9357 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9358 ASSERT_NO_FATAL_FAILURE(
9359 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9360
9361 // FINGER 1 MOVE
9362 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9363 processSync(mapper);
9364
9365 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9366 // from move
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9369 ASSERT_NO_FATAL_FAILURE(
9370 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9371 ASSERT_NO_FATAL_FAILURE(
9372 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9373
9374 // FINGER 0 MOVE
9375 processSlot(mapper, 0);
9376 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9377 processSync(mapper);
9378
9379 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9382 ASSERT_NO_FATAL_FAILURE(
9383 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9384 ASSERT_NO_FATAL_FAILURE(
9385 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9386
9387 // BUTTON DOWN
9388 processKey(mapper, BTN_LEFT, 1);
9389 processSync(mapper);
9390
9391 // touchinputmapper design sends a move before button press
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9396
9397 // BUTTON UP
9398 processKey(mapper, BTN_LEFT, 0);
9399 processSync(mapper);
9400
9401 // touchinputmapper design sends a move after button release
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9406
9407 // FINGER 0 UP
9408 processId(mapper, -1);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9412
9413 // FINGER 1 MOVE
9414 processSlot(mapper, 1);
9415 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9416 processSync(mapper);
9417
9418 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9421 ASSERT_EQ(1U, args.pointerCount);
9422 ASSERT_EQ(1, args.pointerProperties[0].id);
9423 ASSERT_NO_FATAL_FAILURE(
9424 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9425
9426 // FINGER 1 UP
9427 processId(mapper, -1);
9428 processKey(mapper, BTN_TOUCH, 0);
9429 processSync(mapper);
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9432
9433 // non captured touchpad should be a mouse source
9434 mFakePolicy->setPointerCapture(false);
9435 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9437 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9438}
9439
9440TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9441 std::shared_ptr<FakePointerController> fakePointerController =
9442 std::make_shared<FakePointerController>();
9443 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9444 fakePointerController->setPosition(0, 0);
9445 fakePointerController->setButtonState(0);
9446
9447 // prepare device and capture
9448 prepareDisplay(DISPLAY_ORIENTATION_0);
9449 prepareAxes(POSITION | ID | SLOT);
9450 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9451 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009452 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009453 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9454 // run uncaptured pointer tests - pushes out generic events
9455 // FINGER 0 DOWN
9456 processId(mapper, 3);
9457 processPosition(mapper, 100, 100);
9458 processKey(mapper, BTN_TOUCH, 1);
9459 processSync(mapper);
9460
9461 // start at (100,100), cursor should be at (0,0) * scale
9462 NotifyMotionArgs args;
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9465 ASSERT_NO_FATAL_FAILURE(
9466 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9467
9468 // FINGER 0 MOVE
9469 processPosition(mapper, 200, 200);
9470 processSync(mapper);
9471
9472 // compute scaling to help with touch position checking
9473 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9474 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9475 float scale =
9476 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9477
9478 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9482 0, 0, 0, 0, 0, 0, 0));
9483}
9484
9485TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9486 std::shared_ptr<FakePointerController> fakePointerController =
9487 std::make_shared<FakePointerController>();
9488
9489 prepareDisplay(DISPLAY_ORIENTATION_0);
9490 prepareAxes(POSITION | ID | SLOT);
9491 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009492 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009493 mFakePolicy->setPointerCapture(false);
9494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9495
9496 // uncaptured touchpad should be a pointer device
9497 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9498
9499 // captured touchpad should be a touchpad device
9500 mFakePolicy->setPointerCapture(true);
9501 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9502 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9503}
9504
HQ Liue6983c72022-04-19 22:14:56 +00009505class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9506protected:
9507 float mPointerMovementScale;
9508 float mPointerXZoomScale;
9509 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9510 addConfigurationProperty("touch.deviceType", "pointer");
9511 std::shared_ptr<FakePointerController> fakePointerController =
9512 std::make_shared<FakePointerController>();
9513 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9514 fakePointerController->setPosition(0, 0);
9515 fakePointerController->setButtonState(0);
9516 prepareDisplay(DISPLAY_ORIENTATION_0);
9517
9518 prepareAxes(POSITION);
9519 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9520 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9521 // needs to be disabled, and the pointer gesture needs to be enabled.
9522 mFakePolicy->setPointerCapture(false);
9523 mFakePolicy->setPointerGestureEnabled(true);
9524 mFakePolicy->setPointerController(fakePointerController);
9525
9526 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9527 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9528 mPointerMovementScale =
9529 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9530 mPointerXZoomScale =
9531 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9532 }
9533
9534 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9535 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9536 /*flat*/ 0,
9537 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9538 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9539 /*flat*/ 0,
9540 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9541 }
9542};
9543
9544/**
9545 * Two fingers down on a pointer mode touch pad. The width
9546 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9547 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9548 * be greater than the both value to be freeform gesture, so that after two
9549 * fingers start to move downwards, the gesture should be swipe.
9550 */
9551TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9552 // The min freeform gesture width is 25units/mm x 30mm = 750
9553 // which is greater than fraction of the diagnal length of the touchpad (349).
9554 // Thus, MaxSwipWidth is 750.
9555 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9556 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9557 NotifyMotionArgs motionArgs;
9558
9559 // Two fingers down at once.
9560 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9561 // Pointer's initial position is used the [0,0] coordinate.
9562 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9563
9564 processId(mapper, FIRST_TRACKING_ID);
9565 processPosition(mapper, x1, y1);
9566 processMTSync(mapper);
9567 processId(mapper, SECOND_TRACKING_ID);
9568 processPosition(mapper, x2, y2);
9569 processMTSync(mapper);
9570 processSync(mapper);
9571
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9573 ASSERT_EQ(1U, motionArgs.pointerCount);
9574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9576 ASSERT_NO_FATAL_FAILURE(
9577 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9578
9579 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9580 // that there should be 1 pointer.
9581 int32_t movingDistance = 200;
9582 y1 += movingDistance;
9583 y2 += movingDistance;
9584
9585 processId(mapper, FIRST_TRACKING_ID);
9586 processPosition(mapper, x1, y1);
9587 processMTSync(mapper);
9588 processId(mapper, SECOND_TRACKING_ID);
9589 processPosition(mapper, x2, y2);
9590 processMTSync(mapper);
9591 processSync(mapper);
9592
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 ASSERT_EQ(1U, motionArgs.pointerCount);
9595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9598 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9599 0, 0, 0, 0));
9600}
9601
9602/**
9603 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9604 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9605 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9606 * value to be freeform gesture, so that after two fingers start to move downwards,
9607 * the gesture should be swipe.
9608 */
9609TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9610 // The min freeform gesture width is 5units/mm x 30mm = 150
9611 // which is greater than fraction of the diagnal length of the touchpad (349).
9612 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9613 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9614 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9615 NotifyMotionArgs motionArgs;
9616
9617 // Two fingers down at once.
9618 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9619 // Pointer's initial position is used the [0,0] coordinate.
9620 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9621
9622 processId(mapper, FIRST_TRACKING_ID);
9623 processPosition(mapper, x1, y1);
9624 processMTSync(mapper);
9625 processId(mapper, SECOND_TRACKING_ID);
9626 processPosition(mapper, x2, y2);
9627 processMTSync(mapper);
9628 processSync(mapper);
9629
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9631 ASSERT_EQ(1U, motionArgs.pointerCount);
9632 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9634 ASSERT_NO_FATAL_FAILURE(
9635 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9636
9637 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9638 // and there should be 1 pointer.
9639 int32_t movingDistance = 200;
9640 y1 += movingDistance;
9641 y2 += movingDistance;
9642
9643 processId(mapper, FIRST_TRACKING_ID);
9644 processPosition(mapper, x1, y1);
9645 processMTSync(mapper);
9646 processId(mapper, SECOND_TRACKING_ID);
9647 processPosition(mapper, x2, y2);
9648 processMTSync(mapper);
9649 processSync(mapper);
9650
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9652 ASSERT_EQ(1U, motionArgs.pointerCount);
9653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9654 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9655 // New coordinate is the scaled relative coordinate from the initial coordinate.
9656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9657 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9658 0, 0, 0, 0));
9659}
9660
9661/**
9662 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9663 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9664 * freeform gestures after two fingers start to move downwards.
9665 */
9666TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9667 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9668 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9669
9670 NotifyMotionArgs motionArgs;
9671
9672 // Two fingers down at once. Wider than the max swipe width.
9673 // The gesture is expected to be PRESS, then transformed to FREEFORM
9674 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9675
9676 processId(mapper, FIRST_TRACKING_ID);
9677 processPosition(mapper, x1, y1);
9678 processMTSync(mapper);
9679 processId(mapper, SECOND_TRACKING_ID);
9680 processPosition(mapper, x2, y2);
9681 processMTSync(mapper);
9682 processSync(mapper);
9683
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9685 ASSERT_EQ(1U, motionArgs.pointerCount);
9686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9688 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9689 ASSERT_NO_FATAL_FAILURE(
9690 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9691
9692 int32_t movingDistance = 200;
9693
9694 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9695 // then two down events for two pointers.
9696 y1 += movingDistance;
9697 y2 += movingDistance;
9698
9699 processId(mapper, FIRST_TRACKING_ID);
9700 processPosition(mapper, x1, y1);
9701 processMTSync(mapper);
9702 processId(mapper, SECOND_TRACKING_ID);
9703 processPosition(mapper, x2, y2);
9704 processMTSync(mapper);
9705 processSync(mapper);
9706
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9708 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9709 ASSERT_EQ(1U, motionArgs.pointerCount);
9710 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9713 ASSERT_EQ(1U, motionArgs.pointerCount);
9714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9717 ASSERT_EQ(2U, motionArgs.pointerCount);
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9720 // Two pointers' scaled relative coordinates from their initial centroid.
9721 // Initial y coordinates are 0 as y1 and y2 have the same value.
9722 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9723 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9724 // When pointers move, the new coordinates equal to the initial coordinates plus
9725 // scaled moving distance.
9726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9727 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9728 0, 0, 0, 0));
9729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9730 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9731 0, 0, 0, 0));
9732
9733 // Move two fingers down again, expect one MOVE motion event.
9734 y1 += movingDistance;
9735 y2 += movingDistance;
9736
9737 processId(mapper, FIRST_TRACKING_ID);
9738 processPosition(mapper, x1, y1);
9739 processMTSync(mapper);
9740 processId(mapper, SECOND_TRACKING_ID);
9741 processPosition(mapper, x2, y2);
9742 processMTSync(mapper);
9743 processSync(mapper);
9744
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9746 ASSERT_EQ(2U, motionArgs.pointerCount);
9747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9750 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9751 0, 0, 0, 0, 0));
9752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9753 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9754 0, 0, 0, 0, 0));
9755}
9756
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009757// --- JoystickInputMapperTest ---
9758
9759class JoystickInputMapperTest : public InputMapperTest {
9760protected:
9761 static const int32_t RAW_X_MIN;
9762 static const int32_t RAW_X_MAX;
9763 static const int32_t RAW_Y_MIN;
9764 static const int32_t RAW_Y_MAX;
9765
9766 void SetUp() override {
9767 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9768 }
9769 void prepareAxes() {
9770 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9772 }
9773
9774 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9776 }
9777
9778 void processSync(JoystickInputMapper& mapper) {
9779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9780 }
9781
9782 void prepareVirtualDisplay(int32_t orientation) {
9783 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9784 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9785 NO_PORT, ViewportType::VIRTUAL);
9786 }
9787};
9788
9789const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9790const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9791const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9792const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9793
9794TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9795 prepareAxes();
9796 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9797
9798 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9799
9800 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9801
9802 // Send an axis event
9803 processAxis(mapper, ABS_X, 100);
9804 processSync(mapper);
9805
9806 NotifyMotionArgs args;
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9808 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9809
9810 // Send another axis event
9811 processAxis(mapper, ABS_Y, 100);
9812 processSync(mapper);
9813
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9815 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9816}
9817
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009818// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009819
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009820class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009821protected:
9822 static const char* DEVICE_NAME;
9823 static const char* DEVICE_LOCATION;
9824 static const int32_t DEVICE_ID;
9825 static const int32_t DEVICE_GENERATION;
9826 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009827 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009828 static const int32_t EVENTHUB_ID;
9829
9830 std::shared_ptr<FakeEventHub> mFakeEventHub;
9831 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009832 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009833 std::unique_ptr<InstrumentedInputReader> mReader;
9834 std::shared_ptr<InputDevice> mDevice;
9835
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009836 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009837 mFakeEventHub = std::make_unique<FakeEventHub>();
9838 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009839 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009840 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009841 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009842 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9843 }
9844
9845 void SetUp() override { SetUp(DEVICE_CLASSES); }
9846
9847 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009848 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009849 mFakePolicy.clear();
9850 }
9851
9852 void configureDevice(uint32_t changes) {
9853 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9854 mReader->requestRefreshConfiguration(changes);
9855 mReader->loopOnce();
9856 }
9857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9858 }
9859
9860 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9861 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009862 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009863 InputDeviceIdentifier identifier;
9864 identifier.name = name;
9865 identifier.location = location;
9866 std::shared_ptr<InputDevice> device =
9867 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9868 identifier);
9869 mReader->pushNextDevice(device);
9870 mFakeEventHub->addDevice(eventHubId, name, classes);
9871 mReader->loopOnce();
9872 return device;
9873 }
9874
9875 template <class T, typename... Args>
9876 T& addControllerAndConfigure(Args... args) {
9877 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9878
9879 return controller;
9880 }
9881};
9882
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009883const char* PeripheralControllerTest::DEVICE_NAME = "device";
9884const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9885const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9886const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9887const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009888const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9889 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009890const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009891
9892// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009893class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009894protected:
9895 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009896 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009897 }
9898};
9899
9900TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009901 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009902
9903 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9904 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9905}
9906
9907TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009908 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009909
9910 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9911 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9912}
9913
9914// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009915class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009916protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009917 void SetUp() override {
9918 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9919 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920};
9921
Chris Ye85758332021-05-16 23:05:17 -07009922TEST_F(LightControllerTest, MonoLight) {
9923 RawLightInfo infoMono = {.id = 1,
9924 .name = "Mono",
9925 .maxBrightness = 255,
9926 .flags = InputLightClass::BRIGHTNESS,
9927 .path = ""};
9928 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009929
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009930 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009931 InputDeviceInfo info;
9932 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009933 std::vector<InputDeviceLightInfo> lights = info.getLights();
9934 ASSERT_EQ(1U, lights.size());
9935 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009936
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009937 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9938 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009939}
9940
9941TEST_F(LightControllerTest, RGBLight) {
9942 RawLightInfo infoRed = {.id = 1,
9943 .name = "red",
9944 .maxBrightness = 255,
9945 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9946 .path = ""};
9947 RawLightInfo infoGreen = {.id = 2,
9948 .name = "green",
9949 .maxBrightness = 255,
9950 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9951 .path = ""};
9952 RawLightInfo infoBlue = {.id = 3,
9953 .name = "blue",
9954 .maxBrightness = 255,
9955 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9956 .path = ""};
9957 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9958 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9959 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9960
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009961 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009962 InputDeviceInfo info;
9963 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009964 std::vector<InputDeviceLightInfo> lights = info.getLights();
9965 ASSERT_EQ(1U, lights.size());
9966 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009967
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009968 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9969 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009970}
9971
9972TEST_F(LightControllerTest, MultiColorRGBLight) {
9973 RawLightInfo infoColor = {.id = 1,
9974 .name = "red",
9975 .maxBrightness = 255,
9976 .flags = InputLightClass::BRIGHTNESS |
9977 InputLightClass::MULTI_INTENSITY |
9978 InputLightClass::MULTI_INDEX,
9979 .path = ""};
9980
9981 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9982
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009983 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009984 InputDeviceInfo info;
9985 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009986 std::vector<InputDeviceLightInfo> lights = info.getLights();
9987 ASSERT_EQ(1U, lights.size());
9988 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009989
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009990 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9991 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009992}
9993
9994TEST_F(LightControllerTest, PlayerIdLight) {
9995 RawLightInfo info1 = {.id = 1,
9996 .name = "player1",
9997 .maxBrightness = 255,
9998 .flags = InputLightClass::BRIGHTNESS,
9999 .path = ""};
10000 RawLightInfo info2 = {.id = 2,
10001 .name = "player2",
10002 .maxBrightness = 255,
10003 .flags = InputLightClass::BRIGHTNESS,
10004 .path = ""};
10005 RawLightInfo info3 = {.id = 3,
10006 .name = "player3",
10007 .maxBrightness = 255,
10008 .flags = InputLightClass::BRIGHTNESS,
10009 .path = ""};
10010 RawLightInfo info4 = {.id = 4,
10011 .name = "player4",
10012 .maxBrightness = 255,
10013 .flags = InputLightClass::BRIGHTNESS,
10014 .path = ""};
10015 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10016 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10017 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10018 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10019
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010020 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021 InputDeviceInfo info;
10022 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010023 std::vector<InputDeviceLightInfo> lights = info.getLights();
10024 ASSERT_EQ(1U, lights.size());
10025 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010026
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010027 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10028 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10029 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010030}
10031
Michael Wrightd02c5b62014-02-10 15:10:22 -080010032} // namespace android