blob: afba34417c8925ec07dc04883aacf0815e19dc4a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
HQ Liue6983c72022-04-19 22:14:56 +0000376 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
377
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800378 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
379
HQ Liue6983c72022-04-19 22:14:56 +0000380 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
381
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000382 void setVelocityControlParams(const VelocityControlParameters& params) {
383 mConfig.pointerVelocityControlParameters = params;
384 mConfig.wheelVelocityControlParameters = params;
385 }
386
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000388 uint32_t mNextPointerCaptureSequenceNumber = 0;
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 *outConfig = mConfig;
392 }
393
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000394 std::shared_ptr<PointerControllerInterface> obtainPointerController(
395 int32_t /*deviceId*/) override {
396 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398
Chris Yea52ade12020-08-27 16:49:20 -0700399 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 mInputDevicesChanged = true;
403 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
407 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700408 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 }
410
Chris Yea52ade12020-08-27 16:49:20 -0700411 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800412
413 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
414 std::unique_lock<std::mutex> lock(mLock);
415 base::ScopedLockAssertion assumeLocked(mLock);
416
417 const bool devicesChanged =
418 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
419 return mInputDevicesChanged;
420 });
421 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
422 mInputDevicesChanged = false;
423 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424};
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426// --- FakeEventHub ---
427
428class FakeEventHub : public EventHubInterface {
429 struct KeyInfo {
430 int32_t keyCode;
431 uint32_t flags;
432 };
433
Chris Yef59a2f42020-10-16 12:55:26 -0700434 struct SensorInfo {
435 InputDeviceSensorType sensorType;
436 int32_t sensorDataIndex;
437 };
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 struct Device {
440 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700441 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 PropertyMap configuration;
443 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
444 KeyedVector<int, bool> relativeAxes;
445 KeyedVector<int32_t, int32_t> keyCodeStates;
446 KeyedVector<int32_t, int32_t> scanCodeStates;
447 KeyedVector<int32_t, int32_t> switchStates;
448 KeyedVector<int32_t, int32_t> absoluteAxisValue;
449 KeyedVector<int32_t, KeyInfo> keysByScanCode;
450 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
451 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100452 // fake mapping which would normally come from keyCharacterMap
453 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700454 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
455 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800456 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700457 bool enabled;
458
459 status_t enable() {
460 enabled = true;
461 return OK;
462 }
463
464 status_t disable() {
465 enabled = false;
466 return OK;
467 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700469 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 };
471
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700472 std::mutex mLock;
473 std::condition_variable mEventsCondition;
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100476 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000477 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600478 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000479 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800480 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
481 // Simulates a device light brightness, from light id to light brightness.
482 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
483 // Simulates a device light intensities, from light id to light intensities map.
484 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
485 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700487public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 virtual ~FakeEventHub() {
489 for (size_t i = 0; i < mDevices.size(); i++) {
490 delete mDevices.valueAt(i);
491 }
492 }
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 FakeEventHub() { }
495
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700496 void addDevice(int32_t deviceId, const std::string& name,
497 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 Device* device = new Device(classes);
499 device->identifier.name = name;
500 mDevices.add(deviceId, device);
501
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000502 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
505 void removeDevice(int32_t deviceId) {
506 delete mDevices.valueFor(deviceId);
507 mDevices.removeItem(deviceId);
508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000509 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 }
511
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 bool isDeviceEnabled(int32_t deviceId) {
513 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700514 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700515 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
516 return false;
517 }
518 return device->enabled;
519 }
520
521 status_t enableDevice(int32_t deviceId) {
522 status_t result;
523 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700524 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700525 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
526 return BAD_VALUE;
527 }
528 if (device->enabled) {
529 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
530 return OK;
531 }
532 result = device->enable();
533 return result;
534 }
535
536 status_t disableDevice(int32_t deviceId) {
537 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700538 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
540 return BAD_VALUE;
541 }
542 if (!device->enabled) {
543 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
544 return OK;
545 }
546 return device->disable();
547 }
548
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000550 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 }
552
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700553 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 Device* device = getDevice(deviceId);
555 device->configuration.addProperty(key, value);
556 }
557
558 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
559 Device* device = getDevice(deviceId);
560 device->configuration.addAll(configuration);
561 }
562
563 void addAbsoluteAxis(int32_t deviceId, int axis,
564 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
565 Device* device = getDevice(deviceId);
566
567 RawAbsoluteAxisInfo info;
568 info.valid = true;
569 info.minValue = minValue;
570 info.maxValue = maxValue;
571 info.flat = flat;
572 info.fuzz = fuzz;
573 info.resolution = resolution;
574 device->absoluteAxes.add(axis, info);
575 }
576
577 void addRelativeAxis(int32_t deviceId, int32_t axis) {
578 Device* device = getDevice(deviceId);
579 device->relativeAxes.add(axis, true);
580 }
581
582 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
583 Device* device = getDevice(deviceId);
584 device->keyCodeStates.replaceValueFor(keyCode, state);
585 }
586
587 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
588 Device* device = getDevice(deviceId);
589 device->scanCodeStates.replaceValueFor(scanCode, state);
590 }
591
592 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
593 Device* device = getDevice(deviceId);
594 device->switchStates.replaceValueFor(switchCode, state);
595 }
596
597 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
598 Device* device = getDevice(deviceId);
599 device->absoluteAxisValue.replaceValueFor(axis, value);
600 }
601
602 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
603 int32_t keyCode, uint32_t flags) {
604 Device* device = getDevice(deviceId);
605 KeyInfo info;
606 info.keyCode = keyCode;
607 info.flags = flags;
608 if (scanCode) {
609 device->keysByScanCode.add(scanCode, info);
610 }
611 if (usageCode) {
612 device->keysByUsageCode.add(usageCode, info);
613 }
614 }
615
Philip Junker4af3b3d2021-12-14 10:36:55 +0100616 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
617 Device* device = getDevice(deviceId);
618 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
619 }
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 void addLed(int32_t deviceId, int32_t led, bool initialState) {
622 Device* device = getDevice(deviceId);
623 device->leds.add(led, initialState);
624 }
625
Chris Yef59a2f42020-10-16 12:55:26 -0700626 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
627 int32_t sensorDataIndex) {
628 Device* device = getDevice(deviceId);
629 SensorInfo info;
630 info.sensorType = sensorType;
631 info.sensorDataIndex = sensorDataIndex;
632 device->sensorsByAbsCode.emplace(absCode, info);
633 }
634
635 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
636 Device* device = getDevice(deviceId);
637 typename BitArray<MSC_MAX>::Buffer buffer;
638 buffer[mscEvent / 32] = 1 << mscEvent % 32;
639 device->mscBitmask.loadFromBuffer(buffer);
640 }
641
Chris Ye3fdbfef2021-01-06 18:45:18 -0800642 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
643 mRawLightInfos.emplace(rawId, std::move(info));
644 }
645
646 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
647 mLightBrightness.emplace(rawId, brightness);
648 }
649
650 void fakeLightIntensities(int32_t rawId,
651 const std::unordered_map<LightColor, int32_t> intensities) {
652 mLightIntensities.emplace(rawId, std::move(intensities));
653 }
654
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 bool getLedState(int32_t deviceId, int32_t led) {
656 Device* device = getDevice(deviceId);
657 return device->leds.valueFor(led);
658 }
659
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 return mExcludedDevices;
662 }
663
664 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
665 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800666 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000669 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
670 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700671 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 RawEvent event;
673 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000674 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 event.deviceId = deviceId;
676 event.type = type;
677 event.code = code;
678 event.value = value;
679 mEvents.push_back(event);
680
681 if (type == EV_ABS) {
682 setAbsoluteAxisValue(deviceId, code, value);
683 }
684 }
685
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600686 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
687 std::vector<TouchVideoFrame>> videoFrames) {
688 mVideoFrames = std::move(videoFrames);
689 }
690
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700692 std::unique_lock<std::mutex> lock(mLock);
693 base::ScopedLockAssertion assumeLocked(mLock);
694 const bool queueIsEmpty =
695 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
696 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
697 if (!queueIsEmpty) {
698 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
702private:
703 Device* getDevice(int32_t deviceId) const {
704 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100705 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 }
707
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700708 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700710 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Chris Yea52ade12020-08-27 16:49:20 -0700713 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 Device* device = getDevice(deviceId);
715 return device ? device->identifier : InputDeviceIdentifier();
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
Chris Yea52ade12020-08-27 16:49:20 -0700720 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 *outConfiguration = device->configuration;
724 }
725 }
726
Chris Yea52ade12020-08-27 16:49:20 -0700727 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
728 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800730 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 ssize_t index = device->absoluteAxes.indexOfKey(axis);
732 if (index >= 0) {
733 *outAxisInfo = device->absoluteAxes.valueAt(index);
734 return OK;
735 }
736 }
737 outAxisInfo->clear();
738 return -1;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
743 if (device) {
744 return device->relativeAxes.indexOfKey(axis) >= 0;
745 }
746 return false;
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yef59a2f42020-10-16 12:55:26 -0700751 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
752 Device* device = getDevice(deviceId);
753 if (device) {
754 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
755 }
756 return false;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
760 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 Device* device = getDevice(deviceId);
762 if (device) {
763 const KeyInfo* key = getKey(device, scanCode, usageCode);
764 if (key) {
765 if (outKeycode) {
766 *outKeycode = key->keyCode;
767 }
768 if (outFlags) {
769 *outFlags = key->flags;
770 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700771 if (outMetaState) {
772 *outMetaState = metaState;
773 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 return OK;
775 }
776 }
777 return NAME_NOT_FOUND;
778 }
779
780 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
781 if (usageCode) {
782 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
783 if (index >= 0) {
784 return &device->keysByUsageCode.valueAt(index);
785 }
786 }
787 if (scanCode) {
788 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
789 if (index >= 0) {
790 return &device->keysByScanCode.valueAt(index);
791 }
792 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700793 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
Chris Yef59a2f42020-10-16 12:55:26 -0700798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
799 int32_t absCode) {
800 Device* device = getDevice(deviceId);
801 if (!device) {
802 return Errorf("Sensor device not found.");
803 }
804 auto it = device->sensorsByAbsCode.find(absCode);
805 if (it == device->sensorsByAbsCode.end()) {
806 return Errorf("Sensor map not found.");
807 }
808 const SensorInfo& info = it->second;
809 return std::make_pair(info.sensorType, info.sensorDataIndex);
810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 mExcludedDevices = devices;
814 }
815
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000816 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
817 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000819 const size_t filledSize = std::min(mEvents.size(), bufferSize);
820 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
821
822 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700823 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000824 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600828 auto it = mVideoFrames.find(deviceId);
829 if (it != mVideoFrames.end()) {
830 std::vector<TouchVideoFrame> frames = std::move(it->second);
831 mVideoFrames.erase(deviceId);
832 return frames;
833 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800834 return {};
835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 Device* device = getDevice(deviceId);
839 if (device) {
840 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
841 if (index >= 0) {
842 return device->scanCodeStates.valueAt(index);
843 }
844 }
845 return AKEY_STATE_UNKNOWN;
846 }
847
Chris Yea52ade12020-08-27 16:49:20 -0700848 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 Device* device = getDevice(deviceId);
850 if (device) {
851 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
852 if (index >= 0) {
853 return device->keyCodeStates.valueAt(index);
854 }
855 }
856 return AKEY_STATE_UNKNOWN;
857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 Device* device = getDevice(deviceId);
861 if (device) {
862 ssize_t index = device->switchStates.indexOfKey(sw);
863 if (index >= 0) {
864 return device->switchStates.valueAt(index);
865 }
866 }
867 return AKEY_STATE_UNKNOWN;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
871 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 Device* device = getDevice(deviceId);
873 if (device) {
874 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
875 if (index >= 0) {
876 *outValue = device->absoluteAxisValue.valueAt(index);
877 return OK;
878 }
879 }
880 *outValue = 0;
881 return -1;
882 }
883
Philip Junker4af3b3d2021-12-14 10:36:55 +0100884 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
885 Device* device = getDevice(deviceId);
886 if (!device) {
887 return AKEYCODE_UNKNOWN;
888 }
889 auto it = device->keyCodeMapping.find(locationKeyCode);
890 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700894 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700895 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 bool result = false;
897 Device* device = getDevice(deviceId);
898 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700899 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700900 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
902 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
903 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905 }
906 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
907 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
908 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 }
910 }
911 }
912 }
913 return result;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 if (device) {
919 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
920 return index >= 0;
921 }
922 return false;
923 }
924
Arthur Hungcb40a002021-08-03 14:31:01 +0000925 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
926 Device* device = getDevice(deviceId);
927 if (!device) {
928 return false;
929 }
930 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
931 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
932 return true;
933 }
934 }
935 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
936 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
937 return true;
938 }
939 }
940 return false;
941 }
942
Chris Yea52ade12020-08-27 16:49:20 -0700943 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 Device* device = getDevice(deviceId);
945 return device && device->leds.indexOfKey(led) >= 0;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 Device* device = getDevice(deviceId);
950 if (device) {
951 ssize_t index = device->leds.indexOfKey(led);
952 if (index >= 0) {
953 device->leds.replaceValueAt(led, on);
954 } else {
955 ADD_FAILURE()
956 << "Attempted to set the state of an LED that the EventHub declared "
957 "was not present. led=" << led;
958 }
959 }
960 }
961
Chris Yea52ade12020-08-27 16:49:20 -0700962 void getVirtualKeyDefinitions(
963 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 outVirtualKeys.clear();
965
966 Device* device = getDevice(deviceId);
967 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800968 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 }
970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700973 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 return false;
978 }
979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Chris Ye87143712020-11-10 05:05:58 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
987 return BATTERY_CAPACITY;
988 }
Kim Low03ea0352020-11-06 12:45:07 -0800989
Chris Yee2b1e5c2021-03-10 22:45:12 -0800990 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
991 return BATTERY_STATUS;
992 }
993
994 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
995
996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
997 return std::nullopt;
998 }
Kim Low03ea0352020-11-06 12:45:07 -0800999
Chris Ye3fdbfef2021-01-06 18:45:18 -08001000 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1001 std::vector<int32_t> ids;
1002 for (const auto& [rawId, info] : mRawLightInfos) {
1003 ids.push_back(rawId);
1004 }
1005 return ids;
1006 }
1007
1008 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1009 auto it = mRawLightInfos.find(lightId);
1010 if (it == mRawLightInfos.end()) {
1011 return std::nullopt;
1012 }
1013 return it->second;
1014 }
1015
1016 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1017 mLightBrightness.emplace(lightId, brightness);
1018 }
1019
1020 void setLightIntensities(int32_t deviceId, int32_t lightId,
1021 std::unordered_map<LightColor, int32_t> intensities) override {
1022 mLightIntensities.emplace(lightId, intensities);
1023 };
1024
1025 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightBrightness.find(lightId);
1027 if (lightIt == mLightBrightness.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 }
1032
1033 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1034 int32_t deviceId, int32_t lightId) override {
1035 auto lightIt = mLightIntensities.find(lightId);
1036 if (lightIt == mLightIntensities.end()) {
1037 return std::nullopt;
1038 }
1039 return lightIt->second;
1040 };
1041
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001042 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 return false;
1044 }
1045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051
Chris Yea52ade12020-08-27 16:49:20 -07001052 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053};
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055// --- FakeInputMapper ---
1056
1057class FakeInputMapper : public InputMapper {
1058 uint32_t mSources;
1059 int32_t mKeyboardType;
1060 int32_t mMetaState;
1061 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1062 KeyedVector<int32_t, int32_t> mScanCodeStates;
1063 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001064 // fake mapping which would normally come from keyCharacterMap
1065 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001066 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::mutex mLock;
1069 std::condition_variable mStateChangedCondition;
1070 bool mConfigureWasCalled GUARDED_BY(mLock);
1071 bool mResetWasCalled GUARDED_BY(mLock);
1072 bool mProcessWasCalled GUARDED_BY(mLock);
1073 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Arthur Hungc23540e2018-11-29 20:42:11 +08001075 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001077 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1078 : InputMapper(deviceContext),
1079 mSources(sources),
1080 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001082 mConfigureWasCalled(false),
1083 mResetWasCalled(false),
1084 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Chris Yea52ade12020-08-27 16:49:20 -07001086 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087
1088 void setKeyboardType(int32_t keyboardType) {
1089 mKeyboardType = keyboardType;
1090 }
1091
1092 void setMetaState(int32_t metaState) {
1093 mMetaState = metaState;
1094 }
1095
1096 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::unique_lock<std::mutex> lock(mLock);
1098 base::ScopedLockAssertion assumeLocked(mLock);
1099 const bool configureCalled =
1100 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1101 return mConfigureWasCalled;
1102 });
1103 if (!configureCalled) {
1104 FAIL() << "Expected configure() to have been called.";
1105 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mConfigureWasCalled = false;
1107 }
1108
1109 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::unique_lock<std::mutex> lock(mLock);
1111 base::ScopedLockAssertion assumeLocked(mLock);
1112 const bool resetCalled =
1113 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1114 return mResetWasCalled;
1115 });
1116 if (!resetCalled) {
1117 FAIL() << "Expected reset() to have been called.";
1118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 mResetWasCalled = false;
1120 }
1121
Yi Kong9b14ac62018-07-17 13:48:38 -07001122 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001123 std::unique_lock<std::mutex> lock(mLock);
1124 base::ScopedLockAssertion assumeLocked(mLock);
1125 const bool processCalled =
1126 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1127 return mProcessWasCalled;
1128 });
1129 if (!processCalled) {
1130 FAIL() << "Expected process() to have been called.";
1131 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 if (outLastEvent) {
1133 *outLastEvent = mLastEvent;
1134 }
1135 mProcessWasCalled = false;
1136 }
1137
1138 void setKeyCodeState(int32_t keyCode, int32_t state) {
1139 mKeyCodeStates.replaceValueFor(keyCode, state);
1140 }
1141
1142 void setScanCodeState(int32_t scanCode, int32_t state) {
1143 mScanCodeStates.replaceValueFor(scanCode, state);
1144 }
1145
1146 void setSwitchState(int32_t switchCode, int32_t state) {
1147 mSwitchStates.replaceValueFor(switchCode, state);
1148 }
1149
1150 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001151 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 }
1153
Philip Junker4af3b3d2021-12-14 10:36:55 +01001154 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1155 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1156 }
1157
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001159 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Chris Yea52ade12020-08-27 16:49:20 -07001161 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 InputMapper::populateDeviceInfo(deviceInfo);
1163
1164 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1165 deviceInfo->setKeyboardType(mKeyboardType);
1166 }
1167 }
1168
Chris Yea52ade12020-08-27 16:49:20 -07001169 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001170 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001172
1173 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001174 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001175 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1176 mViewport = config->getDisplayViewportByPort(*displayPort);
1177 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001178
1179 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 }
1181
Chris Yea52ade12020-08-27 16:49:20 -07001182 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Chris Yea52ade12020-08-27 16:49:20 -07001188 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 mLastEvent = *rawEvent;
1191 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 }
1194
Chris Yea52ade12020-08-27 16:49:20 -07001195 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1197 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1198 }
1199
Philip Junker4af3b3d2021-12-14 10:36:55 +01001200 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1201 auto it = mKeyCodeMapping.find(locationKeyCode);
1202 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1207 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1208 }
1209
Chris Yea52ade12020-08-27 16:49:20 -07001210 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1212 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1213 }
1214
Chris Yea52ade12020-08-27 16:49:20 -07001215 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001216 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001217 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001218 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1220 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1221 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 }
1223 }
1224 }
Chris Yea52ade12020-08-27 16:49:20 -07001225 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 return result;
1227 }
1228
1229 virtual int32_t getMetaState() {
1230 return mMetaState;
1231 }
1232
1233 virtual void fadePointer() {
1234 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001235
1236 virtual std::optional<int32_t> getAssociatedDisplay() {
1237 if (mViewport) {
1238 return std::make_optional(mViewport->displayId);
1239 }
1240 return std::nullopt;
1241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242};
1243
1244
1245// --- InstrumentedInputReader ---
1246
1247class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001248 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001251 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1252 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001253 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001254 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001256 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001258 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001260 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001261 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 InputDeviceIdentifier identifier;
1263 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001264 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001266 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 }
1268
Prabir Pradhan28efc192019-11-05 01:10:04 +00001269 // Make the protected loopOnce method accessible to tests.
1270 using InputReader::loopOnce;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001273 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1274 const InputDeviceIdentifier& identifier)
1275 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 if (!mNextDevices.empty()) {
1277 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1278 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 return device;
1280 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001281 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282 }
1283
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 // --- FakeInputReaderContext ---
1285 class FakeInputReaderContext : public ContextImpl {
1286 int32_t mGlobalMetaState;
1287 bool mUpdateGlobalMetaStateWasCalled;
1288 int32_t mGeneration;
1289
1290 public:
1291 FakeInputReaderContext(InputReader* reader)
1292 : ContextImpl(reader),
1293 mGlobalMetaState(0),
1294 mUpdateGlobalMetaStateWasCalled(false),
1295 mGeneration(1) {}
1296
1297 virtual ~FakeInputReaderContext() {}
1298
1299 void assertUpdateGlobalMetaStateWasCalled() {
1300 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1301 << "Expected updateGlobalMetaState() to have been called.";
1302 mUpdateGlobalMetaStateWasCalled = false;
1303 }
1304
1305 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1306
1307 uint32_t getGeneration() { return mGeneration; }
1308
1309 void updateGlobalMetaState() override {
1310 mUpdateGlobalMetaStateWasCalled = true;
1311 ContextImpl::updateGlobalMetaState();
1312 }
1313
1314 int32_t getGlobalMetaState() override {
1315 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1316 }
1317
1318 int32_t bumpGeneration() override {
1319 mGeneration = ContextImpl::bumpGeneration();
1320 return mGeneration;
1321 }
1322 } mFakeContext;
1323
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001325
1326public:
1327 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328};
1329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330// --- InputReaderPolicyTest ---
1331class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001332protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 sp<FakeInputReaderPolicy> mFakePolicy;
1334
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001335 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001336 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337};
1338
1339/**
1340 * Check that empty set of viewports is an acceptable configuration.
1341 * Also try to get internal viewport two different ways - by type and by uniqueId.
1342 *
1343 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1344 * Such configuration is not currently allowed.
1345 */
1346TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001347 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348
1349 // We didn't add any viewports yet, so there shouldn't be any.
1350 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_FALSE(internalViewport);
1353
1354 // Add an internal viewport, then clear it
1355 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001357 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358
1359 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368
1369 mFakePolicy->clearViewports();
1370 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001371 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_FALSE(internalViewport);
1375}
1376
1377TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1378 const std::string internalUniqueId = "local:0";
1379 const std::string externalUniqueId = "local:1";
1380 const std::string virtualUniqueId1 = "virtual:2";
1381 const std::string virtualUniqueId2 = "virtual:3";
1382 constexpr int32_t virtualDisplayId1 = 2;
1383 constexpr int32_t virtualDisplayId2 = 3;
1384
1385 // Add an internal viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1388 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an external viewport
1390 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1392 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add an virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 // Add another virtual viewport
1398 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001399 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1400 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401
1402 // Check matching by type for internal
1403 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001404 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 ASSERT_TRUE(internalViewport);
1406 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1407
1408 // Check matching by type for external
1409 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001410 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_TRUE(externalViewport);
1412 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1413
1414 // Check matching by uniqueId for virtual viewport #1
1415 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001418 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1420 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1421
1422 // Check matching by uniqueId for virtual viewport #2
1423 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001424 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1428 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1429}
1430
1431
1432/**
1433 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1434 * that lookup works by checking display id.
1435 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1436 */
1437TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1438 const std::string uniqueId1 = "uniqueId1";
1439 const std::string uniqueId2 = "uniqueId2";
1440 constexpr int32_t displayId1 = 2;
1441 constexpr int32_t displayId2 = 3;
1442
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1444 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 for (const ViewportType& type : types) {
1446 mFakePolicy->clearViewports();
1447 // Add a viewport
1448 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 // Add another viewport
1452 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001453 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1454 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455
1456 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001457 std::optional<DisplayViewport> viewport1 =
1458 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001459 ASSERT_TRUE(viewport1);
1460 ASSERT_EQ(displayId1, viewport1->displayId);
1461 ASSERT_EQ(type, viewport1->type);
1462
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 std::optional<DisplayViewport> viewport2 =
1464 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_TRUE(viewport2);
1466 ASSERT_EQ(displayId2, viewport2->displayId);
1467 ASSERT_EQ(type, viewport2->type);
1468
1469 // When there are multiple viewports of the same kind, and uniqueId is not specified
1470 // in the call to getDisplayViewport, then that situation is not supported.
1471 // The viewports can be stored in any order, so we cannot rely on the order, since that
1472 // is just implementation detail.
1473 // However, we can check that it still returns *a* viewport, we just cannot assert
1474 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 ASSERT_TRUE(someViewport);
1477 }
1478}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001481 * When we have multiple internal displays make sure we always return the default display when
1482 * querying by type.
1483 */
1484TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1485 const std::string uniqueId1 = "uniqueId1";
1486 const std::string uniqueId2 = "uniqueId2";
1487 constexpr int32_t nonDefaultDisplayId = 2;
1488 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1489 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1490
1491 // Add the default display first and ensure it gets returned.
1492 mFakePolicy->clearViewports();
1493 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001497 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001498 ViewportType::INTERNAL);
1499
1500 std::optional<DisplayViewport> viewport =
1501 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1502 ASSERT_TRUE(viewport);
1503 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1504 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1505
1506 // Add the default display second to make sure order doesn't matter.
1507 mFakePolicy->clearViewports();
1508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514
1515 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1516 ASSERT_TRUE(viewport);
1517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1519}
1520
1521/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 * Check getDisplayViewportByPort
1523 */
1524TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526 const std::string uniqueId1 = "uniqueId1";
1527 const std::string uniqueId2 = "uniqueId2";
1528 constexpr int32_t displayId1 = 1;
1529 constexpr int32_t displayId2 = 2;
1530 const uint8_t hdmi1 = 0;
1531 const uint8_t hdmi2 = 1;
1532 const uint8_t hdmi3 = 2;
1533
1534 mFakePolicy->clearViewports();
1535 // Add a viewport that's associated with some display port that's not of interest.
1536 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 // Add another viewport, connected to HDMI1 port
1540 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001541 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1542 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001543
1544 // Check that correct display viewport was returned by comparing the display ports.
1545 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1546 ASSERT_TRUE(hdmi1Viewport);
1547 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1548 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1549
1550 // Check that we can still get the same viewport using the uniqueId
1551 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1552 ASSERT_TRUE(hdmi1Viewport);
1553 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1554 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1555 ASSERT_EQ(type, hdmi1Viewport->type);
1556
1557 // Check that we cannot find a port with "HDMI2", because we never added one
1558 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1559 ASSERT_FALSE(hdmi2Viewport);
1560}
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562// --- InputReaderTest ---
1563
1564class InputReaderTest : public testing::Test {
1565protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001566 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001569 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001572 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001573 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
Prabir Pradhan28efc192019-11-05 01:10:04 +00001576 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 }
1579
Chris Yea52ade12020-08-27 16:49:20 -07001580 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001581 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 }
1584
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001585 void addDevice(int32_t eventHubId, const std::string& name,
1586 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 }
1592 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593 mReader->loopOnce();
1594 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001595 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1596 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001599 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 }
1603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001606 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 }
1608
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001610 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001611 ftl::Flags<InputDeviceClass> classes,
1612 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1615 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001616 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 return mapper;
1619 }
1620};
1621
Chris Ye98d3f532020-10-01 21:48:59 -07001622TEST_F(InputReaderTest, PolicyGetInputDevices) {
1623 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001624 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001625 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626
1627 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001628 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001631 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001634 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635}
1636
Chris Yee7310032020-09-22 15:36:28 -07001637TEST_F(InputReaderTest, GetMergedInputDevices) {
1638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1639 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1640 // Add two subdevices to device
1641 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1642 // Must add at least one mapper or the device will be ignored!
1643 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1644 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1645
1646 // Push same device instance for next device to be added, so they'll have same identifier.
1647 mReader->pushNextDevice(device);
1648 mReader->pushNextDevice(device);
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1651 ASSERT_NO_FATAL_FAILURE(
1652 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1653
1654 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001655 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001656}
1657
Chris Yee14523a2020-12-19 13:46:00 -08001658TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1660 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1661 // Add two subdevices to device
1662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1663 // Must add at least one mapper or the device will be ignored!
1664 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1665 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1666
1667 // Push same device instance for next device to be added, so they'll have same identifier.
1668 mReader->pushNextDevice(device);
1669 mReader->pushNextDevice(device);
1670 // Sensor device is initially disabled
1671 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1672 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1673 nullptr));
1674 // Device is disabled because the only sub device is a sensor device and disabled initially.
1675 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1676 ASSERT_FALSE(device->isEnabled());
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1679 // The merged device is enabled if any sub device is enabled
1680 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1681 ASSERT_TRUE(device->isEnabled());
1682}
1683
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001686 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 constexpr int32_t eventHubId = 1;
1688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001691 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
Yi Kong9b14ac62018-07-17 13:48:38 -07001694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695
1696 NotifyDeviceResetArgs resetArgs;
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001698 ASSERT_EQ(deviceId, resetArgs.deviceId);
1699
1700 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001702 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 ASSERT_EQ(deviceId, resetArgs.deviceId);
1706 ASSERT_EQ(device->isEnabled(), false);
1707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001709 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 ASSERT_EQ(device->isEnabled(), false);
1713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 ASSERT_EQ(deviceId, resetArgs.deviceId);
1718 ASSERT_EQ(device->isEnabled(), true);
1719}
1720
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001723 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 AINPUT_SOURCE_KEYBOARD, nullptr);
1728 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1731 AINPUT_SOURCE_ANY, AKEYCODE_A))
1732 << "Should return unknown when the device id is >= 0 but unknown.";
1733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1735 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown when the device id is valid but the sources are not "
1737 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 ASSERT_EQ(AKEY_STATE_DOWN,
1740 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1741 AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is valid and the device "
1743 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1748
1749 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1750 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1751 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1752}
1753
Philip Junker4af3b3d2021-12-14 10:36:55 +01001754TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1755 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1756 constexpr int32_t eventHubId = 1;
1757 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1758 InputDeviceClass::KEYBOARD,
1759 AINPUT_SOURCE_KEYBOARD, nullptr);
1760 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1761
1762 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1763 << "Should return unknown when the device with the specified id is not found.";
1764
1765 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1766 << "Should return correct mapping when device id is valid and mapping exists.";
1767
1768 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1769 << "Should return the location key code when device id is valid and there's no "
1770 "mapping.";
1771}
1772
1773TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1775 constexpr int32_t eventHubId = 1;
1776 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1777 InputDeviceClass::JOYSTICK,
1778 AINPUT_SOURCE_GAMEPAD, nullptr);
1779 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1780
1781 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1783}
1784
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001787 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001790 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 AINPUT_SOURCE_KEYBOARD, nullptr);
1792 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
1794 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1795 AINPUT_SOURCE_ANY, KEY_A))
1796 << "Should return unknown when the device id is >= 0 but unknown.";
1797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1799 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1800 << "Should return unknown when the device id is valid but the sources are not "
1801 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_EQ(AKEY_STATE_DOWN,
1804 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1805 KEY_A))
1806 << "Should return value provided by mapper when device id is valid and the device "
1807 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808
1809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1812
1813 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1814 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1815 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1816}
1817
1818TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001820 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 AINPUT_SOURCE_KEYBOARD, nullptr);
1825 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1828 AINPUT_SOURCE_ANY, SW_LID))
1829 << "Should return unknown when the device id is >= 0 but unknown.";
1830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1832 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1833 << "Should return unknown when the device id is valid but the sources are not "
1834 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 ASSERT_EQ(AKEY_STATE_DOWN,
1837 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1838 SW_LID))
1839 << "Should return value provided by mapper when device id is valid and the device "
1840 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841
1842 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1845
1846 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1847 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1848 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1849}
1850
1851TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001859 mapper.addSupportedKeyCode(AKEYCODE_A);
1860 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 uint8_t flags[4] = { 0, 0, 0, 1 };
1864
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001865 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866 << "Should return false when device id is >= 0 but unknown.";
1867 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1868
1869 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001870 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001871 << "Should return false when device id is valid but the sources are not supported by "
1872 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1874
1875 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001876 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 keyCodes, flags))
1878 << "Should return value provided by mapper when device id is valid and the device "
1879 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1881
1882 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001883 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1884 << "Should return false when the device id is < 0 but the sources are not supported by "
1885 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1887
1888 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_TRUE(
1890 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1891 << "Should return value provided by mapper when device id is < 0 and one of the "
1892 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1894}
1895
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001896TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001898 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001899
1900 NotifyConfigurationChangedArgs args;
1901
1902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1903 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1904}
1905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001906TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001916 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001917 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1919
1920 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 ASSERT_EQ(when, event.when);
1923 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001925 ASSERT_EQ(EV_KEY, event.type);
1926 ASSERT_EQ(KEY_A, event.code);
1927 ASSERT_EQ(1, event.value);
1928}
1929
Garfield Tan1c7bc862020-01-28 13:24:04 -08001930TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 constexpr int32_t eventHubId = 1;
1934 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001937 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001939
1940 NotifyDeviceResetArgs resetArgs;
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001950 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001951 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001953 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001954 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961}
1962
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1964 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001965 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 constexpr int32_t eventHubId = 1;
1967 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1968 // Must add at least one mapper or the device will be ignored!
1969 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001970 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1972
1973 NotifyDeviceResetArgs resetArgs;
1974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1975 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1976}
1977
Arthur Hungc23540e2018-11-29 20:42:11 +08001978TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001981 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001982 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001983 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1984 FakeInputMapper& mapper =
1985 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001986 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001987
1988 const uint8_t hdmi1 = 1;
1989
1990 // Associated touch screen with second display.
1991 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1992
1993 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001994 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001999 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002000 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002002 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002003
2004 // Add the device, and make sure all of the callbacks are triggered.
2005 // The device is added after the input port associations are processed since
2006 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002010 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
Arthur Hung2c9a3342019-07-23 14:18:59 +08002012 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 ASSERT_EQ(deviceId, device->getId());
2014 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2015 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002016
2017 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002018 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002021}
2022
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002026 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2028 // Must add at least one mapper or the device will be ignored!
2029 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2030 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2031 mReader->pushNextDevice(device);
2032 mReader->pushNextDevice(device);
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2034 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2037
2038 NotifyDeviceResetArgs resetArgs;
2039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2040 ASSERT_EQ(deviceId, resetArgs.deviceId);
2041 ASSERT_TRUE(device->isEnabled());
2042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2043 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2044
2045 disableDevice(deviceId);
2046 mReader->loopOnce();
2047
2048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2049 ASSERT_EQ(deviceId, resetArgs.deviceId);
2050 ASSERT_FALSE(device->isEnabled());
2051 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2052 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2053
2054 enableDevice(deviceId);
2055 mReader->loopOnce();
2056
2057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2058 ASSERT_EQ(deviceId, resetArgs.deviceId);
2059 ASSERT_TRUE(device->isEnabled());
2060 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2061 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2062}
2063
2064TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2065 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002066 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002067 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2068 // Add two subdevices to device
2069 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2070 FakeInputMapper& mapperDevice1 =
2071 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2072 FakeInputMapper& mapperDevice2 =
2073 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2074 mReader->pushNextDevice(device);
2075 mReader->pushNextDevice(device);
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2077 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2078
2079 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2080 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2081
2082 ASSERT_EQ(AKEY_STATE_DOWN,
2083 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2084 ASSERT_EQ(AKEY_STATE_DOWN,
2085 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2086 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2087 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2088}
2089
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2091 NotifyPointerCaptureChangedArgs args;
2092
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002093 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2098 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099
2100 mFakePolicy->setPointerCapture(false);
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
2103 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002104 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 // does not change.
2108 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2109 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002111}
2112
Chris Ye87143712020-11-10 05:05:58 +00002113class FakeVibratorInputMapper : public FakeInputMapper {
2114public:
2115 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2116 : FakeInputMapper(deviceContext, sources) {}
2117
2118 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2119};
2120
2121TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2122 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002123 ftl::Flags<InputDeviceClass> deviceClass =
2124 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002125 constexpr int32_t eventHubId = 1;
2126 const char* DEVICE_LOCATION = "BLUETOOTH";
2127 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2128 FakeVibratorInputMapper& mapper =
2129 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2130 mReader->pushNextDevice(device);
2131
2132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2133 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2134
2135 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2136 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2137}
2138
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002139// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002140
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002141class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002142public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002143 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002145 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002146
2147 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2148
2149 void dump(std::string& dump) override {}
2150
2151 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2152 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002153 }
2154
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2156 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002157 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
2159 bool setLightColor(int32_t lightId, int32_t color) override {
2160 getDeviceContext().setLightBrightness(lightId, color >> 24);
2161 return true;
2162 }
2163
2164 std::optional<int32_t> getLightColor(int32_t lightId) override {
2165 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2166 if (!result.has_value()) {
2167 return std::nullopt;
2168 }
2169 return result.value() << 24;
2170 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
2172 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2173
2174 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2175
2176private:
2177 InputDeviceContext& mDeviceContext;
2178 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2179 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002180};
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182TEST_F(InputReaderTest, BatteryGetCapacity) {
2183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002184 ftl::Flags<InputDeviceClass> deviceClass =
2185 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186 constexpr int32_t eventHubId = 1;
2187 const char* DEVICE_LOCATION = "BLUETOOTH";
2188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController& controller =
2190 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 mReader->pushNextDevice(device);
2192
2193 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2194
2195 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2196 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2197}
2198
2199TEST_F(InputReaderTest, BatteryGetStatus) {
2200 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002201 ftl::Flags<InputDeviceClass> deviceClass =
2202 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 constexpr int32_t eventHubId = 1;
2204 const char* DEVICE_LOCATION = "BLUETOOTH";
2205 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002206 FakePeripheralController& controller =
2207 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002208 mReader->pushNextDevice(device);
2209
2210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2211
2212 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2213 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2214}
2215
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216TEST_F(InputReaderTest, LightGetColor) {
2217 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002218 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002224 mReader->pushNextDevice(device);
2225 RawLightInfo info = {.id = 1,
2226 .name = "Mono",
2227 .maxBrightness = 255,
2228 .flags = InputLightClass::BRIGHTNESS,
2229 .path = ""};
2230 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2231 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2232
2233 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2238 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2239}
2240
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241// --- InputReaderIntegrationTest ---
2242
2243// These tests create and interact with the InputReader only through its interface.
2244// The InputReader is started during SetUp(), which starts its processing in its own
2245// thread. The tests use linux uinput to emulate input devices.
2246// NOTE: Interacting with the physical device while these tests are running may cause
2247// the tests to fail.
2248class InputReaderIntegrationTest : public testing::Test {
2249protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002252 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002254 std::shared_ptr<FakePointerController> mFakePointerController;
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002257 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002258 mFakePointerController = std::make_shared<FakePointerController>();
2259 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2261 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002263 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2264 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 ASSERT_EQ(mReader->start(), OK);
2266
2267 // Since this test is run on a real device, all the input devices connected
2268 // to the test device will show up in mReader. We wait for those input devices to
2269 // show up before beginning the tests.
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2272 }
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mReader.reset();
2277 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278 mFakePolicy.clear();
2279 }
2280};
2281
2282TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2283 // An invalid input device that is only used for this test.
2284 class InvalidUinputDevice : public UinputDevice {
2285 public:
2286 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2287
2288 private:
2289 void configureDevice(int fd, uinput_user_dev* device) override {}
2290 };
2291
2292 const size_t numDevices = mFakePolicy->getInputDevices().size();
2293
2294 // UinputDevice does not set any event or key bits, so InputReader should not
2295 // consider it as a valid device.
2296 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2299 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2300
2301 invalidDevice.reset();
2302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2304 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2305}
2306
2307TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2308 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2309
2310 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2314
2315 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002317 const auto& it =
2318 std::find_if(inputDevices.begin(), inputDevices.end(),
2319 [&keyboard](const InputDeviceInfo& info) {
2320 return info.getIdentifier().name == keyboard->getName();
2321 });
2322
2323 ASSERT_NE(it, inputDevices.end());
2324 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2325 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2326 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002327
2328 keyboard.reset();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2332}
2333
2334TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2335 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2337
2338 NotifyConfigurationChangedArgs configChangedArgs;
2339 ASSERT_NO_FATAL_FAILURE(
2340 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002341 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002342 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2343
2344 NotifyKeyArgs keyArgs;
2345 keyboard->pressAndReleaseHomeKey();
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002348 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002349 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352 prevTimestamp = keyArgs.eventTime;
2353
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002356 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002358 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002361/**
2362 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2363 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2364 * are passed to the listener.
2365 */
2366static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2367TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2368 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2369 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2370 NotifyKeyArgs keyArgs;
2371
2372 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2375 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2376
2377 controller->pressAndReleaseKey(BTN_GEAR_UP);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2380 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2381}
2382
Arthur Hungaab25622020-01-16 11:22:11 +08002383// --- TouchProcessTest ---
2384class TouchIntegrationTest : public InputReaderIntegrationTest {
2385protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002386 const std::string UNIQUE_ID = "local:0";
2387
Chris Yea52ade12020-08-27 16:49:20 -07002388 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002389 InputReaderIntegrationTest::SetUp();
2390 // At least add an internal display.
2391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2392 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002393 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002394
2395 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2398 }
2399
2400 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2401 int32_t orientation, const std::string& uniqueId,
2402 std::optional<uint8_t> physicalPort,
2403 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002404 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2405 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002406 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2407 }
2408
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002409 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2410 NotifyMotionArgs args;
2411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2412 EXPECT_EQ(action, args.action);
2413 ASSERT_EQ(points.size(), args.pointerCount);
2414 for (size_t i = 0; i < args.pointerCount; i++) {
2415 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2416 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2417 }
2418 }
2419
Arthur Hungaab25622020-01-16 11:22:11 +08002420 std::unique_ptr<UinputTouchScreen> mDevice;
2421};
2422
2423TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2424 NotifyMotionArgs args;
2425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002428 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002429 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2433
2434 // ACTION_MOVE
2435 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002436 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2439
2440 // ACTION_UP
2441 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002442 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2445}
2446
2447TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2448 NotifyMotionArgs args;
2449 const Point centerPoint = mDevice->getCenterPoint();
2450
2451 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002452 mDevice->sendSlot(FIRST_SLOT);
2453 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002454 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2458
2459 // ACTION_POINTER_DOWN (Second slot)
2460 const Point secondPoint = centerPoint + Point(100, 100);
2461 mDevice->sendSlot(SECOND_SLOT);
2462 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002463 mDevice->sendDown(secondPoint);
2464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendMove(secondPoint + Point(1, 1));
2470 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2473
2474 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002475 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002478 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002479
2480 // ACTION_UP
2481 mDevice->sendSlot(FIRST_SLOT);
2482 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2486}
2487
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488/**
2489 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2490 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2491 * data?
2492 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2493 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2494 * for Pointer 0 only is generated after.
2495 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2496 * events, we will not miss any information.
2497 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2498 * event generated afterwards that contains the newest movement of pointer 0.
2499 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2500 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2501 * losing information about non-palm pointers.
2502 */
2503TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2504 NotifyMotionArgs args;
2505 const Point centerPoint = mDevice->getCenterPoint();
2506
2507 // ACTION_DOWN
2508 mDevice->sendSlot(FIRST_SLOT);
2509 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2510 mDevice->sendDown(centerPoint);
2511 mDevice->sendSync();
2512 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2513
2514 // ACTION_POINTER_DOWN (Second slot)
2515 const Point secondPoint = centerPoint + Point(100, 100);
2516 mDevice->sendSlot(SECOND_SLOT);
2517 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2518 mDevice->sendDown(secondPoint);
2519 mDevice->sendSync();
2520 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2521
2522 // ACTION_MOVE (First slot)
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendMove(centerPoint + Point(5, 5));
2525 // ACTION_POINTER_UP (Second slot)
2526 mDevice->sendSlot(SECOND_SLOT);
2527 mDevice->sendPointerUp();
2528 // Send a single sync for the above 2 pointer updates
2529 mDevice->sendSync();
2530
2531 // First, we should get POINTER_UP for the second pointer
2532 assertReceivedMotion(ACTION_POINTER_1_UP,
2533 {/*first pointer */ centerPoint + Point(5, 5),
2534 /*second pointer*/ secondPoint});
2535
2536 // Next, the MOVE event for the first pointer
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2538}
2539
2540/**
2541 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2542 * move, and then it will go up, all in the same frame.
2543 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2544 * gets sent to the listener.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendMove(secondPoint + Point(6, 6));
2571 mDevice->sendPointerUp();
2572 // Send a single sync for the above 2 pointer updates
2573 mDevice->sendSync();
2574
2575 // First, we should get POINTER_UP for the second pointer
2576 // The movement of the second pointer during the liftoff frame is ignored.
2577 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2578 assertReceivedMotion(ACTION_POINTER_1_UP,
2579 {/*first pointer */ centerPoint + Point(5, 5),
2580 /*second pointer*/ secondPoint});
2581
2582 // Next, the MOVE event for the first pointer
2583 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2584}
2585
Arthur Hungaab25622020-01-16 11:22:11 +08002586TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2587 NotifyMotionArgs args;
2588 const Point centerPoint = mDevice->getCenterPoint();
2589
2590 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002593 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2597
arthurhungcc7f9802020-04-30 17:55:40 +08002598 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002599 const Point secondPoint = centerPoint + Point(100, 100);
2600 mDevice->sendSlot(SECOND_SLOT);
2601 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2602 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002605 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002606
arthurhungcc7f9802020-04-30 17:55:40 +08002607 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002608 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2612
arthurhungcc7f9802020-04-30 17:55:40 +08002613 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2614 // a palm event.
2615 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002616 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002619 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002620 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002621
arthurhungcc7f9802020-04-30 17:55:40 +08002622 // Send up to second slot, expect first slot send moving.
2623 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002625 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002627
arthurhungcc7f9802020-04-30 17:55:40 +08002628 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002629 mDevice->sendSlot(FIRST_SLOT);
2630 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002632
arthurhungcc7f9802020-04-30 17:55:40 +08002633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2634 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002635}
2636
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638class InputDeviceTest : public testing::Test {
2639protected:
2640 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 static const int32_t DEVICE_ID;
2643 static const int32_t DEVICE_GENERATION;
2644 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002645 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002646 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002652 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Chris Yea52ade12020-08-27 16:49:20 -07002654 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002655 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002656 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 InputDeviceIdentifier identifier;
2661 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002662 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002666 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 }
2669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002671 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674};
2675
2676const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002677const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2680const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002682 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
2685TEST_F(InputDeviceTest, ImmutableProperties) {
2686 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002687 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689}
2690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2692 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002693}
2694
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2696 // Configuration.
2697 InputReaderConfiguration config;
2698 mDevice->configure(ARBITRARY_TIME, &config, 0);
2699
2700 // Reset.
2701 mDevice->reset(ARBITRARY_TIME);
2702
2703 NotifyDeviceResetArgs resetArgs;
2704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2705 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2706 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2707
2708 // Metadata.
2709 ASSERT_TRUE(mDevice->isIgnored());
2710 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2711
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002712 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002714 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2716 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2717
2718 // State queries.
2719 ASSERT_EQ(0, mDevice->getMetaState());
2720
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown key code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown scan code state.";
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2726 << "Ignored device should return unknown switch state.";
2727
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002728 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002730 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 << "Ignored device should never mark any key codes.";
2732 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2733 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2734}
2735
2736TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2737 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002738 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper1 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2743 mapper1.setMetaState(AMETA_ALT_ON);
2744 mapper1.addSupportedKeyCode(AKEYCODE_A);
2745 mapper1.addSupportedKeyCode(AKEYCODE_B);
2746 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2747 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2748 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2749 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2750 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002752 FakeInputMapper& mapper2 =
2753 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
2756 InputReaderConfiguration config;
2757 mDevice->configure(ARBITRARY_TIME, &config, 0);
2758
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 std::string propertyValue;
2760 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002762 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2765 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 // Reset
2768 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2770 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 NotifyDeviceResetArgs resetArgs;
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2774 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2775 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2776
2777 // Metadata.
2778 ASSERT_FALSE(mDevice->isIgnored());
2779 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2780
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002781 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002783 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2785 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2786
2787 // State queries.
2788 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2789 << "Should query mappers and combine meta states.";
2790
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown key code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown scan code state when source not supported.";
2795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2796 << "Should return unknown switch state when source not supported.";
2797
2798 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2801 << "Should query mapper when source is supported.";
2802 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2803 << "Should query mapper when source is supported.";
2804
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002805 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 << "Should do nothing when source is unsupported.";
2809 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2811 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2812 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2813
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002814 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2817 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2818 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2819 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2820
2821 // Event handling.
2822 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 mDevice->process(&event, 1);
2825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2827 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830// A single input device is associated with a specific display. Check that:
2831// 1. Device is disabled if the viewport corresponding to the associated display is not found
2832// 2. Device is disabled when setEnabled API is called
2833TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835
2836 // First Configuration.
2837 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2838
2839 // Device should be enabled by default.
2840 ASSERT_TRUE(mDevice->isEnabled());
2841
2842 // Prepare associated info.
2843 constexpr uint8_t hdmi = 1;
2844 const std::string UNIQUE_ID = "local:1";
2845
2846 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2847 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2848 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2849 // Device should be disabled because it is associated with a specific display via
2850 // input port <-> display port association, but the corresponding display is not found
2851 ASSERT_FALSE(mDevice->isEnabled());
2852
2853 // Prepare displays.
2854 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002855 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2856 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled after set disable.
2862 mFakePolicy->addDisabledDevice(mDevice->getId());
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2865 ASSERT_FALSE(mDevice->isEnabled());
2866
2867 // Device should still be disabled even found the associated display.
2868 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2869 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2870 ASSERT_FALSE(mDevice->isEnabled());
2871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2874 // Device should be enabled by default.
2875 mFakePolicy->clearViewports();
2876 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2878 ASSERT_TRUE(mDevice->isEnabled());
2879
2880 // Device should be disabled because it is associated with a specific display, but the
2881 // corresponding display is not found.
2882 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002883 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887
2888 // Device should be enabled when a display is found.
2889 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2890 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2891 NO_PORT, ViewportType::INTERNAL);
2892 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled after set disable.
2897 mFakePolicy->addDisabledDevice(mDevice->getId());
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2900 ASSERT_FALSE(mDevice->isEnabled());
2901
2902 // Device should still be disabled even found the associated display.
2903 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2905 ASSERT_FALSE(mDevice->isEnabled());
2906}
2907
Christine Franks2a2293c2022-01-18 11:51:16 -08002908TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2909 mFakePolicy->clearViewports();
2910 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2912
2913 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2915 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2916 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2917 NO_PORT, ViewportType::INTERNAL);
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2921}
2922
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923// --- InputMapperTest ---
2924
2925class InputMapperTest : public testing::Test {
2926protected:
2927 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 static const int32_t DEVICE_ID;
2930 static const int32_t DEVICE_GENERATION;
2931 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002932 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 std::unique_ptr<InstrumentedInputReader> mReader;
2939 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002941 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002942 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002943 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002946 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002948 // Consume the device reset notification generated when adding a new device.
2949 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950 }
2951
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002952 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002953 SetUp(DEVICE_CLASSES);
2954 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955
Chris Yea52ade12020-08-27 16:49:20 -07002956 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002957 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002959 }
2960
2961 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002962 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002963 }
2964
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002965 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002966 if (!changes ||
2967 (changes &
2968 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2969 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 mReader->requestRefreshConfiguration(changes);
2971 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002972 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002973 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002974 // Loop the reader to flush the input listener queue.
2975 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002976 }
2977
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2979 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002980 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002981 InputDeviceIdentifier identifier;
2982 identifier.name = name;
2983 identifier.location = location;
2984 std::shared_ptr<InputDevice> device =
2985 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2986 identifier);
2987 mReader->pushNextDevice(device);
2988 mFakeEventHub->addDevice(eventHubId, name, classes);
2989 mReader->loopOnce();
2990 return device;
2991 }
2992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 template <class T, typename... Args>
2994 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002996 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002998 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002999 // Loop the reader to flush the input listener queue.
3000 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002 }
3003
3004 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003005 int32_t orientation, const std::string& uniqueId,
3006 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003007 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3008 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003009 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3010 }
3011
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003012 void clearViewports() {
3013 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 }
3015
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003016 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3017 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 RawEvent event;
3019 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003020 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003021 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 event.type = type;
3023 event.code = code;
3024 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003025 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003026 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003027 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003028 }
3029
3030 static void assertMotionRange(const InputDeviceInfo& info,
3031 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3032 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003033 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3035 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3036 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3037 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3038 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3039 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3040 }
3041
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003042 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3043 float size, float touchMajor, float touchMinor, float toolMajor,
3044 float toolMinor, float orientation, float distance,
3045 float scaledAxisEpsilon = 1.f) {
3046 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3047 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003048 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3049 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003050 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3051 scaledAxisEpsilon);
3052 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3053 scaledAxisEpsilon);
3054 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3055 scaledAxisEpsilon);
3056 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3057 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3059 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3060 }
3061
Michael Wright17db18e2020-06-26 20:51:44 +01003062 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003064 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065 ASSERT_NEAR(x, actualX, 1);
3066 ASSERT_NEAR(y, actualY, 1);
3067 }
3068};
3069
3070const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003071const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3074const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003075const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3076 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003077const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
3079// --- SwitchInputMapperTest ---
3080
3081class SwitchInputMapperTest : public InputMapperTest {
3082protected:
3083};
3084
3085TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003086 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089}
3090
3091TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003094 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003097 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003098 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099}
3100
3101TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003108
3109 NotifySwitchArgs args;
3110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3111 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003112 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3113 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003114 args.switchMask);
3115 ASSERT_EQ(uint32_t(0), args.policyFlags);
3116}
3117
Chris Ye87143712020-11-10 05:05:58 +00003118// --- VibratorInputMapperTest ---
3119class VibratorInputMapperTest : public InputMapperTest {
3120protected:
3121 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3122};
3123
3124TEST_F(VibratorInputMapperTest, GetSources) {
3125 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3126
3127 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3128}
3129
3130TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3131 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3132
3133 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3134}
3135
3136TEST_F(VibratorInputMapperTest, Vibrate) {
3137 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003138 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003139 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3140
3141 VibrationElement pattern(2);
3142 VibrationSequence sequence(2);
3143 pattern.duration = std::chrono::milliseconds(200);
3144 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3145 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3146 sequence.addElement(pattern);
3147 pattern.duration = std::chrono::milliseconds(500);
3148 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3149 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3150 sequence.addElement(pattern);
3151
3152 std::vector<int64_t> timings = {0, 1};
3153 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3154
3155 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003156 // Start vibrating
3157 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003158 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003159 // Verify vibrator state listener was notified.
3160 mReader->loopOnce();
3161 NotifyVibratorStateArgs args;
3162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_TRUE(args.isOn);
3165 // Stop vibrating
3166 mapper.cancelVibrate(VIBRATION_TOKEN);
3167 ASSERT_FALSE(mapper.isVibrating());
3168 // Verify vibrator state listener was notified.
3169 mReader->loopOnce();
3170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3171 ASSERT_EQ(DEVICE_ID, args.deviceId);
3172 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003173}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
Chris Yef59a2f42020-10-16 12:55:26 -07003175// --- SensorInputMapperTest ---
3176
3177class SensorInputMapperTest : public InputMapperTest {
3178protected:
3179 static const int32_t ACCEL_RAW_MIN;
3180 static const int32_t ACCEL_RAW_MAX;
3181 static const int32_t ACCEL_RAW_FUZZ;
3182 static const int32_t ACCEL_RAW_FLAT;
3183 static const int32_t ACCEL_RAW_RESOLUTION;
3184
3185 static const int32_t GYRO_RAW_MIN;
3186 static const int32_t GYRO_RAW_MAX;
3187 static const int32_t GYRO_RAW_FUZZ;
3188 static const int32_t GYRO_RAW_FLAT;
3189 static const int32_t GYRO_RAW_RESOLUTION;
3190
3191 static const float GRAVITY_MS2_UNIT;
3192 static const float DEGREE_RADIAN_UNIT;
3193
3194 void prepareAccelAxes();
3195 void prepareGyroAxes();
3196 void setAccelProperties();
3197 void setGyroProperties();
3198 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3199};
3200
3201const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3202const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3203const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3204const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3205const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3206
3207const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3208const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3209const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3210const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3211const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3212
3213const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3214const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3215
3216void SensorInputMapperTest::prepareAccelAxes() {
3217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3218 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3220 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3222 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3223}
3224
3225void SensorInputMapperTest::prepareGyroAxes() {
3226 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3227 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3229 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3231 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3232}
3233
3234void SensorInputMapperTest::setAccelProperties() {
3235 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3236 /* sensorDataIndex */ 0);
3237 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3238 /* sensorDataIndex */ 1);
3239 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3240 /* sensorDataIndex */ 2);
3241 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3242 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3243 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3244 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3245 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3246}
3247
3248void SensorInputMapperTest::setGyroProperties() {
3249 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3250 /* sensorDataIndex */ 0);
3251 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3252 /* sensorDataIndex */ 1);
3253 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3254 /* sensorDataIndex */ 2);
3255 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3256 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3257 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3258 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3259 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3260}
3261
3262TEST_F(SensorInputMapperTest, GetSources) {
3263 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3264
3265 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3266}
3267
3268TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3269 setAccelProperties();
3270 prepareAccelAxes();
3271 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3272
3273 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3274 std::chrono::microseconds(10000),
3275 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003276 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003282
3283 NotifySensorArgs args;
3284 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3285 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3286 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3287
3288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3289 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3290 ASSERT_EQ(args.deviceId, DEVICE_ID);
3291 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3292 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3293 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3294 ASSERT_EQ(args.values, values);
3295 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3296}
3297
3298TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3299 setGyroProperties();
3300 prepareGyroAxes();
3301 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3302
3303 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3304 std::chrono::microseconds(10000),
3305 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003306 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003312
3313 NotifySensorArgs args;
3314 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3315 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3316 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3317
3318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3319 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3320 ASSERT_EQ(args.deviceId, DEVICE_ID);
3321 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3322 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3323 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3324 ASSERT_EQ(args.values, values);
3325 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3326}
3327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328// --- KeyboardInputMapperTest ---
3329
3330class KeyboardInputMapperTest : public InputMapperTest {
3331protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332 const std::string UNIQUE_ID = "local:0";
3333
3334 void prepareDisplay(int32_t orientation);
3335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003336 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 int32_t originalKeyCode, int32_t rotatedKeyCode,
3338 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339};
3340
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003341/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3342 * orientation.
3343 */
3344void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003345 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3346 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003347}
3348
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003349void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 int32_t originalScanCode, int32_t originalKeyCode,
3351 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 NotifyKeyArgs args;
3353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3357 ASSERT_EQ(originalScanCode, args.scanCode);
3358 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3363 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3364 ASSERT_EQ(originalScanCode, args.scanCode);
3365 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367}
3368
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003370 KeyboardInputMapper& mapper =
3371 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3372 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375}
3376
3377TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3378 const int32_t USAGE_A = 0x070004;
3379 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3381 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003382 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3383 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3384 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003386 KeyboardInputMapper& mapper =
3387 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3388 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003389 // Initial metastate is AMETA_NONE.
3390 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
3392 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 NotifyKeyArgs args;
3395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(DEVICE_ID, args.deviceId);
3411 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3412 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3413 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3414 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3415 ASSERT_EQ(KEY_HOME, args.scanCode);
3416 ASSERT_EQ(AMETA_NONE, args.metaState);
3417 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3418 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3419 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3420
3421 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3425 ASSERT_EQ(DEVICE_ID, args.deviceId);
3426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3427 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3429 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3430 ASSERT_EQ(0, args.scanCode);
3431 ASSERT_EQ(AMETA_NONE, args.metaState);
3432 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3434 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3435
3436 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3438 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3440 ASSERT_EQ(DEVICE_ID, args.deviceId);
3441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3442 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3443 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3444 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3445 ASSERT_EQ(0, args.scanCode);
3446 ASSERT_EQ(AMETA_NONE, args.metaState);
3447 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3448 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3450
3451 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3455 ASSERT_EQ(DEVICE_ID, args.deviceId);
3456 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3457 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3458 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3459 ASSERT_EQ(0, args.keyCode);
3460 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3461 ASSERT_EQ(AMETA_NONE, args.metaState);
3462 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3463 ASSERT_EQ(0U, args.policyFlags);
3464 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3465
3466 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3468 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(DEVICE_ID, args.deviceId);
3471 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3472 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3473 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3474 ASSERT_EQ(0, args.keyCode);
3475 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3476 ASSERT_EQ(AMETA_NONE, args.metaState);
3477 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3478 ASSERT_EQ(0U, args.policyFlags);
3479 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3480}
3481
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482/**
3483 * Ensure that the readTime is set to the time when the EV_KEY is received.
3484 */
3485TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3487
3488 KeyboardInputMapper& mapper =
3489 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3490 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3491 NotifyKeyArgs args;
3492
3493 // Key down
3494 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3496 ASSERT_EQ(12, args.readTime);
3497
3498 // Key up
3499 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3501 ASSERT_EQ(15, args.readTime);
3502}
3503
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003507 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3509 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
3512 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Arthur Hung95f68612022-04-07 14:08:22 +08003515 // Initial metastate is AMETA_NONE.
3516 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520 NotifyKeyArgs args;
3521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003524 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3541 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003543 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544}
3545
3546TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3548 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3549 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3550 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 KeyboardInputMapper& mapper =
3553 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3554 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003556 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3558 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3560 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3562 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3564 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3565}
3566
3567TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3571 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 KeyboardInputMapper& mapper =
3575 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3576 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003578 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579 ASSERT_NO_FATAL_FAILURE(
3580 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3582 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3584 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3586 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003588 clearViewports();
3589 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_UP, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
3600 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003601 ASSERT_NO_FATAL_FAILURE(
3602 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3604 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3606 AKEYCODE_DPAD_UP, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3608 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003610 clearViewports();
3611 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003612 ASSERT_NO_FATAL_FAILURE(
3613 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3615 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3617 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3619 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620
3621 // Special case: if orientation changes while key is down, we still emit the same keycode
3622 // in the key up as we did in the key down.
3623 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003624 clearViewports();
3625 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3628 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3629 ASSERT_EQ(KEY_UP, args.scanCode);
3630 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3631
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003632 clearViewports();
3633 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3636 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3637 ASSERT_EQ(KEY_UP, args.scanCode);
3638 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3639}
3640
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003641TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3642 // If the keyboard is not orientation aware,
3643 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 KeyboardInputMapper& mapper =
3647 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649 NotifyKeyArgs args;
3650
3651 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3657
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003658 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3663 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3664}
3665
3666TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3667 // If the keyboard is orientation aware,
3668 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 KeyboardInputMapper& mapper =
3673 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3674 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003675 NotifyKeyArgs args;
3676
3677 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3678 // ^--- already checked by the previous test
3679
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003680 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003681 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3686 ASSERT_EQ(DISPLAY_ID, args.displayId);
3687
3688 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003689 clearViewports();
3690 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003691 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(newDisplayId, args.displayId);
3697}
3698
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 KeyboardInputMapper& mapper =
3701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003707 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
Philip Junker4af3b3d2021-12-14 10:36:55 +01003711TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3715
3716 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3717 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3718 << "If a mapping is available, the result is equal to the mapping";
3719
3720 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3721 << "If no mapping is available, the result is the key location";
3722}
3723
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
3726 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734}
3735
3736TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 KeyboardInputMapper& mapper =
3738 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3739 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003744 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745 ASSERT_TRUE(flags[0]);
3746 ASSERT_FALSE(flags[1]);
3747}
3748
3749TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003750 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3751 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3752 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 KeyboardInputMapper& mapper =
3758 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3759 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003760 // Initial metastate is AMETA_NONE.
3761 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
3763 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
3784 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3788 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
3792 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003795 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3796 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3797 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003798 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799
3800 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003803 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3805 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807
3808 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3812 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3813 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003814 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815}
3816
Chris Yea52ade12020-08-27 16:49:20 -07003817TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3819 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3822
3823 KeyboardInputMapper& mapper =
3824 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3825 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3826
Chris Yea52ade12020-08-27 16:49:20 -07003827 // Meta state should be AMETA_NONE after reset
3828 mapper.reset(ARBITRARY_TIME);
3829 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3830 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3831 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3832 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3833
3834 NotifyKeyArgs args;
3835 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3841 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3842
3843 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3846 ASSERT_EQ(AMETA_NONE, args.metaState);
3847 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3848 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3849 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3850}
3851
Arthur Hung2c9a3342019-07-23 14:18:59 +08003852TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3853 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3856 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3857 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003858
3859 // keyboard 2.
3860 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003861 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003862 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003863 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003864 std::shared_ptr<InputDevice> device2 =
3865 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003866 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003867
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3869 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3870 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3871 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003872
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 KeyboardInputMapper& mapper =
3874 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3875 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003877 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003878 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003880 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3881 device2->reset(ARBITRARY_TIME);
3882
3883 // Prepared displays and associated info.
3884 constexpr uint8_t hdmi1 = 0;
3885 constexpr uint8_t hdmi2 = 1;
3886 const std::string SECONDARY_UNIQUE_ID = "local:1";
3887
3888 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3889 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3890
3891 // No associated display viewport found, should disable the device.
3892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3894 ASSERT_FALSE(device2->isEnabled());
3895
3896 // Prepare second display.
3897 constexpr int32_t newDisplayId = 2;
3898 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003899 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003900 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003901 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902 // Default device will reconfigure above, need additional reconfiguration for another device.
3903 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3905
3906 // Device should be enabled after the associated display is found.
3907 ASSERT_TRUE(mDevice->isEnabled());
3908 ASSERT_TRUE(device2->isEnabled());
3909
3910 // Test pad key events
3911 ASSERT_NO_FATAL_FAILURE(
3912 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3914 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3916 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3918 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3919
3920 ASSERT_NO_FATAL_FAILURE(
3921 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3922 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3923 AKEYCODE_DPAD_RIGHT, newDisplayId));
3924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3925 AKEYCODE_DPAD_DOWN, newDisplayId));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3927 AKEYCODE_DPAD_LEFT, newDisplayId));
3928}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
arthurhungc903df12020-08-11 15:08:42 +08003930TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3931 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3932 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3933 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3937
3938 KeyboardInputMapper& mapper =
3939 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3940 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003941 // Initial metastate is AMETA_NONE.
3942 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003943
3944 // Initialization should have turned all of the lights off.
3945 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3946 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3947 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3948
3949 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003952 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3953 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3954
3955 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003958 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3959 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3960
3961 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003964 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3965 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3966
3967 mFakeEventHub->removeDevice(EVENTHUB_ID);
3968 mReader->loopOnce();
3969
3970 // keyboard 2 should default toggle keys.
3971 const std::string USB2 = "USB2";
3972 const std::string DEVICE_NAME2 = "KEYBOARD2";
3973 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3974 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3975 std::shared_ptr<InputDevice> device2 =
3976 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003977 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003978 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3979 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3980 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3981 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3982 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3983 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3984
arthurhung6fe95782020-10-05 22:41:16 +08003985 KeyboardInputMapper& mapper2 =
3986 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3987 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003988 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3989 device2->reset(ARBITRARY_TIME);
3990
3991 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3992 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3993 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003994 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3995 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003996}
3997
Arthur Hungcb40a002021-08-03 14:31:01 +00003998TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3999 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4002
4003 // Suppose we have two mappers. (DPAD + KEYBOARD)
4004 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4005 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4006 KeyboardInputMapper& mapper =
4007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004009 // Initial metastate is AMETA_NONE.
4010 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004011
4012 mReader->toggleCapsLockState(DEVICE_ID);
4013 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4014}
4015
Arthur Hungfb3cc112022-04-13 07:39:50 +00004016TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4017 // keyboard 1.
4018 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4019 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4020 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4024
4025 KeyboardInputMapper& mapper1 =
4026 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4027 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4028
4029 // keyboard 2.
4030 const std::string USB2 = "USB2";
4031 const std::string DEVICE_NAME2 = "KEYBOARD2";
4032 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4033 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4034 std::shared_ptr<InputDevice> device2 =
4035 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4036 ftl::Flags<InputDeviceClass>(0));
4037 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4038 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4039 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4040 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4041 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4042 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4043
4044 KeyboardInputMapper& mapper2 =
4045 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4046 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4047 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4048 device2->reset(ARBITRARY_TIME);
4049
Arthur Hung95f68612022-04-07 14:08:22 +08004050 // Initial metastate is AMETA_NONE.
4051 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4053
4054 // Toggle num lock on and off.
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004057 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4058 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4059 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4060
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4063 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4064 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4066
4067 // Toggle caps lock on and off.
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4069 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4070 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4071 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4072 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4073
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4076 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4077 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4079
4080 // Toggle scroll lock on and off.
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4082 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4083 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4084 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4085 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4086
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4088 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4089 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4090 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4092}
4093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004094// --- KeyboardInputMapperTest_ExternalDevice ---
4095
4096class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4097protected:
Chris Yea52ade12020-08-27 16:49:20 -07004098 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004099};
4100
4101TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004102 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4103 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004104
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004105 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4106 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4107 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4108 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 KeyboardInputMapper& mapper =
4111 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4112 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004115 NotifyKeyArgs args;
4116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4117 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(uint32_t(0), args.policyFlags);
4122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(uint32_t(0), args.policyFlags);
4126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4129 ASSERT_EQ(uint32_t(0), args.policyFlags);
4130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4133 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4138}
4139
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004141 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004142
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4144 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 KeyboardInputMapper& mapper =
4149 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4150 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004153 NotifyKeyArgs args;
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(uint32_t(0), args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(uint32_t(0), args.policyFlags);
4168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4172
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4175 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4176}
4177
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178// --- CursorInputMapperTest ---
4179
4180class CursorInputMapperTest : public InputMapperTest {
4181protected:
4182 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4183
Michael Wright17db18e2020-06-26 20:51:44 +01004184 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185
Chris Yea52ade12020-08-27 16:49:20 -07004186 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 InputMapperTest::SetUp();
4188
Michael Wright17db18e2020-06-26 20:51:44 +01004189 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004190 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 }
4192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4194 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004195
4196 void prepareDisplay(int32_t orientation) {
4197 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004198 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004199 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4200 orientation, uniqueId, NO_PORT, viewportType);
4201 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202
4203 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4204 float pressure) {
4205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4206 0.0f, 0.0f, 0.0f, EPSILON));
4207 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208};
4209
4210const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4213 int32_t originalY, int32_t rotatedX,
4214 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215 NotifyMotionArgs args;
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(args.pointerCoords[0],
4224 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4225 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
4228TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
4235TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240}
4241
4242TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004244 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
4246 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004247 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
4249 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004250 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4251 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4253 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4254
4255 // When the bounds are set, then there should be a valid motion range.
4256 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4257
4258 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004259 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
4261 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4262 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4263 1, 800 - 1, 0.0f, 0.0f));
4264 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4265 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4266 2, 480 - 1, 0.0f, 0.0f));
4267 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4268 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4269 0.0f, 1.0f, 0.0f, 0.0f));
4270}
4271
4272TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004274 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004277 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278
4279 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4280 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4281 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4282 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4283 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4284 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4285 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4286 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4287 0.0f, 1.0f, 0.0f, 0.0f));
4288}
4289
4290TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
arthurhungdcef2dc2020-08-11 14:47:50 +08004294 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 NotifyMotionArgs args;
4297
4298 // Button press.
4299 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4303 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4304 ASSERT_EQ(DEVICE_ID, args.deviceId);
4305 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4306 ASSERT_EQ(uint32_t(0), args.policyFlags);
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4308 ASSERT_EQ(0, args.flags);
4309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4311 ASSERT_EQ(0, args.edgeFlags);
4312 ASSERT_EQ(uint32_t(1), args.pointerCount);
4313 ASSERT_EQ(0, args.pointerProperties[0].id);
4314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4317 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4318 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4319
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4321 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4322 ASSERT_EQ(DEVICE_ID, args.deviceId);
4323 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4324 ASSERT_EQ(uint32_t(0), args.policyFlags);
4325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4326 ASSERT_EQ(0, args.flags);
4327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4328 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4329 ASSERT_EQ(0, args.edgeFlags);
4330 ASSERT_EQ(uint32_t(1), args.pointerCount);
4331 ASSERT_EQ(0, args.pointerProperties[0].id);
4332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4336 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4337
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4340 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4342 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4343 ASSERT_EQ(DEVICE_ID, args.deviceId);
4344 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4345 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4347 ASSERT_EQ(0, args.flags);
4348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4349 ASSERT_EQ(0, args.buttonState);
4350 ASSERT_EQ(0, args.edgeFlags);
4351 ASSERT_EQ(uint32_t(1), args.pointerCount);
4352 ASSERT_EQ(0, args.pointerProperties[0].id);
4353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004354 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4356 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4357 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4358
4359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4360 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4361 ASSERT_EQ(DEVICE_ID, args.deviceId);
4362 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4363 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4365 ASSERT_EQ(0, args.flags);
4366 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4367 ASSERT_EQ(0, args.buttonState);
4368 ASSERT_EQ(0, args.edgeFlags);
4369 ASSERT_EQ(uint32_t(1), args.pointerCount);
4370 ASSERT_EQ(0, args.pointerProperties[0].id);
4371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4374 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4375 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4376}
4377
4378TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004380 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 NotifyMotionArgs args;
4383
4384 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4390 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4391 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392
4393 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004398 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4399 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400}
4401
4402TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004404 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004405
4406 NotifyMotionArgs args;
4407
4408 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004423 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004425
4426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004428 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429}
4430
4431TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004433 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
4435 NotifyMotionArgs args;
4436
4437 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4445 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4446 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004450 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4451 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4452 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004453
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4461 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4462 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
4464 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004473 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474}
4475
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004476TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004478 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4479 // need to be rotated.
4480 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004481 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004483 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4485 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4489 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4492}
4493
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004494TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004496 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4497 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004498 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004500 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4509
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004510 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004520 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4529
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004530 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539}
4540
4541TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
4545 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4546 mFakePointerController->setPosition(100, 200);
4547 mFakePointerController->setButtonState(0);
4548
4549 NotifyMotionArgs motionArgs;
4550 NotifyKeyArgs keyArgs;
4551
4552 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004559 ASSERT_NO_FATAL_FAILURE(
4560 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004561
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4564 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4565 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004566 ASSERT_NO_FATAL_FAILURE(
4567 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(0, motionArgs.buttonState);
4581 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004587 ASSERT_EQ(0, motionArgs.buttonState);
4588 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
4592 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4598 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4599 motionArgs.buttonState);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4601 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4609 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004610 ASSERT_NO_FATAL_FAILURE(
4611 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4615 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4616 motionArgs.buttonState);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4618 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004619 ASSERT_NO_FATAL_FAILURE(
4620 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004628 ASSERT_NO_FATAL_FAILURE(
4629 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630
4631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004633 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4634 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004635 ASSERT_NO_FATAL_FAILURE(
4636 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4642 ASSERT_EQ(0, motionArgs.buttonState);
4643 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004644 ASSERT_NO_FATAL_FAILURE(
4645 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, mFakePointerController->getButtonState());
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(0, motionArgs.buttonState);
4658 ASSERT_EQ(0, mFakePointerController->getButtonState());
4659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004660 ASSERT_NO_FATAL_FAILURE(
4661 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662
4663 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4667 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4668 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4678 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4679 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4680 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004688 ASSERT_EQ(0, motionArgs.buttonState);
4689 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(
4691 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695 ASSERT_EQ(0, motionArgs.buttonState);
4696 ASSERT_EQ(0, mFakePointerController->getButtonState());
4697
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004698 ASSERT_NO_FATAL_FAILURE(
4699 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4701 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4702 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4703
4704 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4708 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4709 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4720 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4721 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004722 ASSERT_NO_FATAL_FAILURE(
4723 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_EQ(0, motionArgs.buttonState);
4730 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004731 ASSERT_NO_FATAL_FAILURE(
4732 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733
4734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4736 ASSERT_EQ(0, motionArgs.buttonState);
4737 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004738 ASSERT_NO_FATAL_FAILURE(
4739 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4742 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4744
4745 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4750 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4761 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4762 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004763 ASSERT_NO_FATAL_FAILURE(
4764 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004769 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_EQ(0, motionArgs.buttonState);
4771 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(
4773 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774
4775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4776 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4777 ASSERT_EQ(0, motionArgs.buttonState);
4778 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004779 ASSERT_NO_FATAL_FAILURE(
4780 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4783 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4785
4786 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4790 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4791 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4802 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4803 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004804 ASSERT_NO_FATAL_FAILURE(
4805 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004810 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004813 ASSERT_NO_FATAL_FAILURE(
4814 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4818 ASSERT_EQ(0, motionArgs.buttonState);
4819 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004820 ASSERT_NO_FATAL_FAILURE(
4821 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004822
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4824 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4825 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4826}
4827
4828TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004830 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831
4832 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4833 mFakePointerController->setPosition(100, 200);
4834 mFakePointerController->setButtonState(0);
4835
4836 NotifyMotionArgs args;
4837
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004842 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4845 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004846 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004847}
4848
4849TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004850 addConfigurationProperty("cursor.mode", "pointer");
4851 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004853
4854 NotifyDeviceResetArgs resetArgs;
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4856 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4857 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4858
4859 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4860 mFakePointerController->setPosition(100, 200);
4861 mFakePointerController->setButtonState(0);
4862
4863 NotifyMotionArgs args;
4864
4865 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4870 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4873 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004874 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004875
4876 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4880 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4881 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4883 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4885 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4888 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4889
4890 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4892 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4894 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4897 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4899 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4902 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4903
4904 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4909 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4912 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004913 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004914
4915 // Disable pointer capture and check that the device generation got bumped
4916 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004917 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918 mFakePolicy->setPointerCapture(false);
4919 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004920 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004921
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004923 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4929 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4932 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004933 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934}
4935
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004936/**
4937 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4938 * pointer acceleration or speed processing should not be applied.
4939 */
4940TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4941 addConfigurationProperty("cursor.mode", "pointer");
4942 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4943 100.f /*high threshold*/, 10.f /*acceleration*/);
4944 mFakePolicy->setVelocityControlParams(testParams);
4945 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4946
4947 NotifyDeviceResetArgs resetArgs;
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4949 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4950 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4951
4952 NotifyMotionArgs args;
4953
4954 // Move and verify scale is applied.
4955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4959 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4961 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4962 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4963 ASSERT_GT(relX, 10);
4964 ASSERT_GT(relY, 20);
4965
4966 // Enable Pointer Capture
4967 mFakePolicy->setPointerCapture(true);
4968 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4969 NotifyPointerCaptureChangedArgs captureArgs;
4970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4971 ASSERT_TRUE(captureArgs.request.enable);
4972
4973 // Move and verify scale is not applied.
4974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4978 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4980 ASSERT_EQ(10, args.pointerCoords[0].getX());
4981 ASSERT_EQ(20, args.pointerCoords[0].getY());
4982}
4983
Prabir Pradhan208360b2022-06-24 18:37:04 +00004984TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4985 addConfigurationProperty("cursor.mode", "pointer");
4986 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4987
4988 NotifyDeviceResetArgs resetArgs;
4989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4990 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4991 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4992
4993 // Ensure the display is rotated.
4994 prepareDisplay(DISPLAY_ORIENTATION_90);
4995
4996 NotifyMotionArgs args;
4997
4998 // Verify that the coordinates are rotated.
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5005 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5006 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5007
5008 // Enable Pointer Capture.
5009 mFakePolicy->setPointerCapture(true);
5010 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5011 NotifyPointerCaptureChangedArgs captureArgs;
5012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5013 ASSERT_TRUE(captureArgs.request.enable);
5014
5015 // Move and verify rotation is not applied.
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5020 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5022 ASSERT_EQ(10, args.pointerCoords[0].getX());
5023 ASSERT_EQ(20, args.pointerCoords[0].getY());
5024}
5025
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005026TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005027 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005028
Garfield Tan888a6a42020-01-09 11:39:16 -08005029 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005030 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005031 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5032 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005033 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5034 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005035 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5036 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5037
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005038 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5039 mFakePointerController->setPosition(100, 200);
5040 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005041
5042 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5047 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5050 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005051 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005052 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5053}
5054
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055// --- TouchInputMapperTest ---
5056
5057class TouchInputMapperTest : public InputMapperTest {
5058protected:
5059 static const int32_t RAW_X_MIN;
5060 static const int32_t RAW_X_MAX;
5061 static const int32_t RAW_Y_MIN;
5062 static const int32_t RAW_Y_MAX;
5063 static const int32_t RAW_TOUCH_MIN;
5064 static const int32_t RAW_TOUCH_MAX;
5065 static const int32_t RAW_TOOL_MIN;
5066 static const int32_t RAW_TOOL_MAX;
5067 static const int32_t RAW_PRESSURE_MIN;
5068 static const int32_t RAW_PRESSURE_MAX;
5069 static const int32_t RAW_ORIENTATION_MIN;
5070 static const int32_t RAW_ORIENTATION_MAX;
5071 static const int32_t RAW_DISTANCE_MIN;
5072 static const int32_t RAW_DISTANCE_MAX;
5073 static const int32_t RAW_TILT_MIN;
5074 static const int32_t RAW_TILT_MAX;
5075 static const int32_t RAW_ID_MIN;
5076 static const int32_t RAW_ID_MAX;
5077 static const int32_t RAW_SLOT_MIN;
5078 static const int32_t RAW_SLOT_MAX;
5079 static const float X_PRECISION;
5080 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005081 static const float X_PRECISION_VIRTUAL;
5082 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083
5084 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005085 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005089 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005090 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005091
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 enum Axes {
5093 POSITION = 1 << 0,
5094 TOUCH = 1 << 1,
5095 TOOL = 1 << 2,
5096 PRESSURE = 1 << 3,
5097 ORIENTATION = 1 << 4,
5098 MINOR = 1 << 5,
5099 ID = 1 << 6,
5100 DISTANCE = 1 << 7,
5101 TILT = 1 << 8,
5102 SLOT = 1 << 9,
5103 TOOL_TYPE = 1 << 10,
5104 };
5105
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005106 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5107 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005108 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005110 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 int32_t toRawX(float displayX);
5112 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005113 int32_t toRotatedRawX(float displayX);
5114 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005115 float toCookedX(float rawX, float rawY);
5116 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005118 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005120 float toDisplayY(int32_t rawY, int32_t displayHeight);
5121
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122};
5123
5124const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5125const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5126const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5127const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5128const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5129const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5130const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005132const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5135const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5136const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5138const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5139const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5140const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5141const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5142const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5143const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5144const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5145const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005146const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5147 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5148const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5149 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005150const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5151 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152
5153const float TouchInputMapperTest::GEOMETRIC_SCALE =
5154 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5155 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5156
5157const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5158 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5159 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5160};
5161
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005162void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005163 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5164 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005165}
5166
5167void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5168 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5169 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170}
5171
Santos Cordonfa5cf462017-04-05 10:37:00 -07005172void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005173 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5174 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5175 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005176}
5177
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005179 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5180 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5181 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5182 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183}
5184
Jason Gerecke489fda82012-09-07 17:19:40 -07005185void TouchInputMapperTest::prepareLocationCalibration() {
5186 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5187}
5188
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189int32_t TouchInputMapperTest::toRawX(float displayX) {
5190 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5191}
5192
5193int32_t TouchInputMapperTest::toRawY(float displayY) {
5194 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5195}
5196
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005197int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5198 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5199}
5200
5201int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5202 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5203}
5204
Jason Gerecke489fda82012-09-07 17:19:40 -07005205float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5206 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5207 return rawX;
5208}
5209
5210float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5211 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5212 return rawY;
5213}
5214
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005216 return toDisplayX(rawX, DISPLAY_WIDTH);
5217}
5218
5219float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5220 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221}
5222
5223float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005224 return toDisplayY(rawY, DISPLAY_HEIGHT);
5225}
5226
5227float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5228 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
5231
5232// --- SingleTouchInputMapperTest ---
5233
5234class SingleTouchInputMapperTest : public TouchInputMapperTest {
5235protected:
5236 void prepareButtons();
5237 void prepareAxes(int axes);
5238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005239 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5240 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5241 void processUp(SingleTouchInputMapper& mappery);
5242 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5243 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5244 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5245 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5246 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5247 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248};
5249
5250void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005251 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252}
5253
5254void SingleTouchInputMapperTest::prepareAxes(int axes) {
5255 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 }
5259 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5261 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262 }
5263 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5265 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266 }
5267 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5269 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 }
5271 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274 }
5275}
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281}
5282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286}
5287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005288void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290}
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294}
5295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5297 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5306 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309}
5310
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005311void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5312 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314}
5315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005316void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318}
5319
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 prepareButtons();
5322 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005325 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326}
5327
5328TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005329 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5330 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 prepareButtons();
5332 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005335 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336}
5337
5338TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 prepareButtons();
5340 prepareAxes(POSITION);
5341 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005342 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345}
5346
5347TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348 prepareButtons();
5349 prepareAxes(POSITION);
5350 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005351 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005353 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354}
5355
5356TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 addConfigurationProperty("touch.deviceType", "touchScreen");
5358 prepareDisplay(DISPLAY_ORIENTATION_0);
5359 prepareButtons();
5360 prepareAxes(POSITION);
5361 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005362 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363
5364 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005365 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366
5367 // Virtual key is down.
5368 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5369 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5370 processDown(mapper, x, y);
5371 processSync(mapper);
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
5376 // Virtual key is up.
5377 processUp(mapper);
5378 processSync(mapper);
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5380
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382}
5383
5384TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 addConfigurationProperty("touch.deviceType", "touchScreen");
5386 prepareDisplay(DISPLAY_ORIENTATION_0);
5387 prepareButtons();
5388 prepareAxes(POSITION);
5389 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005390 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391
5392 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005393 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394
5395 // Virtual key is down.
5396 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5397 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5398 processDown(mapper, x, y);
5399 processSync(mapper);
5400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
5404 // Virtual key is up.
5405 processUp(mapper);
5406 processSync(mapper);
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005409 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410}
5411
5412TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 addConfigurationProperty("touch.deviceType", "touchScreen");
5414 prepareDisplay(DISPLAY_ORIENTATION_0);
5415 prepareButtons();
5416 prepareAxes(POSITION);
5417 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005421 ASSERT_TRUE(
5422 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_TRUE(flags[0]);
5424 ASSERT_FALSE(flags[1]);
5425}
5426
5427TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 addConfigurationProperty("touch.deviceType", "touchScreen");
5429 prepareDisplay(DISPLAY_ORIENTATION_0);
5430 prepareButtons();
5431 prepareAxes(POSITION);
5432 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005433 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434
arthurhungdcef2dc2020-08-11 14:47:50 +08005435 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436
5437 NotifyKeyArgs args;
5438
5439 // Press virtual key.
5440 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5441 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5442 processDown(mapper, x, y);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5446 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5447 ASSERT_EQ(DEVICE_ID, args.deviceId);
5448 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5449 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5451 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5452 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5453 ASSERT_EQ(KEY_HOME, args.scanCode);
5454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5455 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5456
5457 // Release virtual key.
5458 processUp(mapper);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5462 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5463 ASSERT_EQ(DEVICE_ID, args.deviceId);
5464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5465 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5468 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5469 ASSERT_EQ(KEY_HOME, args.scanCode);
5470 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5471 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5472
5473 // Should not have sent any motions.
5474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5475}
5476
5477TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 addConfigurationProperty("touch.deviceType", "touchScreen");
5479 prepareDisplay(DISPLAY_ORIENTATION_0);
5480 prepareButtons();
5481 prepareAxes(POSITION);
5482 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005483 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484
arthurhungdcef2dc2020-08-11 14:47:50 +08005485 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005486
5487 NotifyKeyArgs keyArgs;
5488
5489 // Press virtual key.
5490 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5491 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5492 processDown(mapper, x, y);
5493 processSync(mapper);
5494
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5496 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5497 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5498 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5499 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5500 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5501 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5502 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5503 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5505 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5506
5507 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5508 // into the display area.
5509 y -= 100;
5510 processMove(mapper, x, y);
5511 processSync(mapper);
5512
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5514 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5515 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5516 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5517 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5518 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5519 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5520 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5521 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5522 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5524 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5525
5526 NotifyMotionArgs motionArgs;
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5528 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5529 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5530 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5531 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5533 ASSERT_EQ(0, motionArgs.flags);
5534 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5535 ASSERT_EQ(0, motionArgs.buttonState);
5536 ASSERT_EQ(0, motionArgs.edgeFlags);
5537 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5538 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5541 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5542 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5543 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5544 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5545
5546 // Keep moving out of bounds. Should generate a pointer move.
5547 y -= 50;
5548 processMove(mapper, x, y);
5549 processSync(mapper);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5557 ASSERT_EQ(0, motionArgs.flags);
5558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5559 ASSERT_EQ(0, motionArgs.buttonState);
5560 ASSERT_EQ(0, motionArgs.edgeFlags);
5561 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5565 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5569
5570 // Release out of bounds. Should generate a pointer up.
5571 processUp(mapper);
5572 processSync(mapper);
5573
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5576 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5578 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5580 ASSERT_EQ(0, motionArgs.flags);
5581 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5582 ASSERT_EQ(0, motionArgs.buttonState);
5583 ASSERT_EQ(0, motionArgs.edgeFlags);
5584 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5585 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5588 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5589 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5590 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5592
5593 // Should not have sent any more keys or motions.
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5596}
5597
5598TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 addConfigurationProperty("touch.deviceType", "touchScreen");
5600 prepareDisplay(DISPLAY_ORIENTATION_0);
5601 prepareButtons();
5602 prepareAxes(POSITION);
5603 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
arthurhungdcef2dc2020-08-11 14:47:50 +08005606 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607
5608 NotifyMotionArgs motionArgs;
5609
5610 // Initially go down out of bounds.
5611 int32_t x = -10;
5612 int32_t y = -10;
5613 processDown(mapper, x, y);
5614 processSync(mapper);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5617
5618 // Move into the display area. Should generate a pointer down.
5619 x = 50;
5620 y = 75;
5621 processMove(mapper, x, y);
5622 processSync(mapper);
5623
5624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5630 ASSERT_EQ(0, motionArgs.flags);
5631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5632 ASSERT_EQ(0, motionArgs.buttonState);
5633 ASSERT_EQ(0, motionArgs.edgeFlags);
5634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5638 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5642
5643 // Release. Should generate a pointer up.
5644 processUp(mapper);
5645 processSync(mapper);
5646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5649 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5650 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5651 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5653 ASSERT_EQ(0, motionArgs.flags);
5654 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5655 ASSERT_EQ(0, motionArgs.buttonState);
5656 ASSERT_EQ(0, motionArgs.edgeFlags);
5657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5661 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5662 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5663 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5665
5666 // Should not have sent any more keys or motions.
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5669}
5670
Santos Cordonfa5cf462017-04-05 10:37:00 -07005671TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005672 addConfigurationProperty("touch.deviceType", "touchScreen");
5673 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5674
5675 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5676 prepareButtons();
5677 prepareAxes(POSITION);
5678 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005679 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005680
arthurhungdcef2dc2020-08-11 14:47:50 +08005681 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005682
5683 NotifyMotionArgs motionArgs;
5684
5685 // Down.
5686 int32_t x = 100;
5687 int32_t y = 125;
5688 processDown(mapper, x, y);
5689 processSync(mapper);
5690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5694 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5695 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5696 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5698 ASSERT_EQ(0, motionArgs.flags);
5699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5700 ASSERT_EQ(0, motionArgs.buttonState);
5701 ASSERT_EQ(0, motionArgs.edgeFlags);
5702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5706 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5707 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // Move.
5713 x += 50;
5714 y += 75;
5715 processMove(mapper, x, y);
5716 processSync(mapper);
5717
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5721 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5725 ASSERT_EQ(0, motionArgs.flags);
5726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5727 ASSERT_EQ(0, motionArgs.buttonState);
5728 ASSERT_EQ(0, motionArgs.edgeFlags);
5729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5733 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5734 1, 0, 0, 0, 0, 0, 0, 0));
5735 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5736 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5738
5739 // Up.
5740 processUp(mapper);
5741 processSync(mapper);
5742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5746 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5747 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5748 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5749 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5750 ASSERT_EQ(0, motionArgs.flags);
5751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5752 ASSERT_EQ(0, motionArgs.buttonState);
5753 ASSERT_EQ(0, motionArgs.edgeFlags);
5754 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5759 1, 0, 0, 0, 0, 0, 0, 0));
5760 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5761 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5763
5764 // Should not have sent any more keys or motions.
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5767}
5768
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 addConfigurationProperty("touch.deviceType", "touchScreen");
5771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 prepareButtons();
5773 prepareAxes(POSITION);
5774 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005775 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776
arthurhungdcef2dc2020-08-11 14:47:50 +08005777 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 NotifyMotionArgs motionArgs;
5780
5781 // Down.
5782 int32_t x = 100;
5783 int32_t y = 125;
5784 processDown(mapper, x, y);
5785 processSync(mapper);
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5789 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5790 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5791 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5792 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5793 ASSERT_EQ(0, motionArgs.flags);
5794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5795 ASSERT_EQ(0, motionArgs.buttonState);
5796 ASSERT_EQ(0, motionArgs.edgeFlags);
5797 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5801 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5802 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5803 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5804 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5805
5806 // Move.
5807 x += 50;
5808 y += 75;
5809 processMove(mapper, x, y);
5810 processSync(mapper);
5811
5812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5814 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5815 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5816 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5818 ASSERT_EQ(0, motionArgs.flags);
5819 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5820 ASSERT_EQ(0, motionArgs.buttonState);
5821 ASSERT_EQ(0, motionArgs.edgeFlags);
5822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5823 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5826 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5827 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5828 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5829 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5830
5831 // Up.
5832 processUp(mapper);
5833 processSync(mapper);
5834
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5841 ASSERT_EQ(0, motionArgs.flags);
5842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5843 ASSERT_EQ(0, motionArgs.buttonState);
5844 ASSERT_EQ(0, motionArgs.edgeFlags);
5845 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5849 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5850 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5851 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5853
5854 // Should not have sent any more keys or motions.
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5857}
5858
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005859TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 addConfigurationProperty("touch.deviceType", "touchScreen");
5861 prepareButtons();
5862 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005863 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5864 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
5867 NotifyMotionArgs args;
5868
5869 // Rotation 90.
5870 prepareDisplay(DISPLAY_ORIENTATION_90);
5871 processDown(mapper, toRawX(50), toRawY(75));
5872 processSync(mapper);
5873
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5875 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5876 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5877
5878 processUp(mapper);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5881}
5882
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005883TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 addConfigurationProperty("touch.deviceType", "touchScreen");
5885 prepareButtons();
5886 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005887 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5888 // orientation-aware are affected by display rotation.
5889 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005890 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891
5892 NotifyMotionArgs args;
5893
5894 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005895 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 prepareDisplay(DISPLAY_ORIENTATION_0);
5897 processDown(mapper, toRawX(50), toRawY(75));
5898 processSync(mapper);
5899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5901 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5902 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5903
5904 processUp(mapper);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5907
5908 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005909 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005911 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 processSync(mapper);
5913
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5915 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5916 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5917
5918 processUp(mapper);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5921
5922 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005923 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 prepareDisplay(DISPLAY_ORIENTATION_180);
5925 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5926 processSync(mapper);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5930 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5931
5932 processUp(mapper);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5935
5936 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005937 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005939 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 processSync(mapper);
5941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5943 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5944 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5945
5946 processUp(mapper);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5949}
5950
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005951TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5952 addConfigurationProperty("touch.deviceType", "touchScreen");
5953 prepareButtons();
5954 prepareAxes(POSITION);
5955 addConfigurationProperty("touch.orientationAware", "1");
5956 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5957 clearViewports();
5958 prepareDisplay(DISPLAY_ORIENTATION_0);
5959 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5960 NotifyMotionArgs args;
5961
5962 // Orientation 0.
5963 processDown(mapper, toRawX(50), toRawY(75));
5964 processSync(mapper);
5965
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5967 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5968 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5969
5970 processUp(mapper);
5971 processSync(mapper);
5972 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5973}
5974
5975TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareButtons();
5978 prepareAxes(POSITION);
5979 addConfigurationProperty("touch.orientationAware", "1");
5980 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5981 clearViewports();
5982 prepareDisplay(DISPLAY_ORIENTATION_0);
5983 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5984 NotifyMotionArgs args;
5985
5986 // Orientation 90.
5987 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5988 processSync(mapper);
5989
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5991 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5992 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5993
5994 processUp(mapper);
5995 processSync(mapper);
5996 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5997}
5998
5999TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6000 addConfigurationProperty("touch.deviceType", "touchScreen");
6001 prepareButtons();
6002 prepareAxes(POSITION);
6003 addConfigurationProperty("touch.orientationAware", "1");
6004 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6005 clearViewports();
6006 prepareDisplay(DISPLAY_ORIENTATION_0);
6007 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6008 NotifyMotionArgs args;
6009
6010 // Orientation 180.
6011 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6012 processSync(mapper);
6013
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6015 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6016 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6017
6018 processUp(mapper);
6019 processSync(mapper);
6020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6021}
6022
6023TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6024 addConfigurationProperty("touch.deviceType", "touchScreen");
6025 prepareButtons();
6026 prepareAxes(POSITION);
6027 addConfigurationProperty("touch.orientationAware", "1");
6028 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6029 clearViewports();
6030 prepareDisplay(DISPLAY_ORIENTATION_0);
6031 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6032 NotifyMotionArgs args;
6033
6034 // Orientation 270.
6035 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6036 processSync(mapper);
6037
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6039 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6040 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6041
6042 processUp(mapper);
6043 processSync(mapper);
6044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6045}
6046
6047TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6048 addConfigurationProperty("touch.deviceType", "touchScreen");
6049 prepareButtons();
6050 prepareAxes(POSITION);
6051 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6052 // orientation-aware are affected by display rotation.
6053 addConfigurationProperty("touch.orientationAware", "0");
6054 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6055 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6056
6057 NotifyMotionArgs args;
6058
6059 // Orientation 90, Rotation 0.
6060 clearViewports();
6061 prepareDisplay(DISPLAY_ORIENTATION_0);
6062 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6063 processSync(mapper);
6064
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6066 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6067 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6068
6069 processUp(mapper);
6070 processSync(mapper);
6071 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6072
6073 // Orientation 90, Rotation 90.
6074 clearViewports();
6075 prepareDisplay(DISPLAY_ORIENTATION_90);
6076 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6077 processSync(mapper);
6078
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6080 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6081 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6082
6083 processUp(mapper);
6084 processSync(mapper);
6085 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6086
6087 // Orientation 90, Rotation 180.
6088 clearViewports();
6089 prepareDisplay(DISPLAY_ORIENTATION_180);
6090 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6091 processSync(mapper);
6092
6093 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6094 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6095 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6096
6097 processUp(mapper);
6098 processSync(mapper);
6099 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6100
6101 // Orientation 90, Rotation 270.
6102 clearViewports();
6103 prepareDisplay(DISPLAY_ORIENTATION_270);
6104 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6105 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6106 processSync(mapper);
6107
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6109 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6110 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6111
6112 processUp(mapper);
6113 processSync(mapper);
6114 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6115}
6116
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118 addConfigurationProperty("touch.deviceType", "touchScreen");
6119 prepareDisplay(DISPLAY_ORIENTATION_0);
6120 prepareButtons();
6121 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006122 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 // These calculations are based on the input device calibration documentation.
6125 int32_t rawX = 100;
6126 int32_t rawY = 200;
6127 int32_t rawPressure = 10;
6128 int32_t rawToolMajor = 12;
6129 int32_t rawDistance = 2;
6130 int32_t rawTiltX = 30;
6131 int32_t rawTiltY = 110;
6132
6133 float x = toDisplayX(rawX);
6134 float y = toDisplayY(rawY);
6135 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6136 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6137 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6138 float distance = float(rawDistance);
6139
6140 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6141 float tiltScale = M_PI / 180;
6142 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6143 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6144 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6145 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6146
6147 processDown(mapper, rawX, rawY);
6148 processPressure(mapper, rawPressure);
6149 processToolMajor(mapper, rawToolMajor);
6150 processDistance(mapper, rawDistance);
6151 processTilt(mapper, rawTiltX, rawTiltY);
6152 processSync(mapper);
6153
6154 NotifyMotionArgs args;
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6157 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6158 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6159}
6160
Jason Gerecke489fda82012-09-07 17:19:40 -07006161TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006162 addConfigurationProperty("touch.deviceType", "touchScreen");
6163 prepareDisplay(DISPLAY_ORIENTATION_0);
6164 prepareLocationCalibration();
6165 prepareButtons();
6166 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006168
6169 int32_t rawX = 100;
6170 int32_t rawY = 200;
6171
6172 float x = toDisplayX(toCookedX(rawX, rawY));
6173 float y = toDisplayY(toCookedY(rawX, rawY));
6174
6175 processDown(mapper, rawX, rawY);
6176 processSync(mapper);
6177
6178 NotifyMotionArgs args;
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6181 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6182}
6183
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 addConfigurationProperty("touch.deviceType", "touchScreen");
6186 prepareDisplay(DISPLAY_ORIENTATION_0);
6187 prepareButtons();
6188 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006189 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190
6191 NotifyMotionArgs motionArgs;
6192 NotifyKeyArgs keyArgs;
6193
6194 processDown(mapper, 100, 200);
6195 processSync(mapper);
6196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6197 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6198 ASSERT_EQ(0, motionArgs.buttonState);
6199
6200 // press BTN_LEFT, release BTN_LEFT
6201 processKey(mapper, BTN_LEFT, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6206
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6210
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 processKey(mapper, BTN_LEFT, 0);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220
6221 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6222 processKey(mapper, BTN_RIGHT, 1);
6223 processKey(mapper, BTN_MIDDLE, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6228 motionArgs.buttonState);
6229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6232 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6236 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6237 motionArgs.buttonState);
6238
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 processKey(mapper, BTN_RIGHT, 0);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006244
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006247 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248
6249 processKey(mapper, BTN_MIDDLE, 0);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258
6259 // press BTN_BACK, release BTN_BACK
6260 processKey(mapper, BTN_BACK, 1);
6261 processSync(mapper);
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006265
Michael Wrightd02c5b62014-02-10 15:10:22 -08006266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006268 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6272 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 processKey(mapper, BTN_BACK, 0);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006282 ASSERT_EQ(0, motionArgs.buttonState);
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6286 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6287
6288 // press BTN_SIDE, release BTN_SIDE
6289 processKey(mapper, BTN_SIDE, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6293 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006294
Michael Wrightd02c5b62014-02-10 15:10:22 -08006295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006297 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302
6303 processKey(mapper, BTN_SIDE, 0);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006311 ASSERT_EQ(0, motionArgs.buttonState);
6312
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6314 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6315 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6316
6317 // press BTN_FORWARD, release BTN_FORWARD
6318 processKey(mapper, BTN_FORWARD, 1);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6321 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6322 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006326 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6330 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331
6332 processKey(mapper, BTN_FORWARD, 0);
6333 processSync(mapper);
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006337
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
6341
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6344 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6345
6346 // press BTN_EXTRA, release BTN_EXTRA
6347 processKey(mapper, BTN_EXTRA, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006352
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006355 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
6361 processKey(mapper, BTN_EXTRA, 0);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(0, motionArgs.buttonState);
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6372 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6373 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6374
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6376
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 // press BTN_STYLUS, release BTN_STYLUS
6378 processKey(mapper, BTN_STYLUS, 1);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006382 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6386 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387
6388 processKey(mapper, BTN_STYLUS, 0);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006396 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397
6398 // press BTN_STYLUS2, release BTN_STYLUS2
6399 processKey(mapper, BTN_STYLUS2, 1);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006403 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408
6409 processKey(mapper, BTN_STYLUS2, 0);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006417 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 // release touch
6420 processUp(mapper);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6424 ASSERT_EQ(0, motionArgs.buttonState);
6425}
6426
6427TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006428 addConfigurationProperty("touch.deviceType", "touchScreen");
6429 prepareDisplay(DISPLAY_ORIENTATION_0);
6430 prepareButtons();
6431 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006432 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433
6434 NotifyMotionArgs motionArgs;
6435
6436 // default tool type is finger
6437 processDown(mapper, 100, 200);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6442
6443 // eraser
6444 processKey(mapper, BTN_TOOL_RUBBER, 1);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6449
6450 // stylus
6451 processKey(mapper, BTN_TOOL_RUBBER, 0);
6452 processKey(mapper, BTN_TOOL_PEN, 1);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6457
6458 // brush
6459 processKey(mapper, BTN_TOOL_PEN, 0);
6460 processKey(mapper, BTN_TOOL_BRUSH, 1);
6461 processSync(mapper);
6462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6465
6466 // pencil
6467 processKey(mapper, BTN_TOOL_BRUSH, 0);
6468 processKey(mapper, BTN_TOOL_PENCIL, 1);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6473
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006474 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 processKey(mapper, BTN_TOOL_PENCIL, 0);
6476 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6477 processSync(mapper);
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6481
6482 // mouse
6483 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6484 processKey(mapper, BTN_TOOL_MOUSE, 1);
6485 processSync(mapper);
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6489
6490 // lens
6491 processKey(mapper, BTN_TOOL_MOUSE, 0);
6492 processKey(mapper, BTN_TOOL_LENS, 1);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6497
6498 // double-tap
6499 processKey(mapper, BTN_TOOL_LENS, 0);
6500 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6501 processSync(mapper);
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6505
6506 // triple-tap
6507 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6508 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6509 processSync(mapper);
6510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6513
6514 // quad-tap
6515 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6516 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6521
6522 // finger
6523 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6524 processKey(mapper, BTN_TOOL_FINGER, 1);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6529
6530 // stylus trumps finger
6531 processKey(mapper, BTN_TOOL_PEN, 1);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6536
6537 // eraser trumps stylus
6538 processKey(mapper, BTN_TOOL_RUBBER, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6543
6544 // mouse trumps eraser
6545 processKey(mapper, BTN_TOOL_MOUSE, 1);
6546 processSync(mapper);
6547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6550
6551 // back to default tool type
6552 processKey(mapper, BTN_TOOL_MOUSE, 0);
6553 processKey(mapper, BTN_TOOL_RUBBER, 0);
6554 processKey(mapper, BTN_TOOL_PEN, 0);
6555 processKey(mapper, BTN_TOOL_FINGER, 0);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6560}
6561
6562TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563 addConfigurationProperty("touch.deviceType", "touchScreen");
6564 prepareDisplay(DISPLAY_ORIENTATION_0);
6565 prepareButtons();
6566 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006567 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006568 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006569
6570 NotifyMotionArgs motionArgs;
6571
6572 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6573 processKey(mapper, BTN_TOOL_FINGER, 1);
6574 processMove(mapper, 100, 200);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6580
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6584 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6585
6586 // move a little
6587 processMove(mapper, 150, 250);
6588 processSync(mapper);
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6593
6594 // down when BTN_TOUCH is pressed, pressure defaults to 1
6595 processKey(mapper, BTN_TOUCH, 1);
6596 processSync(mapper);
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6606
6607 // up when BTN_TOUCH is released, hover restored
6608 processKey(mapper, BTN_TOUCH, 0);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6613 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6614
6615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6616 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6618 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6619
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6623 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6624
6625 // exit hover when pointer goes away
6626 processKey(mapper, BTN_TOOL_FINGER, 0);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6631 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6632}
6633
6634TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635 addConfigurationProperty("touch.deviceType", "touchScreen");
6636 prepareDisplay(DISPLAY_ORIENTATION_0);
6637 prepareButtons();
6638 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640
6641 NotifyMotionArgs motionArgs;
6642
6643 // initially hovering because pressure is 0
6644 processDown(mapper, 100, 200);
6645 processPressure(mapper, 0);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6651
6652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6655 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6656
6657 // move a little
6658 processMove(mapper, 150, 250);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6664
6665 // down when pressure is non-zero
6666 processPressure(mapper, RAW_PRESSURE_MAX);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6672
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6676 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 // up when pressure becomes 0, hover restored
6679 processPressure(mapper, 0);
6680 processSync(mapper);
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6685
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6689 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6694 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6695
6696 // exit hover when pointer goes away
6697 processUp(mapper);
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6702 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6703}
6704
lilinnan687e58f2022-07-19 16:00:50 +08006705TEST_F(SingleTouchInputMapperTest,
6706 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6707 addConfigurationProperty("touch.deviceType", "touchScreen");
6708 prepareDisplay(DISPLAY_ORIENTATION_0);
6709 prepareButtons();
6710 prepareAxes(POSITION);
6711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6712 NotifyMotionArgs motionArgs;
6713
6714 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006715 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006716 processSync(mapper);
6717
6718 // We should receive a down event
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6721
6722 // Change display id
6723 clearViewports();
6724 prepareSecondaryDisplay(ViewportType::INTERNAL);
6725
6726 // We should receive a cancel event
6727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6728 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6729 // Then receive reset called
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6731}
6732
Prabir Pradhan5632d622021-09-06 07:57:20 -07006733// --- TouchDisplayProjectionTest ---
6734
6735class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6736public:
6737 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6738 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6739 // rotated equivalent of the given un-rotated physical display bounds.
6740 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6741 uint32_t inverseRotationFlags;
6742 auto width = DISPLAY_WIDTH;
6743 auto height = DISPLAY_HEIGHT;
6744 switch (orientation) {
6745 case DISPLAY_ORIENTATION_90:
6746 inverseRotationFlags = ui::Transform::ROT_270;
6747 std::swap(width, height);
6748 break;
6749 case DISPLAY_ORIENTATION_180:
6750 inverseRotationFlags = ui::Transform::ROT_180;
6751 break;
6752 case DISPLAY_ORIENTATION_270:
6753 inverseRotationFlags = ui::Transform::ROT_90;
6754 std::swap(width, height);
6755 break;
6756 case DISPLAY_ORIENTATION_0:
6757 inverseRotationFlags = ui::Transform::ROT_0;
6758 break;
6759 default:
6760 FAIL() << "Invalid orientation: " << orientation;
6761 }
6762
6763 const ui::Transform rotation(inverseRotationFlags, width, height);
6764 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6765
6766 std::optional<DisplayViewport> internalViewport =
6767 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6768 DisplayViewport& v = *internalViewport;
6769 v.displayId = DISPLAY_ID;
6770 v.orientation = orientation;
6771
6772 v.logicalLeft = 0;
6773 v.logicalTop = 0;
6774 v.logicalRight = 100;
6775 v.logicalBottom = 100;
6776
6777 v.physicalLeft = rotatedPhysicalDisplay.left;
6778 v.physicalTop = rotatedPhysicalDisplay.top;
6779 v.physicalRight = rotatedPhysicalDisplay.right;
6780 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6781
6782 v.deviceWidth = width;
6783 v.deviceHeight = height;
6784
6785 v.isActive = true;
6786 v.uniqueId = UNIQUE_ID;
6787 v.type = ViewportType::INTERNAL;
6788 mFakePolicy->updateViewport(v);
6789 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6790 }
6791
6792 void assertReceivedMove(const Point& point) {
6793 NotifyMotionArgs motionArgs;
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6796 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6798 1, 0, 0, 0, 0, 0, 0, 0));
6799 }
6800};
6801
6802TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareDisplay(DISPLAY_ORIENTATION_0);
6805
6806 prepareButtons();
6807 prepareAxes(POSITION);
6808 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6809
6810 NotifyMotionArgs motionArgs;
6811
6812 // Configure the DisplayViewport such that the logical display maps to a subsection of
6813 // the display panel called the physical display. Here, the physical display is bounded by the
6814 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6815 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6816 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6817 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6818
6819 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6820 DISPLAY_ORIENTATION_270}) {
6821 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6822
6823 // Touches outside the physical display should be ignored, and should not generate any
6824 // events. Ensure touches at the following points that lie outside of the physical display
6825 // area do not generate any events.
6826 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6827 processDown(mapper, toRawX(point.x), toRawY(point.y));
6828 processSync(mapper);
6829 processUp(mapper);
6830 processSync(mapper);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6832 << "Unexpected event generated for touch outside physical display at point: "
6833 << point.x << ", " << point.y;
6834 }
6835 }
6836}
6837
6838TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6839 addConfigurationProperty("touch.deviceType", "touchScreen");
6840 prepareDisplay(DISPLAY_ORIENTATION_0);
6841
6842 prepareButtons();
6843 prepareAxes(POSITION);
6844 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6845
6846 NotifyMotionArgs motionArgs;
6847
6848 // Configure the DisplayViewport such that the logical display maps to a subsection of
6849 // the display panel called the physical display. Here, the physical display is bounded by the
6850 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6851 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6852
6853 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6854 DISPLAY_ORIENTATION_270}) {
6855 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6856
6857 // Touches that start outside the physical display should be ignored until it enters the
6858 // physical display bounds, at which point it should generate a down event. Start a touch at
6859 // the point (5, 100), which is outside the physical display bounds.
6860 static const Point kOutsidePoint{5, 100};
6861 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6864
6865 // Move the touch into the physical display area. This should generate a pointer down.
6866 processMove(mapper, toRawX(11), toRawY(21));
6867 processSync(mapper);
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6869 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6870 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6871 ASSERT_NO_FATAL_FAILURE(
6872 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6873
6874 // Move the touch inside the physical display area. This should generate a pointer move.
6875 processMove(mapper, toRawX(69), toRawY(159));
6876 processSync(mapper);
6877 assertReceivedMove({69, 159});
6878
6879 // Move outside the physical display area. Since the pointer is already down, this should
6880 // now continue generating events.
6881 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6882 processSync(mapper);
6883 assertReceivedMove(kOutsidePoint);
6884
6885 // Release. This should generate a pointer up.
6886 processUp(mapper);
6887 processSync(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6889 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6891 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6892
6893 // Ensure no more events were generated.
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6896 }
6897}
6898
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899// --- MultiTouchInputMapperTest ---
6900
6901class MultiTouchInputMapperTest : public TouchInputMapperTest {
6902protected:
6903 void prepareAxes(int axes);
6904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006905 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6906 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6907 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6908 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6909 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6910 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6911 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6912 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6913 void processId(MultiTouchInputMapper& mapper, int32_t id);
6914 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6915 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6916 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6917 void processMTSync(MultiTouchInputMapper& mapper);
6918 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919};
6920
6921void MultiTouchInputMapperTest::prepareAxes(int axes) {
6922 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 }
6926 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006927 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6928 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006930 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6931 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932 }
6933 }
6934 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6936 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006939 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 }
6941 }
6942 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006943 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6944 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945 }
6946 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006947 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6948 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 }
6950 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006951 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6952 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006953 }
6954 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6956 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957 }
6958 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6960 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961 }
6962 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006963 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964 }
6965}
6966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006967void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6968 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006971}
6972
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006973void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6974 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6979 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981}
6982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006983void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985}
6986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989}
6990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006991void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6992 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994}
6995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006996void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998}
6999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007000void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002}
7003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007004void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006}
7007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007008void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007010}
7011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007012void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014}
7015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7017 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019}
7020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007021void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023}
7024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007025void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007027}
7028
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030 addConfigurationProperty("touch.deviceType", "touchScreen");
7031 prepareDisplay(DISPLAY_ORIENTATION_0);
7032 prepareAxes(POSITION);
7033 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007034 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035
arthurhungdcef2dc2020-08-11 14:47:50 +08007036 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037
7038 NotifyMotionArgs motionArgs;
7039
7040 // Two fingers down at once.
7041 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7042 processPosition(mapper, x1, y1);
7043 processMTSync(mapper);
7044 processPosition(mapper, x2, y2);
7045 processMTSync(mapper);
7046 processSync(mapper);
7047
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7050 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7051 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7052 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7054 ASSERT_EQ(0, motionArgs.flags);
7055 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7056 ASSERT_EQ(0, motionArgs.buttonState);
7057 ASSERT_EQ(0, motionArgs.edgeFlags);
7058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7062 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7063 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7064 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7066
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7069 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7070 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7071 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007072 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 ASSERT_EQ(0, motionArgs.flags);
7074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7075 ASSERT_EQ(0, motionArgs.buttonState);
7076 ASSERT_EQ(0, motionArgs.edgeFlags);
7077 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7080 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7083 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7085 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7086 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7087 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7089
7090 // Move.
7091 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7092 processPosition(mapper, x1, y1);
7093 processMTSync(mapper);
7094 processPosition(mapper, x2, y2);
7095 processMTSync(mapper);
7096 processSync(mapper);
7097
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7100 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7101 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7102 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7104 ASSERT_EQ(0, motionArgs.flags);
7105 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7106 ASSERT_EQ(0, motionArgs.buttonState);
7107 ASSERT_EQ(0, motionArgs.edgeFlags);
7108 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7109 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7111 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7114 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7116 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7117 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7118 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7119 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7120
7121 // First finger up.
7122 x2 += 15; y2 -= 20;
7123 processPosition(mapper, x2, y2);
7124 processMTSync(mapper);
7125 processSync(mapper);
7126
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7129 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7130 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7131 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007132 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007133 ASSERT_EQ(0, motionArgs.flags);
7134 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7135 ASSERT_EQ(0, motionArgs.buttonState);
7136 ASSERT_EQ(0, motionArgs.edgeFlags);
7137 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7138 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7140 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7143 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7145 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7146 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7147 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7149
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7152 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7153 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7154 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7156 ASSERT_EQ(0, motionArgs.flags);
7157 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7158 ASSERT_EQ(0, motionArgs.buttonState);
7159 ASSERT_EQ(0, motionArgs.edgeFlags);
7160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7161 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7164 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7165 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7166 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7168
7169 // Move.
7170 x2 += 20; y2 -= 25;
7171 processPosition(mapper, x2, y2);
7172 processMTSync(mapper);
7173 processSync(mapper);
7174
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7177 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7178 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7179 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7181 ASSERT_EQ(0, motionArgs.flags);
7182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7183 ASSERT_EQ(0, motionArgs.buttonState);
7184 ASSERT_EQ(0, motionArgs.edgeFlags);
7185 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7186 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7189 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7190 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7191 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7193
7194 // New finger down.
7195 int32_t x3 = 700, y3 = 300;
7196 processPosition(mapper, x2, y2);
7197 processMTSync(mapper);
7198 processPosition(mapper, x3, y3);
7199 processMTSync(mapper);
7200 processSync(mapper);
7201
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007207 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208 ASSERT_EQ(0, motionArgs.flags);
7209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7210 ASSERT_EQ(0, motionArgs.buttonState);
7211 ASSERT_EQ(0, motionArgs.edgeFlags);
7212 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7215 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7218 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7219 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7220 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7221 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7222 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7224
7225 // Second finger up.
7226 x3 += 30; y3 -= 20;
7227 processPosition(mapper, x3, y3);
7228 processMTSync(mapper);
7229 processSync(mapper);
7230
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7233 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7234 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7235 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007236 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 ASSERT_EQ(0, motionArgs.flags);
7238 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7239 ASSERT_EQ(0, motionArgs.buttonState);
7240 ASSERT_EQ(0, motionArgs.edgeFlags);
7241 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7242 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7244 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7247 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7249 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7250 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7251 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7253
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7258 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7260 ASSERT_EQ(0, motionArgs.flags);
7261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7262 ASSERT_EQ(0, motionArgs.buttonState);
7263 ASSERT_EQ(0, motionArgs.edgeFlags);
7264 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7268 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7269 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7270 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7272
7273 // Last finger up.
7274 processMTSync(mapper);
7275 processSync(mapper);
7276
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7279 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7280 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7281 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7283 ASSERT_EQ(0, motionArgs.flags);
7284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7285 ASSERT_EQ(0, motionArgs.buttonState);
7286 ASSERT_EQ(0, motionArgs.edgeFlags);
7287 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7288 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7289 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7291 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7295
7296 // Should not have sent any more keys or motions.
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7299}
7300
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007301TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7302 addConfigurationProperty("touch.deviceType", "touchScreen");
7303 prepareDisplay(DISPLAY_ORIENTATION_0);
7304
7305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7306 /*fuzz*/ 0, /*resolution*/ 10);
7307 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7308 /*fuzz*/ 0, /*resolution*/ 11);
7309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7310 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7311 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7312 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7314 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7315 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7316 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7317
7318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7319
7320 // X and Y axes
7321 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7322 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7323 // Touch major and minor
7324 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7325 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7326 // Tool major and minor
7327 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7328 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7329}
7330
7331TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7332 addConfigurationProperty("touch.deviceType", "touchScreen");
7333 prepareDisplay(DISPLAY_ORIENTATION_0);
7334
7335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7336 /*fuzz*/ 0, /*resolution*/ 10);
7337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7338 /*fuzz*/ 0, /*resolution*/ 11);
7339
7340 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7341
7342 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7343
7344 // Touch major and minor
7345 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7346 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7347 // Tool major and minor
7348 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7349 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7350}
7351
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 addConfigurationProperty("touch.deviceType", "touchScreen");
7354 prepareDisplay(DISPLAY_ORIENTATION_0);
7355 prepareAxes(POSITION | ID);
7356 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007357 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007358
arthurhungdcef2dc2020-08-11 14:47:50 +08007359 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360
7361 NotifyMotionArgs motionArgs;
7362
7363 // Two fingers down at once.
7364 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7365 processPosition(mapper, x1, y1);
7366 processId(mapper, 1);
7367 processMTSync(mapper);
7368 processPosition(mapper, x2, y2);
7369 processId(mapper, 2);
7370 processMTSync(mapper);
7371 processSync(mapper);
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7375 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7379 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7380
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007382 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7384 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7386 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7389 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7391 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7392
7393 // Move.
7394 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7395 processPosition(mapper, x1, y1);
7396 processId(mapper, 1);
7397 processMTSync(mapper);
7398 processPosition(mapper, x2, y2);
7399 processId(mapper, 2);
7400 processMTSync(mapper);
7401 processSync(mapper);
7402
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7408 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7411 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7414
7415 // First finger up.
7416 x2 += 15; y2 -= 20;
7417 processPosition(mapper, x2, y2);
7418 processId(mapper, 2);
7419 processMTSync(mapper);
7420 processSync(mapper);
7421
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007423 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007424 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7427 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7430 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7432 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7436 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7437 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7441
7442 // Move.
7443 x2 += 20; y2 -= 25;
7444 processPosition(mapper, x2, y2);
7445 processId(mapper, 2);
7446 processMTSync(mapper);
7447 processSync(mapper);
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7452 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7455 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7456
7457 // New finger down.
7458 int32_t x3 = 700, y3 = 300;
7459 processPosition(mapper, x2, y2);
7460 processId(mapper, 2);
7461 processMTSync(mapper);
7462 processPosition(mapper, x3, y3);
7463 processId(mapper, 3);
7464 processMTSync(mapper);
7465 processSync(mapper);
7466
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007468 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7470 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7472 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7475 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7477 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7478
7479 // Second finger up.
7480 x3 += 30; y3 -= 20;
7481 processPosition(mapper, x3, y3);
7482 processId(mapper, 3);
7483 processMTSync(mapper);
7484 processSync(mapper);
7485
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007487 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7491 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7494 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7496 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7497
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7500 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7504 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7505
7506 // Last finger up.
7507 processMTSync(mapper);
7508 processSync(mapper);
7509
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7512 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7513 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7516 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7517
7518 // Should not have sent any more keys or motions.
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7521}
7522
7523TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 addConfigurationProperty("touch.deviceType", "touchScreen");
7525 prepareDisplay(DISPLAY_ORIENTATION_0);
7526 prepareAxes(POSITION | ID | SLOT);
7527 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007528 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529
arthurhungdcef2dc2020-08-11 14:47:50 +08007530 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531
7532 NotifyMotionArgs motionArgs;
7533
7534 // Two fingers down at once.
7535 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7536 processPosition(mapper, x1, y1);
7537 processId(mapper, 1);
7538 processSlot(mapper, 1);
7539 processPosition(mapper, x2, y2);
7540 processId(mapper, 2);
7541 processSync(mapper);
7542
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007552 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7554 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7556 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7559 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7561 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7562
7563 // Move.
7564 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7565 processSlot(mapper, 0);
7566 processPosition(mapper, x1, y1);
7567 processSlot(mapper, 1);
7568 processPosition(mapper, x2, y2);
7569 processSync(mapper);
7570
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7573 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7574 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7575 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7576 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7579 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7581 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7582
7583 // First finger up.
7584 x2 += 15; y2 -= 20;
7585 processSlot(mapper, 0);
7586 processId(mapper, -1);
7587 processSlot(mapper, 1);
7588 processPosition(mapper, x2, y2);
7589 processSync(mapper);
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007592 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007593 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7599 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7602
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7605 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7606 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7609 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7610
7611 // Move.
7612 x2 += 20; y2 -= 25;
7613 processPosition(mapper, x2, y2);
7614 processSync(mapper);
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7618 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7619 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // New finger down.
7625 int32_t x3 = 700, y3 = 300;
7626 processPosition(mapper, x2, y2);
7627 processSlot(mapper, 0);
7628 processId(mapper, 3);
7629 processPosition(mapper, x3, y3);
7630 processSync(mapper);
7631
7632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007633 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // Second finger up.
7645 x3 += 30; y3 -= 20;
7646 processSlot(mapper, 1);
7647 processId(mapper, -1);
7648 processSlot(mapper, 0);
7649 processPosition(mapper, x3, y3);
7650 processSync(mapper);
7651
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007653 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7663
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7666 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7670 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7671
7672 // Last finger up.
7673 processId(mapper, -1);
7674 processSync(mapper);
7675
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7677 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7678 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7682 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7683
7684 // Should not have sent any more keys or motions.
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7687}
7688
7689TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 addConfigurationProperty("touch.deviceType", "touchScreen");
7691 prepareDisplay(DISPLAY_ORIENTATION_0);
7692 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007693 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694
7695 // These calculations are based on the input device calibration documentation.
7696 int32_t rawX = 100;
7697 int32_t rawY = 200;
7698 int32_t rawTouchMajor = 7;
7699 int32_t rawTouchMinor = 6;
7700 int32_t rawToolMajor = 9;
7701 int32_t rawToolMinor = 8;
7702 int32_t rawPressure = 11;
7703 int32_t rawDistance = 0;
7704 int32_t rawOrientation = 3;
7705 int32_t id = 5;
7706
7707 float x = toDisplayX(rawX);
7708 float y = toDisplayY(rawY);
7709 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7710 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7711 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7712 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7713 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7714 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7715 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7716 float distance = float(rawDistance);
7717
7718 processPosition(mapper, rawX, rawY);
7719 processTouchMajor(mapper, rawTouchMajor);
7720 processTouchMinor(mapper, rawTouchMinor);
7721 processToolMajor(mapper, rawToolMajor);
7722 processToolMinor(mapper, rawToolMinor);
7723 processPressure(mapper, rawPressure);
7724 processOrientation(mapper, rawOrientation);
7725 processDistance(mapper, rawDistance);
7726 processId(mapper, id);
7727 processMTSync(mapper);
7728 processSync(mapper);
7729
7730 NotifyMotionArgs args;
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7732 ASSERT_EQ(0, args.pointerProperties[0].id);
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7734 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7735 orientation, distance));
7736}
7737
7738TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 addConfigurationProperty("touch.deviceType", "touchScreen");
7740 prepareDisplay(DISPLAY_ORIENTATION_0);
7741 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7742 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744
7745 // These calculations are based on the input device calibration documentation.
7746 int32_t rawX = 100;
7747 int32_t rawY = 200;
7748 int32_t rawTouchMajor = 140;
7749 int32_t rawTouchMinor = 120;
7750 int32_t rawToolMajor = 180;
7751 int32_t rawToolMinor = 160;
7752
7753 float x = toDisplayX(rawX);
7754 float y = toDisplayY(rawY);
7755 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7756 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7757 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7758 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7759 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7760
7761 processPosition(mapper, rawX, rawY);
7762 processTouchMajor(mapper, rawTouchMajor);
7763 processTouchMinor(mapper, rawTouchMinor);
7764 processToolMajor(mapper, rawToolMajor);
7765 processToolMinor(mapper, rawToolMinor);
7766 processMTSync(mapper);
7767 processSync(mapper);
7768
7769 NotifyMotionArgs args;
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7772 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7773}
7774
7775TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 addConfigurationProperty("touch.deviceType", "touchScreen");
7777 prepareDisplay(DISPLAY_ORIENTATION_0);
7778 prepareAxes(POSITION | TOUCH | TOOL);
7779 addConfigurationProperty("touch.size.calibration", "diameter");
7780 addConfigurationProperty("touch.size.scale", "10");
7781 addConfigurationProperty("touch.size.bias", "160");
7782 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007783 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784
7785 // These calculations are based on the input device calibration documentation.
7786 // Note: We only provide a single common touch/tool value because the device is assumed
7787 // not to emit separate values for each pointer (isSummed = 1).
7788 int32_t rawX = 100;
7789 int32_t rawY = 200;
7790 int32_t rawX2 = 150;
7791 int32_t rawY2 = 250;
7792 int32_t rawTouchMajor = 5;
7793 int32_t rawToolMajor = 8;
7794
7795 float x = toDisplayX(rawX);
7796 float y = toDisplayY(rawY);
7797 float x2 = toDisplayX(rawX2);
7798 float y2 = toDisplayY(rawY2);
7799 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7800 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7801 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7802
7803 processPosition(mapper, rawX, rawY);
7804 processTouchMajor(mapper, rawTouchMajor);
7805 processToolMajor(mapper, rawToolMajor);
7806 processMTSync(mapper);
7807 processPosition(mapper, rawX2, rawY2);
7808 processTouchMajor(mapper, rawTouchMajor);
7809 processToolMajor(mapper, rawToolMajor);
7810 processMTSync(mapper);
7811 processSync(mapper);
7812
7813 NotifyMotionArgs args;
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7815 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007818 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_EQ(size_t(2), args.pointerCount);
7820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7821 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7823 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7824}
7825
7826TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007827 addConfigurationProperty("touch.deviceType", "touchScreen");
7828 prepareDisplay(DISPLAY_ORIENTATION_0);
7829 prepareAxes(POSITION | TOUCH | TOOL);
7830 addConfigurationProperty("touch.size.calibration", "area");
7831 addConfigurationProperty("touch.size.scale", "43");
7832 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007833 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834
7835 // These calculations are based on the input device calibration documentation.
7836 int32_t rawX = 100;
7837 int32_t rawY = 200;
7838 int32_t rawTouchMajor = 5;
7839 int32_t rawToolMajor = 8;
7840
7841 float x = toDisplayX(rawX);
7842 float y = toDisplayY(rawY);
7843 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7844 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7845 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7846
7847 processPosition(mapper, rawX, rawY);
7848 processTouchMajor(mapper, rawTouchMajor);
7849 processToolMajor(mapper, rawToolMajor);
7850 processMTSync(mapper);
7851 processSync(mapper);
7852
7853 NotifyMotionArgs args;
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7856 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7857}
7858
7859TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 addConfigurationProperty("touch.deviceType", "touchScreen");
7861 prepareDisplay(DISPLAY_ORIENTATION_0);
7862 prepareAxes(POSITION | PRESSURE);
7863 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7864 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007865 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866
Michael Wrightaa449c92017-12-13 21:21:43 +00007867 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007868 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007869 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7870 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7871 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7872
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 // These calculations are based on the input device calibration documentation.
7874 int32_t rawX = 100;
7875 int32_t rawY = 200;
7876 int32_t rawPressure = 60;
7877
7878 float x = toDisplayX(rawX);
7879 float y = toDisplayY(rawY);
7880 float pressure = float(rawPressure) * 0.01f;
7881
7882 processPosition(mapper, rawX, rawY);
7883 processPressure(mapper, rawPressure);
7884 processMTSync(mapper);
7885 processSync(mapper);
7886
7887 NotifyMotionArgs args;
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7890 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7891}
7892
7893TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 addConfigurationProperty("touch.deviceType", "touchScreen");
7895 prepareDisplay(DISPLAY_ORIENTATION_0);
7896 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007897 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898
7899 NotifyMotionArgs motionArgs;
7900 NotifyKeyArgs keyArgs;
7901
7902 processId(mapper, 1);
7903 processPosition(mapper, 100, 200);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7907 ASSERT_EQ(0, motionArgs.buttonState);
7908
7909 // press BTN_LEFT, release BTN_LEFT
7910 processKey(mapper, BTN_LEFT, 1);
7911 processSync(mapper);
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7914 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7915
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7918 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7919
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 processKey(mapper, BTN_LEFT, 0);
7921 processSync(mapper);
7922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007925
7926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007928 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929
7930 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7931 processKey(mapper, BTN_RIGHT, 1);
7932 processKey(mapper, BTN_MIDDLE, 1);
7933 processSync(mapper);
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7936 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7937 motionArgs.buttonState);
7938
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7940 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7941 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7942
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7945 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7946 motionArgs.buttonState);
7947
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948 processKey(mapper, BTN_RIGHT, 0);
7949 processSync(mapper);
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007953
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007956 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957
7958 processKey(mapper, BTN_MIDDLE, 0);
7959 processSync(mapper);
7960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007963
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007966 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967
7968 // press BTN_BACK, release BTN_BACK
7969 processKey(mapper, BTN_BACK, 1);
7970 processSync(mapper);
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7972 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7973 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007974
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7978
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7981 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982
7983 processKey(mapper, BTN_BACK, 0);
7984 processSync(mapper);
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007988
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007991 ASSERT_EQ(0, motionArgs.buttonState);
7992
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7994 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7995 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7996
7997 // press BTN_SIDE, release BTN_SIDE
7998 processKey(mapper, BTN_SIDE, 1);
7999 processSync(mapper);
8000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8001 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8002 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008003
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008006 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8007
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8010 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011
8012 processKey(mapper, BTN_SIDE, 0);
8013 processSync(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008017
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008020 ASSERT_EQ(0, motionArgs.buttonState);
8021
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8023 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8024 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8025
8026 // press BTN_FORWARD, release BTN_FORWARD
8027 processKey(mapper, BTN_FORWARD, 1);
8028 processSync(mapper);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8030 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8031 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008032
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8039 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040
8041 processKey(mapper, BTN_FORWARD, 0);
8042 processSync(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008049 ASSERT_EQ(0, motionArgs.buttonState);
8050
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8052 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8053 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8054
8055 // press BTN_EXTRA, release BTN_EXTRA
8056 processKey(mapper, BTN_EXTRA, 1);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8059 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8060 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008061
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008064 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8065
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8068 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069
8070 processKey(mapper, BTN_EXTRA, 0);
8071 processSync(mapper);
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008075
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078 ASSERT_EQ(0, motionArgs.buttonState);
8079
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8081 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8082 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8083
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8085
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 // press BTN_STYLUS, release BTN_STYLUS
8087 processKey(mapper, BTN_STYLUS, 1);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8092
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8095 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096
8097 processKey(mapper, BTN_STYLUS, 0);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008105 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106
8107 // press BTN_STYLUS2, release BTN_STYLUS2
8108 processKey(mapper, BTN_STYLUS2, 1);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8113
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 processKey(mapper, BTN_STYLUS2, 0);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127
8128 // release touch
8129 processId(mapper, -1);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8133 ASSERT_EQ(0, motionArgs.buttonState);
8134}
8135
8136TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 addConfigurationProperty("touch.deviceType", "touchScreen");
8138 prepareDisplay(DISPLAY_ORIENTATION_0);
8139 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141
8142 NotifyMotionArgs motionArgs;
8143
8144 // default tool type is finger
8145 processId(mapper, 1);
8146 processPosition(mapper, 100, 200);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8149 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8151
8152 // eraser
8153 processKey(mapper, BTN_TOOL_RUBBER, 1);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8158
8159 // stylus
8160 processKey(mapper, BTN_TOOL_RUBBER, 0);
8161 processKey(mapper, BTN_TOOL_PEN, 1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8166
8167 // brush
8168 processKey(mapper, BTN_TOOL_PEN, 0);
8169 processKey(mapper, BTN_TOOL_BRUSH, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8174
8175 // pencil
8176 processKey(mapper, BTN_TOOL_BRUSH, 0);
8177 processKey(mapper, BTN_TOOL_PENCIL, 1);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8182
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008183 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184 processKey(mapper, BTN_TOOL_PENCIL, 0);
8185 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8190
8191 // mouse
8192 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8193 processKey(mapper, BTN_TOOL_MOUSE, 1);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8198
8199 // lens
8200 processKey(mapper, BTN_TOOL_MOUSE, 0);
8201 processKey(mapper, BTN_TOOL_LENS, 1);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8206
8207 // double-tap
8208 processKey(mapper, BTN_TOOL_LENS, 0);
8209 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
8215 // triple-tap
8216 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8217 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8222
8223 // quad-tap
8224 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8225 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8230
8231 // finger
8232 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8233 processKey(mapper, BTN_TOOL_FINGER, 1);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8238
8239 // stylus trumps finger
8240 processKey(mapper, BTN_TOOL_PEN, 1);
8241 processSync(mapper);
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8245
8246 // eraser trumps stylus
8247 processKey(mapper, BTN_TOOL_RUBBER, 1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8252
8253 // mouse trumps eraser
8254 processKey(mapper, BTN_TOOL_MOUSE, 1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8259
8260 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8261 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8265 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8266
8267 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8268 processToolType(mapper, MT_TOOL_PEN);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8273
8274 // back to default tool type
8275 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8276 processKey(mapper, BTN_TOOL_MOUSE, 0);
8277 processKey(mapper, BTN_TOOL_RUBBER, 0);
8278 processKey(mapper, BTN_TOOL_PEN, 0);
8279 processKey(mapper, BTN_TOOL_FINGER, 0);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8284}
8285
8286TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 addConfigurationProperty("touch.deviceType", "touchScreen");
8288 prepareDisplay(DISPLAY_ORIENTATION_0);
8289 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008290 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008291 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008292
8293 NotifyMotionArgs motionArgs;
8294
8295 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8296 processId(mapper, 1);
8297 processPosition(mapper, 100, 200);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8302 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8303
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8307 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8308
8309 // move a little
8310 processPosition(mapper, 150, 250);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8315 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8316
8317 // down when BTN_TOUCH is pressed, pressure defaults to 1
8318 processKey(mapper, BTN_TOUCH, 1);
8319 processSync(mapper);
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8323 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8324
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8328 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8329
8330 // up when BTN_TOUCH is released, hover restored
8331 processKey(mapper, BTN_TOUCH, 0);
8332 processSync(mapper);
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8336 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8337
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8342
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8346 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8347
8348 // exit hover when pointer goes away
8349 processId(mapper, -1);
8350 processSync(mapper);
8351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8354 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8355}
8356
8357TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 addConfigurationProperty("touch.deviceType", "touchScreen");
8359 prepareDisplay(DISPLAY_ORIENTATION_0);
8360 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008361 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008362
8363 NotifyMotionArgs motionArgs;
8364
8365 // initially hovering because pressure is 0
8366 processId(mapper, 1);
8367 processPosition(mapper, 100, 200);
8368 processPressure(mapper, 0);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8373 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8374
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8379
8380 // move a little
8381 processPosition(mapper, 150, 250);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8387
8388 // down when pressure becomes non-zero
8389 processPressure(mapper, RAW_PRESSURE_MAX);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8395
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8400
8401 // up when pressure becomes 0, hover restored
8402 processPressure(mapper, 0);
8403 processSync(mapper);
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8407 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8408
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8413
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8417 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8418
8419 // exit hover when pointer goes away
8420 processId(mapper, -1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8425 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8426}
8427
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008428/**
8429 * Set the input device port <--> display port associations, and check that the
8430 * events are routed to the display that matches the display port.
8431 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8432 */
8433TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008434 const std::string usb2 = "USB2";
8435 const uint8_t hdmi1 = 0;
8436 const uint8_t hdmi2 = 1;
8437 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008438 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008439
8440 addConfigurationProperty("touch.deviceType", "touchScreen");
8441 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008443
8444 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8445 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8446
8447 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8448 // for this input device is specified, and the matching viewport is not present,
8449 // the input device should be disabled (at the mapper level).
8450
8451 // Add viewport for display 2 on hdmi2
8452 prepareSecondaryDisplay(type, hdmi2);
8453 // Send a touch event
8454 processPosition(mapper, 100, 100);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8457
8458 // Add viewport for display 1 on hdmi1
8459 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8460 // Send a touch event again
8461 processPosition(mapper, 100, 100);
8462 processSync(mapper);
8463
8464 NotifyMotionArgs args;
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8466 ASSERT_EQ(DISPLAY_ID, args.displayId);
8467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008469TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8470 addConfigurationProperty("touch.deviceType", "touchScreen");
8471 prepareAxes(POSITION);
8472 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8473
8474 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8475
8476 prepareDisplay(DISPLAY_ORIENTATION_0);
8477 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8478
8479 // Send a touch event
8480 processPosition(mapper, 100, 100);
8481 processSync(mapper);
8482
8483 NotifyMotionArgs args;
8484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8485 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8486}
8487
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008488TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008489 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008490 std::shared_ptr<FakePointerController> fakePointerController =
8491 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008492 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008493 fakePointerController->setPosition(100, 200);
8494 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008495 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008496
Garfield Tan888a6a42020-01-09 11:39:16 -08008497 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008498 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008499
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008500 prepareDisplay(DISPLAY_ORIENTATION_0);
8501 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008502 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008503
8504 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008505 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008506
8507 NotifyMotionArgs motionArgs;
8508 processPosition(mapper, 100, 100);
8509 processSync(mapper);
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8513 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8514}
8515
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008516/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008517 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8518 */
8519TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8520 addConfigurationProperty("touch.deviceType", "touchScreen");
8521 prepareAxes(POSITION);
8522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8523
8524 prepareDisplay(DISPLAY_ORIENTATION_0);
8525 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8526 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8527 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8528 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8529
8530 NotifyMotionArgs args;
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8532 ASSERT_EQ(26, args.readTime);
8533
8534 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8535 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8536 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8537
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8539 ASSERT_EQ(33, args.readTime);
8540}
8541
8542/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008543 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8544 * events should not be delivered to the listener.
8545 */
8546TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8547 addConfigurationProperty("touch.deviceType", "touchScreen");
8548 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8549 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8550 ViewportType::INTERNAL);
8551 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8552 prepareAxes(POSITION);
8553 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8554
8555 NotifyMotionArgs motionArgs;
8556 processPosition(mapper, 100, 100);
8557 processSync(mapper);
8558
8559 mFakeListener->assertNotifyMotionWasNotCalled();
8560}
8561
Garfield Tanc734e4f2021-01-15 20:01:39 -08008562TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8563 addConfigurationProperty("touch.deviceType", "touchScreen");
8564 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8565 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8566 ViewportType::INTERNAL);
8567 std::optional<DisplayViewport> optionalDisplayViewport =
8568 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8569 ASSERT_TRUE(optionalDisplayViewport.has_value());
8570 DisplayViewport displayViewport = *optionalDisplayViewport;
8571
8572 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8573 prepareAxes(POSITION);
8574 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8575
8576 // Finger down
8577 int32_t x = 100, y = 100;
8578 processPosition(mapper, x, y);
8579 processSync(mapper);
8580
8581 NotifyMotionArgs motionArgs;
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8584
8585 // Deactivate display viewport
8586 displayViewport.isActive = false;
8587 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8588 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8589
8590 // Finger move
8591 x += 10, y += 10;
8592 processPosition(mapper, x, y);
8593 processSync(mapper);
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8597
8598 // Reactivate display viewport
8599 displayViewport.isActive = true;
8600 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8601 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8602
8603 // Finger move again
8604 x += 10, y += 10;
8605 processPosition(mapper, x, y);
8606 processSync(mapper);
8607
8608 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8609 // no pointer on the touch device.
8610 mFakeListener->assertNotifyMotionWasNotCalled();
8611}
8612
Arthur Hung7c645402019-01-25 17:45:42 +08008613TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8614 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008615 prepareAxes(POSITION | ID | SLOT);
8616 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008618
8619 // Create the second touch screen device, and enable multi fingers.
8620 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008621 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008622 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008623 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008624 std::shared_ptr<InputDevice> device2 =
8625 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008626 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008627
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008628 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8629 0 /*flat*/, 0 /*fuzz*/);
8630 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8631 0 /*flat*/, 0 /*fuzz*/);
8632 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8633 0 /*flat*/, 0 /*fuzz*/);
8634 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8635 0 /*flat*/, 0 /*fuzz*/);
8636 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8637 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8638 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008639
8640 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008641 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008642 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8643 device2->reset(ARBITRARY_TIME);
8644
8645 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008646 std::shared_ptr<FakePointerController> fakePointerController =
8647 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008648 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008649
8650 // Setup policy for associated displays and show touches.
8651 const uint8_t hdmi1 = 0;
8652 const uint8_t hdmi2 = 1;
8653 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8654 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8655 mFakePolicy->setShowTouches(true);
8656
8657 // Create displays.
8658 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008659 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008660
8661 // Default device will reconfigure above, need additional reconfiguration for another device.
8662 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008663 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8664 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008665
8666 // Two fingers down at default display.
8667 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8668 processPosition(mapper, x1, y1);
8669 processId(mapper, 1);
8670 processSlot(mapper, 1);
8671 processPosition(mapper, x2, y2);
8672 processId(mapper, 2);
8673 processSync(mapper);
8674
8675 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8676 fakePointerController->getSpots().find(DISPLAY_ID);
8677 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8678 ASSERT_EQ(size_t(2), iter->second.size());
8679
8680 // Two fingers down at second display.
8681 processPosition(mapper2, x1, y1);
8682 processId(mapper2, 1);
8683 processSlot(mapper2, 1);
8684 processPosition(mapper2, x2, y2);
8685 processId(mapper2, 2);
8686 processSync(mapper2);
8687
8688 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8689 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8690 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008691
8692 // Disable the show touches configuration and ensure the spots are cleared.
8693 mFakePolicy->setShowTouches(false);
8694 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8695 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8696
8697 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008698}
8699
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008700TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008701 prepareAxes(POSITION);
8702 addConfigurationProperty("touch.deviceType", "touchScreen");
8703 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008705
8706 NotifyMotionArgs motionArgs;
8707 // Unrotated video frame
8708 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8709 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008710 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008711 processPosition(mapper, 100, 200);
8712 processSync(mapper);
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(frames, motionArgs.videoFrames);
8715
8716 // Subsequent touch events should not have any videoframes
8717 // This is implemented separately in FakeEventHub,
8718 // but that should match the behaviour of TouchVideoDevice.
8719 processPosition(mapper, 200, 200);
8720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8722 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8723}
8724
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008725TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008726 prepareAxes(POSITION);
8727 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008729 // Unrotated video frame
8730 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8731 NotifyMotionArgs motionArgs;
8732
8733 // Test all 4 orientations
8734 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008735 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8736 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8737 clearViewports();
8738 prepareDisplay(orientation);
8739 std::vector<TouchVideoFrame> frames{frame};
8740 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8741 processPosition(mapper, 100, 200);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(frames, motionArgs.videoFrames);
8745 }
8746}
8747
8748TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8749 prepareAxes(POSITION);
8750 addConfigurationProperty("touch.deviceType", "touchScreen");
8751 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8752 // orientation-aware are affected by display rotation.
8753 addConfigurationProperty("touch.orientationAware", "0");
8754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8755 // Unrotated video frame
8756 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8757 NotifyMotionArgs motionArgs;
8758
8759 // Test all 4 orientations
8760 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008761 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8762 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8763 clearViewports();
8764 prepareDisplay(orientation);
8765 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008766 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008767 processPosition(mapper, 100, 200);
8768 processSync(mapper);
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008770 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8771 // compared to the display. This is so that when the window transform (which contains the
8772 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8773 // window's coordinate space.
8774 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008775 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008776
8777 // Release finger.
8778 processSync(mapper);
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008780 }
8781}
8782
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008783TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008784 prepareAxes(POSITION);
8785 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008787 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8788 // so mix these.
8789 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8790 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8791 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8792 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8793 NotifyMotionArgs motionArgs;
8794
8795 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008796 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008797 processPosition(mapper, 100, 200);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008800 ASSERT_EQ(frames, motionArgs.videoFrames);
8801}
8802
8803TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8804 prepareAxes(POSITION);
8805 addConfigurationProperty("touch.deviceType", "touchScreen");
8806 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8807 // orientation-aware are affected by display rotation.
8808 addConfigurationProperty("touch.orientationAware", "0");
8809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8810 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8811 // so mix these.
8812 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8813 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8814 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8815 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8816 NotifyMotionArgs motionArgs;
8817
8818 prepareDisplay(DISPLAY_ORIENTATION_90);
8819 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8820 processPosition(mapper, 100, 200);
8821 processSync(mapper);
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8824 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8825 // compared to the display. This is so that when the window transform (which contains the
8826 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8827 // window's coordinate space.
8828 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8829 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008830 ASSERT_EQ(frames, motionArgs.videoFrames);
8831}
8832
Arthur Hung9da14732019-09-02 16:16:58 +08008833/**
8834 * If we had defined port associations, but the viewport is not ready, the touch device would be
8835 * expected to be disabled, and it should be enabled after the viewport has found.
8836 */
8837TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008838 constexpr uint8_t hdmi2 = 1;
8839 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008840 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008841
8842 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8843
8844 addConfigurationProperty("touch.deviceType", "touchScreen");
8845 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008846 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008847
8848 ASSERT_EQ(mDevice->isEnabled(), false);
8849
8850 // Add display on hdmi2, the device should be enabled and can receive touch event.
8851 prepareSecondaryDisplay(type, hdmi2);
8852 ASSERT_EQ(mDevice->isEnabled(), true);
8853
8854 // Send a touch event.
8855 processPosition(mapper, 100, 100);
8856 processSync(mapper);
8857
8858 NotifyMotionArgs args;
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8860 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8861}
8862
Arthur Hung421eb1c2020-01-16 00:09:42 +08008863TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008864 addConfigurationProperty("touch.deviceType", "touchScreen");
8865 prepareDisplay(DISPLAY_ORIENTATION_0);
8866 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008868
8869 NotifyMotionArgs motionArgs;
8870
8871 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8872 // finger down
8873 processId(mapper, 1);
8874 processPosition(mapper, x1, y1);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8879
8880 // finger move
8881 processId(mapper, 1);
8882 processPosition(mapper, x2, y2);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8887
8888 // finger up.
8889 processId(mapper, -1);
8890 processSync(mapper);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8894
8895 // new finger down
8896 processId(mapper, 1);
8897 processPosition(mapper, x3, y3);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8902}
8903
8904/**
arthurhungcc7f9802020-04-30 17:55:40 +08008905 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8906 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008907 */
arthurhungcc7f9802020-04-30 17:55:40 +08008908TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008909 addConfigurationProperty("touch.deviceType", "touchScreen");
8910 prepareDisplay(DISPLAY_ORIENTATION_0);
8911 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008913
8914 NotifyMotionArgs motionArgs;
8915
8916 // default tool type is finger
8917 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008918 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008919 processPosition(mapper, x1, y1);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8924
8925 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8926 processToolType(mapper, MT_TOOL_PALM);
8927 processSync(mapper);
8928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8929 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8930
8931 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008932 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008933 processPosition(mapper, x2, y2);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8936
8937 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008938 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008939 processSync(mapper);
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8941
8942 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008943 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008944 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008945 processPosition(mapper, x3, y3);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8950}
8951
arthurhungbf89a482020-04-17 17:37:55 +08008952/**
arthurhungcc7f9802020-04-30 17:55:40 +08008953 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8954 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008955 */
arthurhungcc7f9802020-04-30 17:55:40 +08008956TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008957 addConfigurationProperty("touch.deviceType", "touchScreen");
8958 prepareDisplay(DISPLAY_ORIENTATION_0);
8959 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8961
8962 NotifyMotionArgs motionArgs;
8963
8964 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008965 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8966 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008967 processPosition(mapper, x1, y1);
8968 processSync(mapper);
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8972
8973 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008974 processSlot(mapper, SECOND_SLOT);
8975 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008976 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008979 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8981
8982 // If the tool type of the first finger changes to MT_TOOL_PALM,
8983 // we expect to receive ACTION_POINTER_UP with cancel flag.
8984 processSlot(mapper, FIRST_SLOT);
8985 processId(mapper, FIRST_TRACKING_ID);
8986 processToolType(mapper, MT_TOOL_PALM);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008989 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008990 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8991
8992 // The following MOVE events of second finger should be processed.
8993 processSlot(mapper, SECOND_SLOT);
8994 processId(mapper, SECOND_TRACKING_ID);
8995 processPosition(mapper, x2 + 1, y2 + 1);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8999 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9000
9001 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9002 // it. Second finger receive move.
9003 processSlot(mapper, FIRST_SLOT);
9004 processId(mapper, INVALID_TRACKING_ID);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9008 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9009
9010 // Second finger keeps moving.
9011 processSlot(mapper, SECOND_SLOT);
9012 processId(mapper, SECOND_TRACKING_ID);
9013 processPosition(mapper, x2 + 2, y2 + 2);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9017 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9018
9019 // Second finger up.
9020 processId(mapper, INVALID_TRACKING_ID);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9024 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9025}
9026
9027/**
9028 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9029 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9030 */
9031TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9032 addConfigurationProperty("touch.deviceType", "touchScreen");
9033 prepareDisplay(DISPLAY_ORIENTATION_0);
9034 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9035 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9036
9037 NotifyMotionArgs motionArgs;
9038
9039 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9040 // First finger down.
9041 processId(mapper, FIRST_TRACKING_ID);
9042 processPosition(mapper, x1, y1);
9043 processSync(mapper);
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9047
9048 // Second finger down.
9049 processSlot(mapper, SECOND_SLOT);
9050 processId(mapper, SECOND_TRACKING_ID);
9051 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009054 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9056
arthurhungcc7f9802020-04-30 17:55:40 +08009057 // If the tool type of the first finger changes to MT_TOOL_PALM,
9058 // we expect to receive ACTION_POINTER_UP with cancel flag.
9059 processSlot(mapper, FIRST_SLOT);
9060 processId(mapper, FIRST_TRACKING_ID);
9061 processToolType(mapper, MT_TOOL_PALM);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009064 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009065 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9066
9067 // Second finger keeps moving.
9068 processSlot(mapper, SECOND_SLOT);
9069 processId(mapper, SECOND_TRACKING_ID);
9070 processPosition(mapper, x2 + 1, y2 + 1);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9074
9075 // second finger becomes palm, receive cancel due to only 1 finger is active.
9076 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009077 processToolType(mapper, MT_TOOL_PALM);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9081
arthurhungcc7f9802020-04-30 17:55:40 +08009082 // third finger down.
9083 processSlot(mapper, THIRD_SLOT);
9084 processId(mapper, THIRD_TRACKING_ID);
9085 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009086 processPosition(mapper, x3, y3);
9087 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009091 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9092
9093 // third finger move
9094 processId(mapper, THIRD_TRACKING_ID);
9095 processPosition(mapper, x3 + 1, y3 + 1);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9099
9100 // first finger up, third finger receive move.
9101 processSlot(mapper, FIRST_SLOT);
9102 processId(mapper, INVALID_TRACKING_ID);
9103 processSync(mapper);
9104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9106 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9107
9108 // second finger up, third finger receive move.
9109 processSlot(mapper, SECOND_SLOT);
9110 processId(mapper, INVALID_TRACKING_ID);
9111 processSync(mapper);
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9114 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9115
9116 // third finger up.
9117 processSlot(mapper, THIRD_SLOT);
9118 processId(mapper, INVALID_TRACKING_ID);
9119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9121 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9122 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9123}
9124
9125/**
9126 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9127 * and the active finger could still be allowed to receive the events
9128 */
9129TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9130 addConfigurationProperty("touch.deviceType", "touchScreen");
9131 prepareDisplay(DISPLAY_ORIENTATION_0);
9132 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9134
9135 NotifyMotionArgs motionArgs;
9136
9137 // default tool type is finger
9138 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9139 processId(mapper, FIRST_TRACKING_ID);
9140 processPosition(mapper, x1, y1);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9145
9146 // Second finger down.
9147 processSlot(mapper, SECOND_SLOT);
9148 processId(mapper, SECOND_TRACKING_ID);
9149 processPosition(mapper, x2, y2);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009152 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9154
9155 // If the tool type of the second finger changes to MT_TOOL_PALM,
9156 // we expect to receive ACTION_POINTER_UP with cancel flag.
9157 processId(mapper, SECOND_TRACKING_ID);
9158 processToolType(mapper, MT_TOOL_PALM);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009161 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009162 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9163
9164 // The following MOVE event should be processed.
9165 processSlot(mapper, FIRST_SLOT);
9166 processId(mapper, FIRST_TRACKING_ID);
9167 processPosition(mapper, x1 + 1, y1 + 1);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9171 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9172
9173 // second finger up.
9174 processSlot(mapper, SECOND_SLOT);
9175 processId(mapper, INVALID_TRACKING_ID);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9179
9180 // first finger keep moving
9181 processSlot(mapper, FIRST_SLOT);
9182 processId(mapper, FIRST_TRACKING_ID);
9183 processPosition(mapper, x1 + 2, y1 + 2);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9187
9188 // first finger up.
9189 processId(mapper, INVALID_TRACKING_ID);
9190 processSync(mapper);
9191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9192 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9193 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009194}
9195
Arthur Hung9ad18942021-06-19 02:04:46 +00009196/**
9197 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9198 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9199 * cause slot be valid again.
9200 */
9201TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9202 addConfigurationProperty("touch.deviceType", "touchScreen");
9203 prepareDisplay(DISPLAY_ORIENTATION_0);
9204 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9206
9207 NotifyMotionArgs motionArgs;
9208
9209 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9210 // First finger down.
9211 processId(mapper, FIRST_TRACKING_ID);
9212 processPosition(mapper, x1, y1);
9213 processPressure(mapper, RAW_PRESSURE_MAX);
9214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9217 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9218
9219 // First finger move.
9220 processId(mapper, FIRST_TRACKING_ID);
9221 processPosition(mapper, x1 + 1, y1 + 1);
9222 processPressure(mapper, RAW_PRESSURE_MAX);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9226 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9227
9228 // Second finger down.
9229 processSlot(mapper, SECOND_SLOT);
9230 processId(mapper, SECOND_TRACKING_ID);
9231 processPosition(mapper, x2, y2);
9232 processPressure(mapper, RAW_PRESSURE_MAX);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009235 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009236 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9237
9238 // second finger up with some unexpected data.
9239 processSlot(mapper, SECOND_SLOT);
9240 processId(mapper, INVALID_TRACKING_ID);
9241 processPosition(mapper, x2, y2);
9242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009244 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009245 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9246
9247 // first finger up with some unexpected data.
9248 processSlot(mapper, FIRST_SLOT);
9249 processId(mapper, INVALID_TRACKING_ID);
9250 processPosition(mapper, x2, y2);
9251 processPressure(mapper, RAW_PRESSURE_MAX);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9255 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9256}
9257
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009258// --- MultiTouchInputMapperTest_ExternalDevice ---
9259
9260class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9261protected:
Chris Yea52ade12020-08-27 16:49:20 -07009262 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009263};
9264
9265/**
9266 * Expect fallback to internal viewport if device is external and external viewport is not present.
9267 */
9268TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9269 prepareAxes(POSITION);
9270 addConfigurationProperty("touch.deviceType", "touchScreen");
9271 prepareDisplay(DISPLAY_ORIENTATION_0);
9272 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9273
9274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9275
9276 NotifyMotionArgs motionArgs;
9277
9278 // Expect the event to be sent to the internal viewport,
9279 // because an external viewport is not present.
9280 processPosition(mapper, 100, 100);
9281 processSync(mapper);
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9283 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9284
9285 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009286 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009287 processPosition(mapper, 100, 100);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9290 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9291}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009292
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009293TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9294 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9295 std::shared_ptr<FakePointerController> fakePointerController =
9296 std::make_shared<FakePointerController>();
9297 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9298 fakePointerController->setPosition(0, 0);
9299 fakePointerController->setButtonState(0);
9300
9301 // prepare device and capture
9302 prepareDisplay(DISPLAY_ORIENTATION_0);
9303 prepareAxes(POSITION | ID | SLOT);
9304 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9305 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9306 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009307 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009308 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9309
9310 // captured touchpad should be a touchpad source
9311 NotifyDeviceResetArgs resetArgs;
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9313 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9314
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009315 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009316
9317 const InputDeviceInfo::MotionRange* relRangeX =
9318 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9319 ASSERT_NE(relRangeX, nullptr);
9320 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9321 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9322 const InputDeviceInfo::MotionRange* relRangeY =
9323 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9324 ASSERT_NE(relRangeY, nullptr);
9325 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9326 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9327
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009328 // run captured pointer tests - note that this is unscaled, so input listener events should be
9329 // identical to what the hardware sends (accounting for any
9330 // calibration).
9331 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009332 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009333 processId(mapper, 1);
9334 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9335 processKey(mapper, BTN_TOUCH, 1);
9336 processSync(mapper);
9337
9338 // expect coord[0] to contain initial location of touch 0
9339 NotifyMotionArgs args;
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9342 ASSERT_EQ(1U, args.pointerCount);
9343 ASSERT_EQ(0, args.pointerProperties[0].id);
9344 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9345 ASSERT_NO_FATAL_FAILURE(
9346 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9347
9348 // FINGER 1 DOWN
9349 processSlot(mapper, 1);
9350 processId(mapper, 2);
9351 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9352 processSync(mapper);
9353
9354 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009356 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009357 ASSERT_EQ(2U, args.pointerCount);
9358 ASSERT_EQ(0, args.pointerProperties[0].id);
9359 ASSERT_EQ(1, args.pointerProperties[1].id);
9360 ASSERT_NO_FATAL_FAILURE(
9361 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9362 ASSERT_NO_FATAL_FAILURE(
9363 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9364
9365 // FINGER 1 MOVE
9366 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9367 processSync(mapper);
9368
9369 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9370 // from move
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9373 ASSERT_NO_FATAL_FAILURE(
9374 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9375 ASSERT_NO_FATAL_FAILURE(
9376 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9377
9378 // FINGER 0 MOVE
9379 processSlot(mapper, 0);
9380 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9381 processSync(mapper);
9382
9383 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9386 ASSERT_NO_FATAL_FAILURE(
9387 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9388 ASSERT_NO_FATAL_FAILURE(
9389 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9390
9391 // BUTTON DOWN
9392 processKey(mapper, BTN_LEFT, 1);
9393 processSync(mapper);
9394
9395 // touchinputmapper design sends a move before button press
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9400
9401 // BUTTON UP
9402 processKey(mapper, BTN_LEFT, 0);
9403 processSync(mapper);
9404
9405 // touchinputmapper design sends a move after button release
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9410
9411 // FINGER 0 UP
9412 processId(mapper, -1);
9413 processSync(mapper);
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9416
9417 // FINGER 1 MOVE
9418 processSlot(mapper, 1);
9419 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9420 processSync(mapper);
9421
9422 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9425 ASSERT_EQ(1U, args.pointerCount);
9426 ASSERT_EQ(1, args.pointerProperties[0].id);
9427 ASSERT_NO_FATAL_FAILURE(
9428 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9429
9430 // FINGER 1 UP
9431 processId(mapper, -1);
9432 processKey(mapper, BTN_TOUCH, 0);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9436
9437 // non captured touchpad should be a mouse source
9438 mFakePolicy->setPointerCapture(false);
9439 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9441 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9442}
9443
9444TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9445 std::shared_ptr<FakePointerController> fakePointerController =
9446 std::make_shared<FakePointerController>();
9447 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9448 fakePointerController->setPosition(0, 0);
9449 fakePointerController->setButtonState(0);
9450
9451 // prepare device and capture
9452 prepareDisplay(DISPLAY_ORIENTATION_0);
9453 prepareAxes(POSITION | ID | SLOT);
9454 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9455 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009456 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9458 // run uncaptured pointer tests - pushes out generic events
9459 // FINGER 0 DOWN
9460 processId(mapper, 3);
9461 processPosition(mapper, 100, 100);
9462 processKey(mapper, BTN_TOUCH, 1);
9463 processSync(mapper);
9464
9465 // start at (100,100), cursor should be at (0,0) * scale
9466 NotifyMotionArgs args;
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9469 ASSERT_NO_FATAL_FAILURE(
9470 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9471
9472 // FINGER 0 MOVE
9473 processPosition(mapper, 200, 200);
9474 processSync(mapper);
9475
9476 // compute scaling to help with touch position checking
9477 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9478 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9479 float scale =
9480 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9481
9482 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9486 0, 0, 0, 0, 0, 0, 0));
9487}
9488
9489TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9490 std::shared_ptr<FakePointerController> fakePointerController =
9491 std::make_shared<FakePointerController>();
9492
9493 prepareDisplay(DISPLAY_ORIENTATION_0);
9494 prepareAxes(POSITION | ID | SLOT);
9495 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009496 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009497 mFakePolicy->setPointerCapture(false);
9498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9499
9500 // uncaptured touchpad should be a pointer device
9501 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9502
9503 // captured touchpad should be a touchpad device
9504 mFakePolicy->setPointerCapture(true);
9505 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9506 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9507}
9508
HQ Liue6983c72022-04-19 22:14:56 +00009509class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9510protected:
9511 float mPointerMovementScale;
9512 float mPointerXZoomScale;
9513 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9514 addConfigurationProperty("touch.deviceType", "pointer");
9515 std::shared_ptr<FakePointerController> fakePointerController =
9516 std::make_shared<FakePointerController>();
9517 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9518 fakePointerController->setPosition(0, 0);
9519 fakePointerController->setButtonState(0);
9520 prepareDisplay(DISPLAY_ORIENTATION_0);
9521
9522 prepareAxes(POSITION);
9523 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9524 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9525 // needs to be disabled, and the pointer gesture needs to be enabled.
9526 mFakePolicy->setPointerCapture(false);
9527 mFakePolicy->setPointerGestureEnabled(true);
9528 mFakePolicy->setPointerController(fakePointerController);
9529
9530 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9531 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9532 mPointerMovementScale =
9533 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9534 mPointerXZoomScale =
9535 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9536 }
9537
9538 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9540 /*flat*/ 0,
9541 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9542 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9543 /*flat*/ 0,
9544 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9545 }
9546};
9547
9548/**
9549 * Two fingers down on a pointer mode touch pad. The width
9550 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9551 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9552 * be greater than the both value to be freeform gesture, so that after two
9553 * fingers start to move downwards, the gesture should be swipe.
9554 */
9555TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9556 // The min freeform gesture width is 25units/mm x 30mm = 750
9557 // which is greater than fraction of the diagnal length of the touchpad (349).
9558 // Thus, MaxSwipWidth is 750.
9559 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9561 NotifyMotionArgs motionArgs;
9562
9563 // Two fingers down at once.
9564 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9565 // Pointer's initial position is used the [0,0] coordinate.
9566 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9567
9568 processId(mapper, FIRST_TRACKING_ID);
9569 processPosition(mapper, x1, y1);
9570 processMTSync(mapper);
9571 processId(mapper, SECOND_TRACKING_ID);
9572 processPosition(mapper, x2, y2);
9573 processMTSync(mapper);
9574 processSync(mapper);
9575
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9577 ASSERT_EQ(1U, motionArgs.pointerCount);
9578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9580 ASSERT_NO_FATAL_FAILURE(
9581 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9582
9583 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9584 // that there should be 1 pointer.
9585 int32_t movingDistance = 200;
9586 y1 += movingDistance;
9587 y2 += movingDistance;
9588
9589 processId(mapper, FIRST_TRACKING_ID);
9590 processPosition(mapper, x1, y1);
9591 processMTSync(mapper);
9592 processId(mapper, SECOND_TRACKING_ID);
9593 processPosition(mapper, x2, y2);
9594 processMTSync(mapper);
9595 processSync(mapper);
9596
9597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9598 ASSERT_EQ(1U, motionArgs.pointerCount);
9599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9602 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9603 0, 0, 0, 0));
9604}
9605
9606/**
9607 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9608 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9609 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9610 * value to be freeform gesture, so that after two fingers start to move downwards,
9611 * the gesture should be swipe.
9612 */
9613TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9614 // The min freeform gesture width is 5units/mm x 30mm = 150
9615 // which is greater than fraction of the diagnal length of the touchpad (349).
9616 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9617 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9619 NotifyMotionArgs motionArgs;
9620
9621 // Two fingers down at once.
9622 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9623 // Pointer's initial position is used the [0,0] coordinate.
9624 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9625
9626 processId(mapper, FIRST_TRACKING_ID);
9627 processPosition(mapper, x1, y1);
9628 processMTSync(mapper);
9629 processId(mapper, SECOND_TRACKING_ID);
9630 processPosition(mapper, x2, y2);
9631 processMTSync(mapper);
9632 processSync(mapper);
9633
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9635 ASSERT_EQ(1U, motionArgs.pointerCount);
9636 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9638 ASSERT_NO_FATAL_FAILURE(
9639 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9640
9641 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9642 // and there should be 1 pointer.
9643 int32_t movingDistance = 200;
9644 y1 += movingDistance;
9645 y2 += movingDistance;
9646
9647 processId(mapper, FIRST_TRACKING_ID);
9648 processPosition(mapper, x1, y1);
9649 processMTSync(mapper);
9650 processId(mapper, SECOND_TRACKING_ID);
9651 processPosition(mapper, x2, y2);
9652 processMTSync(mapper);
9653 processSync(mapper);
9654
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9656 ASSERT_EQ(1U, motionArgs.pointerCount);
9657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9659 // New coordinate is the scaled relative coordinate from the initial coordinate.
9660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9661 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9662 0, 0, 0, 0));
9663}
9664
9665/**
9666 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9667 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9668 * freeform gestures after two fingers start to move downwards.
9669 */
9670TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9671 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9672 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9673
9674 NotifyMotionArgs motionArgs;
9675
9676 // Two fingers down at once. Wider than the max swipe width.
9677 // The gesture is expected to be PRESS, then transformed to FREEFORM
9678 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9679
9680 processId(mapper, FIRST_TRACKING_ID);
9681 processPosition(mapper, x1, y1);
9682 processMTSync(mapper);
9683 processId(mapper, SECOND_TRACKING_ID);
9684 processPosition(mapper, x2, y2);
9685 processMTSync(mapper);
9686 processSync(mapper);
9687
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9689 ASSERT_EQ(1U, motionArgs.pointerCount);
9690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9692 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9693 ASSERT_NO_FATAL_FAILURE(
9694 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9695
9696 int32_t movingDistance = 200;
9697
9698 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9699 // then two down events for two pointers.
9700 y1 += movingDistance;
9701 y2 += movingDistance;
9702
9703 processId(mapper, FIRST_TRACKING_ID);
9704 processPosition(mapper, x1, y1);
9705 processMTSync(mapper);
9706 processId(mapper, SECOND_TRACKING_ID);
9707 processPosition(mapper, x2, y2);
9708 processMTSync(mapper);
9709 processSync(mapper);
9710
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9712 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9713 ASSERT_EQ(1U, motionArgs.pointerCount);
9714 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, 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(1U, motionArgs.pointerCount);
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9721 ASSERT_EQ(2U, motionArgs.pointerCount);
9722 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9724 // Two pointers' scaled relative coordinates from their initial centroid.
9725 // Initial y coordinates are 0 as y1 and y2 have the same value.
9726 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9727 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9728 // When pointers move, the new coordinates equal to the initial coordinates plus
9729 // scaled moving distance.
9730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9731 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9732 0, 0, 0, 0));
9733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9734 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9735 0, 0, 0, 0));
9736
9737 // Move two fingers down again, expect one MOVE motion event.
9738 y1 += movingDistance;
9739 y2 += movingDistance;
9740
9741 processId(mapper, FIRST_TRACKING_ID);
9742 processPosition(mapper, x1, y1);
9743 processMTSync(mapper);
9744 processId(mapper, SECOND_TRACKING_ID);
9745 processPosition(mapper, x2, y2);
9746 processMTSync(mapper);
9747 processSync(mapper);
9748
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9750 ASSERT_EQ(2U, motionArgs.pointerCount);
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9754 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9755 0, 0, 0, 0, 0));
9756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9757 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9758 0, 0, 0, 0, 0));
9759}
9760
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009761// --- JoystickInputMapperTest ---
9762
9763class JoystickInputMapperTest : public InputMapperTest {
9764protected:
9765 static const int32_t RAW_X_MIN;
9766 static const int32_t RAW_X_MAX;
9767 static const int32_t RAW_Y_MIN;
9768 static const int32_t RAW_Y_MAX;
9769
9770 void SetUp() override {
9771 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9772 }
9773 void prepareAxes() {
9774 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9775 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9776 }
9777
9778 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9780 }
9781
9782 void processSync(JoystickInputMapper& mapper) {
9783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9784 }
9785
9786 void prepareVirtualDisplay(int32_t orientation) {
9787 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9788 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9789 NO_PORT, ViewportType::VIRTUAL);
9790 }
9791};
9792
9793const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9794const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9795const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9796const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9797
9798TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9799 prepareAxes();
9800 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9801
9802 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9803
9804 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9805
9806 // Send an axis event
9807 processAxis(mapper, ABS_X, 100);
9808 processSync(mapper);
9809
9810 NotifyMotionArgs args;
9811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9812 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9813
9814 // Send another axis event
9815 processAxis(mapper, ABS_Y, 100);
9816 processSync(mapper);
9817
9818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9819 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9820}
9821
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009822// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009823
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009824class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009825protected:
9826 static const char* DEVICE_NAME;
9827 static const char* DEVICE_LOCATION;
9828 static const int32_t DEVICE_ID;
9829 static const int32_t DEVICE_GENERATION;
9830 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009831 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009832 static const int32_t EVENTHUB_ID;
9833
9834 std::shared_ptr<FakeEventHub> mFakeEventHub;
9835 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009836 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009837 std::unique_ptr<InstrumentedInputReader> mReader;
9838 std::shared_ptr<InputDevice> mDevice;
9839
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009840 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009841 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009842 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009843 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009844 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009845 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009846 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9847 }
9848
9849 void SetUp() override { SetUp(DEVICE_CLASSES); }
9850
9851 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009852 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009853 mFakePolicy.clear();
9854 }
9855
9856 void configureDevice(uint32_t changes) {
9857 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9858 mReader->requestRefreshConfiguration(changes);
9859 mReader->loopOnce();
9860 }
9861 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9862 }
9863
9864 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9865 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009866 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009867 InputDeviceIdentifier identifier;
9868 identifier.name = name;
9869 identifier.location = location;
9870 std::shared_ptr<InputDevice> device =
9871 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9872 identifier);
9873 mReader->pushNextDevice(device);
9874 mFakeEventHub->addDevice(eventHubId, name, classes);
9875 mReader->loopOnce();
9876 return device;
9877 }
9878
9879 template <class T, typename... Args>
9880 T& addControllerAndConfigure(Args... args) {
9881 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9882
9883 return controller;
9884 }
9885};
9886
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009887const char* PeripheralControllerTest::DEVICE_NAME = "device";
9888const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9889const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9890const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9891const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009892const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9893 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009894const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009895
9896// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009897class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009898protected:
9899 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009900 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009901 }
9902};
9903
9904TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009905 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009906
9907 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9908 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9909}
9910
9911TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009912 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009913
9914 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9915 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9916}
9917
9918// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009919class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009920protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009921 void SetUp() override {
9922 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9923 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009924};
9925
Chris Ye85758332021-05-16 23:05:17 -07009926TEST_F(LightControllerTest, MonoLight) {
9927 RawLightInfo infoMono = {.id = 1,
9928 .name = "Mono",
9929 .maxBrightness = 255,
9930 .flags = InputLightClass::BRIGHTNESS,
9931 .path = ""};
9932 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009933
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009934 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009935 InputDeviceInfo info;
9936 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009937 std::vector<InputDeviceLightInfo> lights = info.getLights();
9938 ASSERT_EQ(1U, lights.size());
9939 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009940
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009941 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9942 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009943}
9944
9945TEST_F(LightControllerTest, RGBLight) {
9946 RawLightInfo infoRed = {.id = 1,
9947 .name = "red",
9948 .maxBrightness = 255,
9949 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9950 .path = ""};
9951 RawLightInfo infoGreen = {.id = 2,
9952 .name = "green",
9953 .maxBrightness = 255,
9954 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9955 .path = ""};
9956 RawLightInfo infoBlue = {.id = 3,
9957 .name = "blue",
9958 .maxBrightness = 255,
9959 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9960 .path = ""};
9961 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9962 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9963 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9964
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009965 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009966 InputDeviceInfo info;
9967 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009968 std::vector<InputDeviceLightInfo> lights = info.getLights();
9969 ASSERT_EQ(1U, lights.size());
9970 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009971
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009972 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9973 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009974}
9975
9976TEST_F(LightControllerTest, MultiColorRGBLight) {
9977 RawLightInfo infoColor = {.id = 1,
9978 .name = "red",
9979 .maxBrightness = 255,
9980 .flags = InputLightClass::BRIGHTNESS |
9981 InputLightClass::MULTI_INTENSITY |
9982 InputLightClass::MULTI_INDEX,
9983 .path = ""};
9984
9985 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9986
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009987 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009988 InputDeviceInfo info;
9989 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009990 std::vector<InputDeviceLightInfo> lights = info.getLights();
9991 ASSERT_EQ(1U, lights.size());
9992 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009993
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009994 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9995 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009996}
9997
9998TEST_F(LightControllerTest, PlayerIdLight) {
9999 RawLightInfo info1 = {.id = 1,
10000 .name = "player1",
10001 .maxBrightness = 255,
10002 .flags = InputLightClass::BRIGHTNESS,
10003 .path = ""};
10004 RawLightInfo info2 = {.id = 2,
10005 .name = "player2",
10006 .maxBrightness = 255,
10007 .flags = InputLightClass::BRIGHTNESS,
10008 .path = ""};
10009 RawLightInfo info3 = {.id = 3,
10010 .name = "player3",
10011 .maxBrightness = 255,
10012 .flags = InputLightClass::BRIGHTNESS,
10013 .path = ""};
10014 RawLightInfo info4 = {.id = 4,
10015 .name = "player4",
10016 .maxBrightness = 255,
10017 .flags = InputLightClass::BRIGHTNESS,
10018 .path = ""};
10019 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10020 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10021 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10022 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10023
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010024 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010025 InputDeviceInfo info;
10026 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010027 std::vector<InputDeviceLightInfo> lights = info.getLights();
10028 ASSERT_EQ(1U, lights.size());
10029 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010030
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010031 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10032 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10033 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010034}
10035
Michael Wrightd02c5b62014-02-10 15:10:22 -080010036} // namespace android