blob: 0fdffdfdef1b1e3033d22738128fb7627db98d4d [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
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000378 void setVelocityControlParams(const VelocityControlParameters& params) {
379 mConfig.pointerVelocityControlParameters = params;
380 mConfig.wheelVelocityControlParameters = params;
381 }
382
Michael Wrightd02c5b62014-02-10 15:10:22 -0800383private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000384 uint32_t mNextPointerCaptureSequenceNumber = 0;
385
Chris Yea52ade12020-08-27 16:49:20 -0700386 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 *outConfig = mConfig;
388 }
389
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000390 std::shared_ptr<PointerControllerInterface> obtainPointerController(
391 int32_t /*deviceId*/) override {
392 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800393 }
394
Chris Yea52ade12020-08-27 16:49:20 -0700395 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700396 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700398 mInputDevicesChanged = true;
399 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
403 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700404 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405 }
406
Chris Yea52ade12020-08-27 16:49:20 -0700407 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800408
409 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412
413 const bool devicesChanged =
414 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
415 return mInputDevicesChanged;
416 });
417 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
418 mInputDevicesChanged = false;
419 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800420};
421
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422// --- FakeEventHub ---
423
424class FakeEventHub : public EventHubInterface {
425 struct KeyInfo {
426 int32_t keyCode;
427 uint32_t flags;
428 };
429
Chris Yef59a2f42020-10-16 12:55:26 -0700430 struct SensorInfo {
431 InputDeviceSensorType sensorType;
432 int32_t sensorDataIndex;
433 };
434
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 struct Device {
436 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700437 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800438 PropertyMap configuration;
439 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
440 KeyedVector<int, bool> relativeAxes;
441 KeyedVector<int32_t, int32_t> keyCodeStates;
442 KeyedVector<int32_t, int32_t> scanCodeStates;
443 KeyedVector<int32_t, int32_t> switchStates;
444 KeyedVector<int32_t, int32_t> absoluteAxisValue;
445 KeyedVector<int32_t, KeyInfo> keysByScanCode;
446 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
447 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100448 // fake mapping which would normally come from keyCharacterMap
449 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700450 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
451 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800452 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700453 bool enabled;
454
455 status_t enable() {
456 enabled = true;
457 return OK;
458 }
459
460 status_t disable() {
461 enabled = false;
462 return OK;
463 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800464
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700465 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 };
467
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700468 std::mutex mLock;
469 std::condition_variable mEventsCondition;
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100472 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000473 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600474 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000475 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800476 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
477 // Simulates a device light brightness, from light id to light brightness.
478 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
479 // Simulates a device light intensities, from light id to light intensities map.
480 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
481 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700483public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 virtual ~FakeEventHub() {
485 for (size_t i = 0; i < mDevices.size(); i++) {
486 delete mDevices.valueAt(i);
487 }
488 }
489
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 FakeEventHub() { }
491
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700492 void addDevice(int32_t deviceId, const std::string& name,
493 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 Device* device = new Device(classes);
495 device->identifier.name = name;
496 mDevices.add(deviceId, device);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
501 void removeDevice(int32_t deviceId) {
502 delete mDevices.valueFor(deviceId);
503 mDevices.removeItem(deviceId);
504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000505 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 }
507
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700508 bool isDeviceEnabled(int32_t deviceId) {
509 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700510 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700511 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
512 return false;
513 }
514 return device->enabled;
515 }
516
517 status_t enableDevice(int32_t deviceId) {
518 status_t result;
519 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700520 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700521 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
522 return BAD_VALUE;
523 }
524 if (device->enabled) {
525 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
526 return OK;
527 }
528 result = device->enable();
529 return result;
530 }
531
532 status_t disableDevice(int32_t deviceId) {
533 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700534 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700535 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
536 return BAD_VALUE;
537 }
538 if (!device->enabled) {
539 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
540 return OK;
541 }
542 return device->disable();
543 }
544
Michael Wrightd02c5b62014-02-10 15:10:22 -0800545 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700549 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800550 Device* device = getDevice(deviceId);
551 device->configuration.addProperty(key, value);
552 }
553
554 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
555 Device* device = getDevice(deviceId);
556 device->configuration.addAll(configuration);
557 }
558
559 void addAbsoluteAxis(int32_t deviceId, int axis,
560 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
561 Device* device = getDevice(deviceId);
562
563 RawAbsoluteAxisInfo info;
564 info.valid = true;
565 info.minValue = minValue;
566 info.maxValue = maxValue;
567 info.flat = flat;
568 info.fuzz = fuzz;
569 info.resolution = resolution;
570 device->absoluteAxes.add(axis, info);
571 }
572
573 void addRelativeAxis(int32_t deviceId, int32_t axis) {
574 Device* device = getDevice(deviceId);
575 device->relativeAxes.add(axis, true);
576 }
577
578 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
579 Device* device = getDevice(deviceId);
580 device->keyCodeStates.replaceValueFor(keyCode, state);
581 }
582
583 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
584 Device* device = getDevice(deviceId);
585 device->scanCodeStates.replaceValueFor(scanCode, state);
586 }
587
588 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
589 Device* device = getDevice(deviceId);
590 device->switchStates.replaceValueFor(switchCode, state);
591 }
592
593 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
594 Device* device = getDevice(deviceId);
595 device->absoluteAxisValue.replaceValueFor(axis, value);
596 }
597
598 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
599 int32_t keyCode, uint32_t flags) {
600 Device* device = getDevice(deviceId);
601 KeyInfo info;
602 info.keyCode = keyCode;
603 info.flags = flags;
604 if (scanCode) {
605 device->keysByScanCode.add(scanCode, info);
606 }
607 if (usageCode) {
608 device->keysByUsageCode.add(usageCode, info);
609 }
610 }
611
Philip Junker4af3b3d2021-12-14 10:36:55 +0100612 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
613 Device* device = getDevice(deviceId);
614 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
615 }
616
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 void addLed(int32_t deviceId, int32_t led, bool initialState) {
618 Device* device = getDevice(deviceId);
619 device->leds.add(led, initialState);
620 }
621
Chris Yef59a2f42020-10-16 12:55:26 -0700622 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
623 int32_t sensorDataIndex) {
624 Device* device = getDevice(deviceId);
625 SensorInfo info;
626 info.sensorType = sensorType;
627 info.sensorDataIndex = sensorDataIndex;
628 device->sensorsByAbsCode.emplace(absCode, info);
629 }
630
631 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
632 Device* device = getDevice(deviceId);
633 typename BitArray<MSC_MAX>::Buffer buffer;
634 buffer[mscEvent / 32] = 1 << mscEvent % 32;
635 device->mscBitmask.loadFromBuffer(buffer);
636 }
637
Chris Ye3fdbfef2021-01-06 18:45:18 -0800638 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
639 mRawLightInfos.emplace(rawId, std::move(info));
640 }
641
642 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
643 mLightBrightness.emplace(rawId, brightness);
644 }
645
646 void fakeLightIntensities(int32_t rawId,
647 const std::unordered_map<LightColor, int32_t> intensities) {
648 mLightIntensities.emplace(rawId, std::move(intensities));
649 }
650
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 bool getLedState(int32_t deviceId, int32_t led) {
652 Device* device = getDevice(deviceId);
653 return device->leds.valueFor(led);
654 }
655
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100656 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mExcludedDevices;
658 }
659
660 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
661 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800662 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 }
664
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000665 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
666 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700667 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 RawEvent event;
669 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000670 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 event.deviceId = deviceId;
672 event.type = type;
673 event.code = code;
674 event.value = value;
675 mEvents.push_back(event);
676
677 if (type == EV_ABS) {
678 setAbsoluteAxisValue(deviceId, code, value);
679 }
680 }
681
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600682 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
683 std::vector<TouchVideoFrame>> videoFrames) {
684 mVideoFrames = std::move(videoFrames);
685 }
686
Michael Wrightd02c5b62014-02-10 15:10:22 -0800687 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700688 std::unique_lock<std::mutex> lock(mLock);
689 base::ScopedLockAssertion assumeLocked(mLock);
690 const bool queueIsEmpty =
691 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
692 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
693 if (!queueIsEmpty) {
694 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
695 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
698private:
699 Device* getDevice(int32_t deviceId) const {
700 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100701 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702 }
703
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700704 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700706 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 return device ? device->identifier : InputDeviceIdentifier();
712 }
713
Chris Yea52ade12020-08-27 16:49:20 -0700714 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
Chris Yea52ade12020-08-27 16:49:20 -0700716 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 Device* device = getDevice(deviceId);
718 if (device) {
719 *outConfiguration = device->configuration;
720 }
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
724 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800725 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800726 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800727 ssize_t index = device->absoluteAxes.indexOfKey(axis);
728 if (index >= 0) {
729 *outAxisInfo = device->absoluteAxes.valueAt(index);
730 return OK;
731 }
732 }
733 outAxisInfo->clear();
734 return -1;
735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 if (device) {
740 return device->relativeAxes.indexOfKey(axis) >= 0;
741 }
742 return false;
743 }
744
Chris Yea52ade12020-08-27 16:49:20 -0700745 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800746
Chris Yef59a2f42020-10-16 12:55:26 -0700747 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
748 Device* device = getDevice(deviceId);
749 if (device) {
750 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
751 }
752 return false;
753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
756 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
758 if (device) {
759 const KeyInfo* key = getKey(device, scanCode, usageCode);
760 if (key) {
761 if (outKeycode) {
762 *outKeycode = key->keyCode;
763 }
764 if (outFlags) {
765 *outFlags = key->flags;
766 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700767 if (outMetaState) {
768 *outMetaState = metaState;
769 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800770 return OK;
771 }
772 }
773 return NAME_NOT_FOUND;
774 }
775
776 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
777 if (usageCode) {
778 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
779 if (index >= 0) {
780 return &device->keysByUsageCode.valueAt(index);
781 }
782 }
783 if (scanCode) {
784 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
785 if (index >= 0) {
786 return &device->keysByScanCode.valueAt(index);
787 }
788 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700789 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 }
791
Chris Yea52ade12020-08-27 16:49:20 -0700792 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800793
Chris Yef59a2f42020-10-16 12:55:26 -0700794 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
795 int32_t absCode) {
796 Device* device = getDevice(deviceId);
797 if (!device) {
798 return Errorf("Sensor device not found.");
799 }
800 auto it = device->sensorsByAbsCode.find(absCode);
801 if (it == device->sensorsByAbsCode.end()) {
802 return Errorf("Sensor map not found.");
803 }
804 const SensorInfo& info = it->second;
805 return std::make_pair(info.sensorType, info.sensorDataIndex);
806 }
807
Chris Yea52ade12020-08-27 16:49:20 -0700808 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 mExcludedDevices = devices;
810 }
811
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000812 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
813 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000815 const size_t filledSize = std::min(mEvents.size(), bufferSize);
816 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
817
818 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700819 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000820 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821 }
822
Chris Yea52ade12020-08-27 16:49:20 -0700823 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600824 auto it = mVideoFrames.find(deviceId);
825 if (it != mVideoFrames.end()) {
826 std::vector<TouchVideoFrame> frames = std::move(it->second);
827 mVideoFrames.erase(deviceId);
828 return frames;
829 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800830 return {};
831 }
832
Chris Yea52ade12020-08-27 16:49:20 -0700833 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800834 Device* device = getDevice(deviceId);
835 if (device) {
836 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
837 if (index >= 0) {
838 return device->scanCodeStates.valueAt(index);
839 }
840 }
841 return AKEY_STATE_UNKNOWN;
842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845 Device* device = getDevice(deviceId);
846 if (device) {
847 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
848 if (index >= 0) {
849 return device->keyCodeStates.valueAt(index);
850 }
851 }
852 return AKEY_STATE_UNKNOWN;
853 }
854
Chris Yea52ade12020-08-27 16:49:20 -0700855 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856 Device* device = getDevice(deviceId);
857 if (device) {
858 ssize_t index = device->switchStates.indexOfKey(sw);
859 if (index >= 0) {
860 return device->switchStates.valueAt(index);
861 }
862 }
863 return AKEY_STATE_UNKNOWN;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
867 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868 Device* device = getDevice(deviceId);
869 if (device) {
870 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
871 if (index >= 0) {
872 *outValue = device->absoluteAxisValue.valueAt(index);
873 return OK;
874 }
875 }
876 *outValue = 0;
877 return -1;
878 }
879
Philip Junker4af3b3d2021-12-14 10:36:55 +0100880 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
881 Device* device = getDevice(deviceId);
882 if (!device) {
883 return AKEYCODE_UNKNOWN;
884 }
885 auto it = device->keyCodeMapping.find(locationKeyCode);
886 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700890 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700891 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 bool result = false;
893 Device* device = getDevice(deviceId);
894 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700895 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700896 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800897 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
898 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
899 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901 }
902 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
903 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
904 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 }
906 }
907 }
908 }
909 return result;
910 }
911
Chris Yea52ade12020-08-27 16:49:20 -0700912 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800913 Device* device = getDevice(deviceId);
914 if (device) {
915 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
916 return index >= 0;
917 }
918 return false;
919 }
920
Arthur Hungcb40a002021-08-03 14:31:01 +0000921 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
922 Device* device = getDevice(deviceId);
923 if (!device) {
924 return false;
925 }
926 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
927 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
928 return true;
929 }
930 }
931 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
932 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
933 return true;
934 }
935 }
936 return false;
937 }
938
Chris Yea52ade12020-08-27 16:49:20 -0700939 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 Device* device = getDevice(deviceId);
941 return device && device->leds.indexOfKey(led) >= 0;
942 }
943
Chris Yea52ade12020-08-27 16:49:20 -0700944 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 Device* device = getDevice(deviceId);
946 if (device) {
947 ssize_t index = device->leds.indexOfKey(led);
948 if (index >= 0) {
949 device->leds.replaceValueAt(led, on);
950 } else {
951 ADD_FAILURE()
952 << "Attempted to set the state of an LED that the EventHub declared "
953 "was not present. led=" << led;
954 }
955 }
956 }
957
Chris Yea52ade12020-08-27 16:49:20 -0700958 void getVirtualKeyDefinitions(
959 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 outVirtualKeys.clear();
961
962 Device* device = getDevice(deviceId);
963 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800964 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965 }
966 }
967
Chris Yea52ade12020-08-27 16:49:20 -0700968 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700969 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973 return false;
974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977
Chris Yea52ade12020-08-27 16:49:20 -0700978 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800979
Chris Ye87143712020-11-10 05:05:58 +0000980 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
981
Chris Yee2b1e5c2021-03-10 22:45:12 -0800982 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
983 return BATTERY_CAPACITY;
984 }
Kim Low03ea0352020-11-06 12:45:07 -0800985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
987 return BATTERY_STATUS;
988 }
989
990 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
991
992 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
993 return std::nullopt;
994 }
Kim Low03ea0352020-11-06 12:45:07 -0800995
Chris Ye3fdbfef2021-01-06 18:45:18 -0800996 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
997 std::vector<int32_t> ids;
998 for (const auto& [rawId, info] : mRawLightInfos) {
999 ids.push_back(rawId);
1000 }
1001 return ids;
1002 }
1003
1004 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1005 auto it = mRawLightInfos.find(lightId);
1006 if (it == mRawLightInfos.end()) {
1007 return std::nullopt;
1008 }
1009 return it->second;
1010 }
1011
1012 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1013 mLightBrightness.emplace(lightId, brightness);
1014 }
1015
1016 void setLightIntensities(int32_t deviceId, int32_t lightId,
1017 std::unordered_map<LightColor, int32_t> intensities) override {
1018 mLightIntensities.emplace(lightId, intensities);
1019 };
1020
1021 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1022 auto lightIt = mLightBrightness.find(lightId);
1023 if (lightIt == mLightBrightness.end()) {
1024 return std::nullopt;
1025 }
1026 return lightIt->second;
1027 }
1028
1029 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1030 int32_t deviceId, int32_t lightId) override {
1031 auto lightIt = mLightIntensities.find(lightId);
1032 if (lightIt == mLightIntensities.end()) {
1033 return std::nullopt;
1034 }
1035 return lightIt->second;
1036 };
1037
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001038 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039 return false;
1040 }
1041
Chris Yea52ade12020-08-27 16:49:20 -07001042 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043
Chris Yea52ade12020-08-27 16:49:20 -07001044 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049};
1050
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051// --- FakeInputMapper ---
1052
1053class FakeInputMapper : public InputMapper {
1054 uint32_t mSources;
1055 int32_t mKeyboardType;
1056 int32_t mMetaState;
1057 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1058 KeyedVector<int32_t, int32_t> mScanCodeStates;
1059 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001060 // fake mapping which would normally come from keyCharacterMap
1061 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001062 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001064 std::mutex mLock;
1065 std::condition_variable mStateChangedCondition;
1066 bool mConfigureWasCalled GUARDED_BY(mLock);
1067 bool mResetWasCalled GUARDED_BY(mLock);
1068 bool mProcessWasCalled GUARDED_BY(mLock);
1069 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070
Arthur Hungc23540e2018-11-29 20:42:11 +08001071 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001072public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001073 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1074 : InputMapper(deviceContext),
1075 mSources(sources),
1076 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001078 mConfigureWasCalled(false),
1079 mResetWasCalled(false),
1080 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081
Chris Yea52ade12020-08-27 16:49:20 -07001082 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001083
1084 void setKeyboardType(int32_t keyboardType) {
1085 mKeyboardType = keyboardType;
1086 }
1087
1088 void setMetaState(int32_t metaState) {
1089 mMetaState = metaState;
1090 }
1091
1092 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001093 std::unique_lock<std::mutex> lock(mLock);
1094 base::ScopedLockAssertion assumeLocked(mLock);
1095 const bool configureCalled =
1096 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1097 return mConfigureWasCalled;
1098 });
1099 if (!configureCalled) {
1100 FAIL() << "Expected configure() to have been called.";
1101 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102 mConfigureWasCalled = false;
1103 }
1104
1105 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001106 std::unique_lock<std::mutex> lock(mLock);
1107 base::ScopedLockAssertion assumeLocked(mLock);
1108 const bool resetCalled =
1109 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1110 return mResetWasCalled;
1111 });
1112 if (!resetCalled) {
1113 FAIL() << "Expected reset() to have been called.";
1114 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001115 mResetWasCalled = false;
1116 }
1117
Yi Kong9b14ac62018-07-17 13:48:38 -07001118 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001119 std::unique_lock<std::mutex> lock(mLock);
1120 base::ScopedLockAssertion assumeLocked(mLock);
1121 const bool processCalled =
1122 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1123 return mProcessWasCalled;
1124 });
1125 if (!processCalled) {
1126 FAIL() << "Expected process() to have been called.";
1127 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001128 if (outLastEvent) {
1129 *outLastEvent = mLastEvent;
1130 }
1131 mProcessWasCalled = false;
1132 }
1133
1134 void setKeyCodeState(int32_t keyCode, int32_t state) {
1135 mKeyCodeStates.replaceValueFor(keyCode, state);
1136 }
1137
1138 void setScanCodeState(int32_t scanCode, int32_t state) {
1139 mScanCodeStates.replaceValueFor(scanCode, state);
1140 }
1141
1142 void setSwitchState(int32_t switchCode, int32_t state) {
1143 mSwitchStates.replaceValueFor(switchCode, state);
1144 }
1145
1146 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001147 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148 }
1149
Philip Junker4af3b3d2021-12-14 10:36:55 +01001150 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1151 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1152 }
1153
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001155 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001156
Chris Yea52ade12020-08-27 16:49:20 -07001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158 InputMapper::populateDeviceInfo(deviceInfo);
1159
1160 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1161 deviceInfo->setKeyboardType(mKeyboardType);
1162 }
1163 }
1164
Chris Yea52ade12020-08-27 16:49:20 -07001165 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001166 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001168
1169 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001170 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001171 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1172 mViewport = config->getDisplayViewportByPort(*displayPort);
1173 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174
1175 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Chris Yea52ade12020-08-27 16:49:20 -07001178 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001179 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 }
1183
Chris Yea52ade12020-08-27 16:49:20 -07001184 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 mLastEvent = *rawEvent;
1187 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001188 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001189 }
1190
Chris Yea52ade12020-08-27 16:49:20 -07001191 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001192 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1193 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1194 }
1195
Philip Junker4af3b3d2021-12-14 10:36:55 +01001196 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1197 auto it = mKeyCodeMapping.find(locationKeyCode);
1198 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1199 }
1200
Chris Yea52ade12020-08-27 16:49:20 -07001201 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001202 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1203 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1204 }
1205
Chris Yea52ade12020-08-27 16:49:20 -07001206 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1208 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1209 }
1210
Chris Yea52ade12020-08-27 16:49:20 -07001211 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001212 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001213 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001214 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1216 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1217 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001218 }
1219 }
1220 }
Chris Yea52ade12020-08-27 16:49:20 -07001221 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 return result;
1223 }
1224
1225 virtual int32_t getMetaState() {
1226 return mMetaState;
1227 }
1228
1229 virtual void fadePointer() {
1230 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001231
1232 virtual std::optional<int32_t> getAssociatedDisplay() {
1233 if (mViewport) {
1234 return std::make_optional(mViewport->displayId);
1235 }
1236 return std::nullopt;
1237 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238};
1239
1240
1241// --- InstrumentedInputReader ---
1242
1243class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001244 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245
1246public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001247 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1248 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001249 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001250 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001252 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001254 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001256 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001257 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 InputDeviceIdentifier identifier;
1259 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001260 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001262 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 }
1264
Prabir Pradhan28efc192019-11-05 01:10:04 +00001265 // Make the protected loopOnce method accessible to tests.
1266 using InputReader::loopOnce;
1267
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001269 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1270 const InputDeviceIdentifier& identifier)
1271 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001272 if (!mNextDevices.empty()) {
1273 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1274 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001275 return device;
1276 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001277 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001278 }
1279
arthurhungdcef2dc2020-08-11 14:47:50 +08001280 // --- FakeInputReaderContext ---
1281 class FakeInputReaderContext : public ContextImpl {
1282 int32_t mGlobalMetaState;
1283 bool mUpdateGlobalMetaStateWasCalled;
1284 int32_t mGeneration;
1285
1286 public:
1287 FakeInputReaderContext(InputReader* reader)
1288 : ContextImpl(reader),
1289 mGlobalMetaState(0),
1290 mUpdateGlobalMetaStateWasCalled(false),
1291 mGeneration(1) {}
1292
1293 virtual ~FakeInputReaderContext() {}
1294
1295 void assertUpdateGlobalMetaStateWasCalled() {
1296 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1297 << "Expected updateGlobalMetaState() to have been called.";
1298 mUpdateGlobalMetaStateWasCalled = false;
1299 }
1300
1301 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1302
1303 uint32_t getGeneration() { return mGeneration; }
1304
1305 void updateGlobalMetaState() override {
1306 mUpdateGlobalMetaStateWasCalled = true;
1307 ContextImpl::updateGlobalMetaState();
1308 }
1309
1310 int32_t getGlobalMetaState() override {
1311 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1312 }
1313
1314 int32_t bumpGeneration() override {
1315 mGeneration = ContextImpl::bumpGeneration();
1316 return mGeneration;
1317 }
1318 } mFakeContext;
1319
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001321
1322public:
1323 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324};
1325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326// --- InputReaderPolicyTest ---
1327class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001329 sp<FakeInputReaderPolicy> mFakePolicy;
1330
Chris Yea52ade12020-08-27 16:49:20 -07001331 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1332 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333};
1334
1335/**
1336 * Check that empty set of viewports is an acceptable configuration.
1337 * Also try to get internal viewport two different ways - by type and by uniqueId.
1338 *
1339 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1340 * Such configuration is not currently allowed.
1341 */
1342TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001343 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001344
1345 // We didn't add any viewports yet, so there shouldn't be any.
1346 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348 ASSERT_FALSE(internalViewport);
1349
1350 // Add an internal viewport, then clear it
1351 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001352 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001353 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001354
1355 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001358 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359
1360 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001361 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001363 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364
1365 mFakePolicy->clearViewports();
1366 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001369 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001370 ASSERT_FALSE(internalViewport);
1371}
1372
1373TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1374 const std::string internalUniqueId = "local:0";
1375 const std::string externalUniqueId = "local:1";
1376 const std::string virtualUniqueId1 = "virtual:2";
1377 const std::string virtualUniqueId2 = "virtual:3";
1378 constexpr int32_t virtualDisplayId1 = 2;
1379 constexpr int32_t virtualDisplayId2 = 3;
1380
1381 // Add an internal viewport
1382 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001383 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1384 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001385 // Add an external viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1388 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an virtual viewport
1390 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1392 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add another virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397
1398 // Check matching by type for internal
1399 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001400 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(internalViewport);
1402 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1403
1404 // Check matching by type for external
1405 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001406 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(externalViewport);
1408 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1409
1410 // Check matching by uniqueId for virtual viewport #1
1411 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001412 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1416 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1417
1418 // Check matching by uniqueId for virtual viewport #2
1419 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001420 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1424 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1425}
1426
1427
1428/**
1429 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1430 * that lookup works by checking display id.
1431 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1432 */
1433TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1434 const std::string uniqueId1 = "uniqueId1";
1435 const std::string uniqueId2 = "uniqueId2";
1436 constexpr int32_t displayId1 = 2;
1437 constexpr int32_t displayId2 = 3;
1438
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1440 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 for (const ViewportType& type : types) {
1442 mFakePolicy->clearViewports();
1443 // Add a viewport
1444 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001445 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1446 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001447 // Add another viewport
1448 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451
1452 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 std::optional<DisplayViewport> viewport1 =
1454 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_TRUE(viewport1);
1456 ASSERT_EQ(displayId1, viewport1->displayId);
1457 ASSERT_EQ(type, viewport1->type);
1458
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 std::optional<DisplayViewport> viewport2 =
1460 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001461 ASSERT_TRUE(viewport2);
1462 ASSERT_EQ(displayId2, viewport2->displayId);
1463 ASSERT_EQ(type, viewport2->type);
1464
1465 // When there are multiple viewports of the same kind, and uniqueId is not specified
1466 // in the call to getDisplayViewport, then that situation is not supported.
1467 // The viewports can be stored in any order, so we cannot rely on the order, since that
1468 // is just implementation detail.
1469 // However, we can check that it still returns *a* viewport, we just cannot assert
1470 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001471 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001472 ASSERT_TRUE(someViewport);
1473 }
1474}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001476/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001477 * When we have multiple internal displays make sure we always return the default display when
1478 * querying by type.
1479 */
1480TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1481 const std::string uniqueId1 = "uniqueId1";
1482 const std::string uniqueId2 = "uniqueId2";
1483 constexpr int32_t nonDefaultDisplayId = 2;
1484 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1485 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1486
1487 // Add the default display first and ensure it gets returned.
1488 mFakePolicy->clearViewports();
1489 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001490 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001491 ViewportType::INTERNAL);
1492 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001493 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001494 ViewportType::INTERNAL);
1495
1496 std::optional<DisplayViewport> viewport =
1497 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1498 ASSERT_TRUE(viewport);
1499 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1500 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1501
1502 // Add the default display second to make sure order doesn't matter.
1503 mFakePolicy->clearViewports();
1504 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001505 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001506 ViewportType::INTERNAL);
1507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001508 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001509 ViewportType::INTERNAL);
1510
1511 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1512 ASSERT_TRUE(viewport);
1513 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1514 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1515}
1516
1517/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001518 * Check getDisplayViewportByPort
1519 */
1520TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001521 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 const std::string uniqueId1 = "uniqueId1";
1523 const std::string uniqueId2 = "uniqueId2";
1524 constexpr int32_t displayId1 = 1;
1525 constexpr int32_t displayId2 = 2;
1526 const uint8_t hdmi1 = 0;
1527 const uint8_t hdmi2 = 1;
1528 const uint8_t hdmi3 = 2;
1529
1530 mFakePolicy->clearViewports();
1531 // Add a viewport that's associated with some display port that's not of interest.
1532 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1534 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001535 // Add another viewport, connected to HDMI1 port
1536 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539
1540 // Check that correct display viewport was returned by comparing the display ports.
1541 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1542 ASSERT_TRUE(hdmi1Viewport);
1543 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1544 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1545
1546 // Check that we can still get the same viewport using the uniqueId
1547 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1548 ASSERT_TRUE(hdmi1Viewport);
1549 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1550 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1551 ASSERT_EQ(type, hdmi1Viewport->type);
1552
1553 // Check that we cannot find a port with "HDMI2", because we never added one
1554 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1555 ASSERT_FALSE(hdmi2Viewport);
1556}
1557
Michael Wrightd02c5b62014-02-10 15:10:22 -08001558// --- InputReaderTest ---
1559
1560class InputReaderTest : public testing::Test {
1561protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001562 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001564 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001565 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001566
Chris Yea52ade12020-08-27 16:49:20 -07001567 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001569 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001570 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571
Prabir Pradhan28efc192019-11-05 01:10:04 +00001572 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001573 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001574 }
1575
Chris Yea52ade12020-08-27 16:49:20 -07001576 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 }
1580
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001581 void addDevice(int32_t eventHubId, const std::string& name,
1582 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001583 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001584
1585 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001586 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 }
1588 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001589 mReader->loopOnce();
1590 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001591 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1592 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 }
1594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001595 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001597 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001598 }
1599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001600 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001601 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001602 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001603 }
1604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001606 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001607 ftl::Flags<InputDeviceClass> classes,
1608 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001609 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001610 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1611 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001612 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001613 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614 return mapper;
1615 }
1616};
1617
Chris Ye98d3f532020-10-01 21:48:59 -07001618TEST_F(InputReaderTest, PolicyGetInputDevices) {
1619 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001620 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001621 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622
1623 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001624 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001627 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001628 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1629 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001630 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001631}
1632
Chris Yee7310032020-09-22 15:36:28 -07001633TEST_F(InputReaderTest, GetMergedInputDevices) {
1634 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1635 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1636 // Add two subdevices to device
1637 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1638 // Must add at least one mapper or the device will be ignored!
1639 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1640 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1641
1642 // Push same device instance for next device to be added, so they'll have same identifier.
1643 mReader->pushNextDevice(device);
1644 mReader->pushNextDevice(device);
1645 ASSERT_NO_FATAL_FAILURE(
1646 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1647 ASSERT_NO_FATAL_FAILURE(
1648 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1649
1650 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001651 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001652}
1653
Chris Yee14523a2020-12-19 13:46:00 -08001654TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1655 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1656 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1657 // Add two subdevices to device
1658 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1659 // Must add at least one mapper or the device will be ignored!
1660 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1661 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1662
1663 // Push same device instance for next device to be added, so they'll have same identifier.
1664 mReader->pushNextDevice(device);
1665 mReader->pushNextDevice(device);
1666 // Sensor device is initially disabled
1667 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1668 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1669 nullptr));
1670 // Device is disabled because the only sub device is a sensor device and disabled initially.
1671 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1672 ASSERT_FALSE(device->isEnabled());
1673 ASSERT_NO_FATAL_FAILURE(
1674 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1675 // The merged device is enabled if any sub device is enabled
1676 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1677 ASSERT_TRUE(device->isEnabled());
1678}
1679
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001680TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001682 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t eventHubId = 1;
1684 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001685 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001686 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001687 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001688 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689
Yi Kong9b14ac62018-07-17 13:48:38 -07001690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
1692 NotifyDeviceResetArgs resetArgs;
1693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695
1696 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001699
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701 ASSERT_EQ(deviceId, resetArgs.deviceId);
1702 ASSERT_EQ(device->isEnabled(), false);
1703
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001704 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001705 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001708 ASSERT_EQ(device->isEnabled(), false);
1709
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001710 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001711 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001713 ASSERT_EQ(deviceId, resetArgs.deviceId);
1714 ASSERT_EQ(device->isEnabled(), true);
1715}
1716
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001719 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001723 AINPUT_SOURCE_KEYBOARD, nullptr);
1724 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001725
1726 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1727 AINPUT_SOURCE_ANY, AKEYCODE_A))
1728 << "Should return unknown when the device id is >= 0 but unknown.";
1729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001730 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1731 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1732 << "Should return unknown when the device id is valid but the sources are not "
1733 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001735 ASSERT_EQ(AKEY_STATE_DOWN,
1736 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1737 AKEYCODE_A))
1738 << "Should return value provided by mapper when device id is valid and the device "
1739 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001740
1741 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1742 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1743 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1744
1745 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1748}
1749
Philip Junker4af3b3d2021-12-14 10:36:55 +01001750TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1752 constexpr int32_t eventHubId = 1;
1753 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1754 InputDeviceClass::KEYBOARD,
1755 AINPUT_SOURCE_KEYBOARD, nullptr);
1756 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1757
1758 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1759 << "Should return unknown when the device with the specified id is not found.";
1760
1761 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1762 << "Should return correct mapping when device id is valid and mapping exists.";
1763
1764 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1765 << "Should return the location key code when device id is valid and there's no "
1766 "mapping.";
1767}
1768
1769TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1770 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1771 constexpr int32_t eventHubId = 1;
1772 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1773 InputDeviceClass::JOYSTICK,
1774 AINPUT_SOURCE_GAMEPAD, nullptr);
1775 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1776
1777 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1778 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1779}
1780
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001783 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
1788 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001789
1790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1791 AINPUT_SOURCE_ANY, KEY_A))
1792 << "Should return unknown when the device id is >= 0 but unknown.";
1793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001794 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1795 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1796 << "Should return unknown when the device id is valid but the sources are not "
1797 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001799 ASSERT_EQ(AKEY_STATE_DOWN,
1800 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1801 KEY_A))
1802 << "Should return value provided by mapper when device id is valid and the device "
1803 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001804
1805 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1806 AINPUT_SOURCE_TRACKBALL, KEY_A))
1807 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1808
1809 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1812}
1813
1814TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001816 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001817 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001818 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001820 AINPUT_SOURCE_KEYBOARD, nullptr);
1821 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001822
1823 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1824 AINPUT_SOURCE_ANY, SW_LID))
1825 << "Should return unknown when the device id is >= 0 but unknown.";
1826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1828 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1829 << "Should return unknown when the device id is valid but the sources are not "
1830 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001832 ASSERT_EQ(AKEY_STATE_DOWN,
1833 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1834 SW_LID))
1835 << "Should return value provided by mapper when device id is valid and the device "
1836 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001837
1838 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1839 AINPUT_SOURCE_TRACKBALL, SW_LID))
1840 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1841
1842 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1845}
1846
1847TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001848 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001849 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001850 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001851 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001853 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 mapper.addSupportedKeyCode(AKEYCODE_A);
1856 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001857
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001858 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859 uint8_t flags[4] = { 0, 0, 0, 1 };
1860
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001861 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 << "Should return false when device id is >= 0 but unknown.";
1863 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1864
1865 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001866 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001867 << "Should return false when device id is valid but the sources are not supported by "
1868 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1870
1871 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001872 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001873 keyCodes, flags))
1874 << "Should return value provided by mapper when device id is valid and the device "
1875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001876 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1877
1878 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001879 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1880 << "Should return false when the device id is < 0 but the sources are not supported by "
1881 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001882 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1883
1884 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001885 ASSERT_TRUE(
1886 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1887 << "Should return value provided by mapper when device id is < 0 and one of the "
1888 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1890}
1891
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001892TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001894 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001895
1896 NotifyConfigurationChangedArgs args;
1897
1898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1899 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1900}
1901
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001902TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001904 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001905 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001907 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001908 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001910 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001912 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001913 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1915
1916 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001917 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001918 ASSERT_EQ(when, event.when);
1919 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001920 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001921 ASSERT_EQ(EV_KEY, event.type);
1922 ASSERT_EQ(KEY_A, event.code);
1923 ASSERT_EQ(1, event.value);
1924}
1925
Garfield Tan1c7bc862020-01-28 13:24:04 -08001926TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001927 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001928 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001929 constexpr int32_t eventHubId = 1;
1930 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001931 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001932 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001933 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935
1936 NotifyDeviceResetArgs resetArgs;
1937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001938 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001939
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001940 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001941 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001943 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001944 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001945
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001946 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001947 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001949 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001950 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001952 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001953 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001955 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001956 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001957}
1958
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1960 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001961 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001962 constexpr int32_t eventHubId = 1;
1963 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1964 // Must add at least one mapper or the device will be ignored!
1965 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001966 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001967 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1968
1969 NotifyDeviceResetArgs resetArgs;
1970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1971 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1972}
1973
Arthur Hungc23540e2018-11-29 20:42:11 +08001974TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001975 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001976 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001977 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001978 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1980 FakeInputMapper& mapper =
1981 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001982 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001983
1984 const uint8_t hdmi1 = 1;
1985
1986 // Associated touch screen with second display.
1987 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1988
1989 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001990 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001991 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001992 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001993 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001994 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001995 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001996 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001997 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001998 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001999
2000 // Add the device, and make sure all of the callbacks are triggered.
2001 // The device is added after the input port associations are processed since
2002 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002003 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002006 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002007
Arthur Hung2c9a3342019-07-23 14:18:59 +08002008 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002009 ASSERT_EQ(deviceId, device->getId());
2010 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2011 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002012
2013 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002014 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002015 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002016 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002017}
2018
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2020 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002021 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002022 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2023 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2024 // Must add at least one mapper or the device will be ignored!
2025 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2026 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2027 mReader->pushNextDevice(device);
2028 mReader->pushNextDevice(device);
2029 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2030 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2031
2032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2033
2034 NotifyDeviceResetArgs resetArgs;
2035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2036 ASSERT_EQ(deviceId, resetArgs.deviceId);
2037 ASSERT_TRUE(device->isEnabled());
2038 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2039 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2040
2041 disableDevice(deviceId);
2042 mReader->loopOnce();
2043
2044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2045 ASSERT_EQ(deviceId, resetArgs.deviceId);
2046 ASSERT_FALSE(device->isEnabled());
2047 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2048 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2049
2050 enableDevice(deviceId);
2051 mReader->loopOnce();
2052
2053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2054 ASSERT_EQ(deviceId, resetArgs.deviceId);
2055 ASSERT_TRUE(device->isEnabled());
2056 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2058}
2059
2060TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2061 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002062 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002063 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2064 // Add two subdevices to device
2065 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2066 FakeInputMapper& mapperDevice1 =
2067 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2068 FakeInputMapper& mapperDevice2 =
2069 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2070 mReader->pushNextDevice(device);
2071 mReader->pushNextDevice(device);
2072 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2073 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2074
2075 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2076 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2077
2078 ASSERT_EQ(AKEY_STATE_DOWN,
2079 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2080 ASSERT_EQ(AKEY_STATE_DOWN,
2081 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2082 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2083 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2084}
2085
Prabir Pradhan7e186182020-11-10 13:56:45 -08002086TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2087 NotifyPointerCaptureChangedArgs args;
2088
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002089 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2091 mReader->loopOnce();
2092 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002093 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2094 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002095
2096 mFakePolicy->setPointerCapture(false);
2097 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2098 mReader->loopOnce();
2099 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002100 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002101
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002102 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002103 // does not change.
2104 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2105 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107}
2108
Chris Ye87143712020-11-10 05:05:58 +00002109class FakeVibratorInputMapper : public FakeInputMapper {
2110public:
2111 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2112 : FakeInputMapper(deviceContext, sources) {}
2113
2114 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2115};
2116
2117TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2118 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002119 ftl::Flags<InputDeviceClass> deviceClass =
2120 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002121 constexpr int32_t eventHubId = 1;
2122 const char* DEVICE_LOCATION = "BLUETOOTH";
2123 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2124 FakeVibratorInputMapper& mapper =
2125 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2126 mReader->pushNextDevice(device);
2127
2128 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2129 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2130
2131 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2132 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2133}
2134
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002135// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002136
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002137class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002138public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002139 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002140
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002141 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002142
2143 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2144
2145 void dump(std::string& dump) override {}
2146
2147 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2148 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002149 }
2150
Chris Yee2b1e5c2021-03-10 22:45:12 -08002151 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2152 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002153 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002154
2155 bool setLightColor(int32_t lightId, int32_t color) override {
2156 getDeviceContext().setLightBrightness(lightId, color >> 24);
2157 return true;
2158 }
2159
2160 std::optional<int32_t> getLightColor(int32_t lightId) override {
2161 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2162 if (!result.has_value()) {
2163 return std::nullopt;
2164 }
2165 return result.value() << 24;
2166 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002167
2168 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2169
2170 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2171
2172private:
2173 InputDeviceContext& mDeviceContext;
2174 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2175 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002176};
2177
Chris Yee2b1e5c2021-03-10 22:45:12 -08002178TEST_F(InputReaderTest, BatteryGetCapacity) {
2179 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002180 ftl::Flags<InputDeviceClass> deviceClass =
2181 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182 constexpr int32_t eventHubId = 1;
2183 const char* DEVICE_LOCATION = "BLUETOOTH";
2184 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002185 FakePeripheralController& controller =
2186 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002187 mReader->pushNextDevice(device);
2188
2189 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2190
2191 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2192 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2193}
2194
2195TEST_F(InputReaderTest, BatteryGetStatus) {
2196 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002197 ftl::Flags<InputDeviceClass> deviceClass =
2198 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002199 constexpr int32_t eventHubId = 1;
2200 const char* DEVICE_LOCATION = "BLUETOOTH";
2201 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002202 FakePeripheralController& controller =
2203 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002204 mReader->pushNextDevice(device);
2205
2206 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2207
2208 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2209 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2210}
2211
Chris Ye3fdbfef2021-01-06 18:45:18 -08002212TEST_F(InputReaderTest, LightGetColor) {
2213 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002214 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002215 constexpr int32_t eventHubId = 1;
2216 const char* DEVICE_LOCATION = "BLUETOOTH";
2217 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002218 FakePeripheralController& controller =
2219 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002220 mReader->pushNextDevice(device);
2221 RawLightInfo info = {.id = 1,
2222 .name = "Mono",
2223 .maxBrightness = 255,
2224 .flags = InputLightClass::BRIGHTNESS,
2225 .path = ""};
2226 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2227 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2228
2229 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2232 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002233 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2234 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2235}
2236
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002237// --- InputReaderIntegrationTest ---
2238
2239// These tests create and interact with the InputReader only through its interface.
2240// The InputReader is started during SetUp(), which starts its processing in its own
2241// thread. The tests use linux uinput to emulate input devices.
2242// NOTE: Interacting with the physical device while these tests are running may cause
2243// the tests to fail.
2244class InputReaderIntegrationTest : public testing::Test {
2245protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002246 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002247 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002248 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002249
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002250 std::shared_ptr<FakePointerController> mFakePointerController;
2251
Chris Yea52ade12020-08-27 16:49:20 -07002252 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002254 mFakePointerController = std::make_shared<FakePointerController>();
2255 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002256 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2257 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002259 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2260 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002261 ASSERT_EQ(mReader->start(), OK);
2262
2263 // Since this test is run on a real device, all the input devices connected
2264 // to the test device will show up in mReader. We wait for those input devices to
2265 // show up before beginning the tests.
2266 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2267 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2268 }
2269
Chris Yea52ade12020-08-27 16:49:20 -07002270 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002271 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002272 mReader.reset();
2273 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002274 mFakePolicy.clear();
2275 }
2276};
2277
2278TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2279 // An invalid input device that is only used for this test.
2280 class InvalidUinputDevice : public UinputDevice {
2281 public:
2282 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2283
2284 private:
2285 void configureDevice(int fd, uinput_user_dev* device) override {}
2286 };
2287
2288 const size_t numDevices = mFakePolicy->getInputDevices().size();
2289
2290 // UinputDevice does not set any event or key bits, so InputReader should not
2291 // consider it as a valid device.
2292 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2295 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2296
2297 invalidDevice.reset();
2298 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2300 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2301}
2302
2303TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2304 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2305
2306 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2307 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2308 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2309 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2310
2311 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002312 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002313 const auto& it =
2314 std::find_if(inputDevices.begin(), inputDevices.end(),
2315 [&keyboard](const InputDeviceInfo& info) {
2316 return info.getIdentifier().name == keyboard->getName();
2317 });
2318
2319 ASSERT_NE(it, inputDevices.end());
2320 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2321 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2322 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002323
2324 keyboard.reset();
2325 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2326 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2327 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2328}
2329
2330TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2331 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2332 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2333
2334 NotifyConfigurationChangedArgs configChangedArgs;
2335 ASSERT_NO_FATAL_FAILURE(
2336 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002337 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002338 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2339
2340 NotifyKeyArgs keyArgs;
2341 keyboard->pressAndReleaseHomeKey();
2342 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2343 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002344 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002345 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002346 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002347 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002348 prevTimestamp = keyArgs.eventTime;
2349
2350 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2351 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002352 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002353 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002354 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002355}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002356
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002357/**
2358 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2359 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2360 * are passed to the listener.
2361 */
2362static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2363TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2364 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2366 NotifyKeyArgs keyArgs;
2367
2368 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2371 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2372
2373 controller->pressAndReleaseKey(BTN_GEAR_UP);
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2376 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2377}
2378
Arthur Hungaab25622020-01-16 11:22:11 +08002379// --- TouchProcessTest ---
2380class TouchIntegrationTest : public InputReaderIntegrationTest {
2381protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002382 const std::string UNIQUE_ID = "local:0";
2383
Chris Yea52ade12020-08-27 16:49:20 -07002384 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002385 InputReaderIntegrationTest::SetUp();
2386 // At least add an internal display.
2387 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2388 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002389 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002390
2391 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2394 }
2395
2396 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2397 int32_t orientation, const std::string& uniqueId,
2398 std::optional<uint8_t> physicalPort,
2399 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002400 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2401 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002402 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2403 }
2404
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002405 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2406 NotifyMotionArgs args;
2407 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2408 EXPECT_EQ(action, args.action);
2409 ASSERT_EQ(points.size(), args.pointerCount);
2410 for (size_t i = 0; i < args.pointerCount; i++) {
2411 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2412 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2413 }
2414 }
2415
Arthur Hungaab25622020-01-16 11:22:11 +08002416 std::unique_ptr<UinputTouchScreen> mDevice;
2417};
2418
2419TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2420 NotifyMotionArgs args;
2421 const Point centerPoint = mDevice->getCenterPoint();
2422
2423 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002424 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002425 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002426 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2429
2430 // ACTION_MOVE
2431 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002432 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2435
2436 // ACTION_UP
2437 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002438 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2440 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2441}
2442
2443TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2444 NotifyMotionArgs args;
2445 const Point centerPoint = mDevice->getCenterPoint();
2446
2447 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002448 mDevice->sendSlot(FIRST_SLOT);
2449 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002450 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2454
2455 // ACTION_POINTER_DOWN (Second slot)
2456 const Point secondPoint = centerPoint + Point(100, 100);
2457 mDevice->sendSlot(SECOND_SLOT);
2458 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002459 mDevice->sendDown(secondPoint);
2460 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002462 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002463
2464 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002465 mDevice->sendMove(secondPoint + Point(1, 1));
2466 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2469
2470 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002471 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002472 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002474 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002475
2476 // ACTION_UP
2477 mDevice->sendSlot(FIRST_SLOT);
2478 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002479 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2482}
2483
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002484/**
2485 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2486 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2487 * data?
2488 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2489 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2490 * for Pointer 0 only is generated after.
2491 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2492 * events, we will not miss any information.
2493 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2494 * event generated afterwards that contains the newest movement of pointer 0.
2495 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2496 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2497 * losing information about non-palm pointers.
2498 */
2499TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2500 NotifyMotionArgs args;
2501 const Point centerPoint = mDevice->getCenterPoint();
2502
2503 // ACTION_DOWN
2504 mDevice->sendSlot(FIRST_SLOT);
2505 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2506 mDevice->sendDown(centerPoint);
2507 mDevice->sendSync();
2508 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2509
2510 // ACTION_POINTER_DOWN (Second slot)
2511 const Point secondPoint = centerPoint + Point(100, 100);
2512 mDevice->sendSlot(SECOND_SLOT);
2513 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2514 mDevice->sendDown(secondPoint);
2515 mDevice->sendSync();
2516 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2517
2518 // ACTION_MOVE (First slot)
2519 mDevice->sendSlot(FIRST_SLOT);
2520 mDevice->sendMove(centerPoint + Point(5, 5));
2521 // ACTION_POINTER_UP (Second slot)
2522 mDevice->sendSlot(SECOND_SLOT);
2523 mDevice->sendPointerUp();
2524 // Send a single sync for the above 2 pointer updates
2525 mDevice->sendSync();
2526
2527 // First, we should get POINTER_UP for the second pointer
2528 assertReceivedMotion(ACTION_POINTER_1_UP,
2529 {/*first pointer */ centerPoint + Point(5, 5),
2530 /*second pointer*/ secondPoint});
2531
2532 // Next, the MOVE event for the first pointer
2533 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2534}
2535
2536/**
2537 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2538 * move, and then it will go up, all in the same frame.
2539 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2540 * gets sent to the listener.
2541 */
2542TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2543 NotifyMotionArgs args;
2544 const Point centerPoint = mDevice->getCenterPoint();
2545
2546 // ACTION_DOWN
2547 mDevice->sendSlot(FIRST_SLOT);
2548 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2549 mDevice->sendDown(centerPoint);
2550 mDevice->sendSync();
2551 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2552
2553 // ACTION_POINTER_DOWN (Second slot)
2554 const Point secondPoint = centerPoint + Point(100, 100);
2555 mDevice->sendSlot(SECOND_SLOT);
2556 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2557 mDevice->sendDown(secondPoint);
2558 mDevice->sendSync();
2559 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2560
2561 // ACTION_MOVE (First slot)
2562 mDevice->sendSlot(FIRST_SLOT);
2563 mDevice->sendMove(centerPoint + Point(5, 5));
2564 // ACTION_POINTER_UP (Second slot)
2565 mDevice->sendSlot(SECOND_SLOT);
2566 mDevice->sendMove(secondPoint + Point(6, 6));
2567 mDevice->sendPointerUp();
2568 // Send a single sync for the above 2 pointer updates
2569 mDevice->sendSync();
2570
2571 // First, we should get POINTER_UP for the second pointer
2572 // The movement of the second pointer during the liftoff frame is ignored.
2573 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2574 assertReceivedMotion(ACTION_POINTER_1_UP,
2575 {/*first pointer */ centerPoint + Point(5, 5),
2576 /*second pointer*/ secondPoint});
2577
2578 // Next, the MOVE event for the first pointer
2579 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2580}
2581
Arthur Hungaab25622020-01-16 11:22:11 +08002582TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2583 NotifyMotionArgs args;
2584 const Point centerPoint = mDevice->getCenterPoint();
2585
2586 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002587 mDevice->sendSlot(FIRST_SLOT);
2588 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002589 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002590 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002591 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2592 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2593
arthurhungcc7f9802020-04-30 17:55:40 +08002594 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002595 const Point secondPoint = centerPoint + Point(100, 100);
2596 mDevice->sendSlot(SECOND_SLOT);
2597 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2598 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002599 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002600 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002601 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002602
arthurhungcc7f9802020-04-30 17:55:40 +08002603 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002604 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002605 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2608
arthurhungcc7f9802020-04-30 17:55:40 +08002609 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2610 // a palm event.
2611 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002612 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002613 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002614 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002615 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002616 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002617
arthurhungcc7f9802020-04-30 17:55:40 +08002618 // Send up to second slot, expect first slot send moving.
2619 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002623
arthurhungcc7f9802020-04-30 17:55:40 +08002624 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002625 mDevice->sendSlot(FIRST_SLOT);
2626 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628
arthurhungcc7f9802020-04-30 17:55:40 +08002629 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002631}
2632
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634class InputDeviceTest : public testing::Test {
2635protected:
2636 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002637 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 static const int32_t DEVICE_ID;
2639 static const int32_t DEVICE_GENERATION;
2640 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002641 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002642 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002644 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002646 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002647 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002648 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
Chris Yea52ade12020-08-27 16:49:20 -07002650 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002651 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002653 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002654 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002655 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 InputDeviceIdentifier identifier;
2657 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002658 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002659 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002660 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002661 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002662 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 }
2665
Chris Yea52ade12020-08-27 16:49:20 -07002666 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002667 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669 }
2670};
2671
2672const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002673const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002674const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2676const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002677const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002678 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680
2681TEST_F(InputDeviceTest, ImmutableProperties) {
2682 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002683 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002684 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685}
2686
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002687TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2688 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002689}
2690
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2692 // Configuration.
2693 InputReaderConfiguration config;
2694 mDevice->configure(ARBITRARY_TIME, &config, 0);
2695
2696 // Reset.
2697 mDevice->reset(ARBITRARY_TIME);
2698
2699 NotifyDeviceResetArgs resetArgs;
2700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2701 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2702 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2703
2704 // Metadata.
2705 ASSERT_TRUE(mDevice->isIgnored());
2706 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2707
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002708 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002710 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2712 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2713
2714 // State queries.
2715 ASSERT_EQ(0, mDevice->getMetaState());
2716
2717 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2718 << "Ignored device should return unknown key code state.";
2719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2720 << "Ignored device should return unknown scan code state.";
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown switch state.";
2723
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002724 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002726 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002727 << "Ignored device should never mark any key codes.";
2728 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2729 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2730}
2731
2732TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2733 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002736 FakeInputMapper& mapper1 =
2737 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002738 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2739 mapper1.setMetaState(AMETA_ALT_ON);
2740 mapper1.addSupportedKeyCode(AKEYCODE_A);
2741 mapper1.addSupportedKeyCode(AKEYCODE_B);
2742 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2743 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2744 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2745 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2746 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002748 FakeInputMapper& mapper2 =
2749 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
2752 InputReaderConfiguration config;
2753 mDevice->configure(ARBITRARY_TIME, &config, 0);
2754
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002755 std::string propertyValue;
2756 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002757 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002758 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002759
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002760 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2761 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002762
2763 // Reset
2764 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002765 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2766 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767
2768 NotifyDeviceResetArgs resetArgs;
2769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2770 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2771 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2772
2773 // Metadata.
2774 ASSERT_FALSE(mDevice->isIgnored());
2775 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2776
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002777 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002778 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002779 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002780 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2781 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2782
2783 // State queries.
2784 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2785 << "Should query mappers and combine meta states.";
2786
2787 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2788 << "Should return unknown key code state when source not supported.";
2789 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2790 << "Should return unknown scan code state when source not supported.";
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown switch state when source not supported.";
2793
2794 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2795 << "Should query mapper when source is supported.";
2796 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2797 << "Should query mapper when source is supported.";
2798 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2799 << "Should query mapper when source is supported.";
2800
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002801 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002803 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 << "Should do nothing when source is unsupported.";
2805 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2806 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2807 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2808 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2809
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002810 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811 << "Should query mapper when source is supported.";
2812 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2813 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2814 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2815 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2816
2817 // Event handling.
2818 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002819 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002820 mDevice->process(&event, 1);
2821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2823 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824}
2825
Arthur Hung2c9a3342019-07-23 14:18:59 +08002826// A single input device is associated with a specific display. Check that:
2827// 1. Device is disabled if the viewport corresponding to the associated display is not found
2828// 2. Device is disabled when setEnabled API is called
2829TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002830 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002831
2832 // First Configuration.
2833 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2834
2835 // Device should be enabled by default.
2836 ASSERT_TRUE(mDevice->isEnabled());
2837
2838 // Prepare associated info.
2839 constexpr uint8_t hdmi = 1;
2840 const std::string UNIQUE_ID = "local:1";
2841
2842 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2843 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2844 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2845 // Device should be disabled because it is associated with a specific display via
2846 // input port <-> display port association, but the corresponding display is not found
2847 ASSERT_FALSE(mDevice->isEnabled());
2848
2849 // Prepare displays.
2850 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002851 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2852 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2854 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2855 ASSERT_TRUE(mDevice->isEnabled());
2856
2857 // Device should be disabled after set disable.
2858 mFakePolicy->addDisabledDevice(mDevice->getId());
2859 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2860 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2861 ASSERT_FALSE(mDevice->isEnabled());
2862
2863 // Device should still be disabled even found the associated display.
2864 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2865 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2866 ASSERT_FALSE(mDevice->isEnabled());
2867}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002868
Christine Franks1ba71cc2021-04-07 14:37:42 -07002869TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2870 // Device should be enabled by default.
2871 mFakePolicy->clearViewports();
2872 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2873 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2874 ASSERT_TRUE(mDevice->isEnabled());
2875
2876 // Device should be disabled because it is associated with a specific display, but the
2877 // corresponding display is not found.
2878 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002879 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002880 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2881 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2882 ASSERT_FALSE(mDevice->isEnabled());
2883
2884 // Device should be enabled when a display is found.
2885 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2886 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2887 NO_PORT, ViewportType::INTERNAL);
2888 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2889 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2890 ASSERT_TRUE(mDevice->isEnabled());
2891
2892 // Device should be disabled after set disable.
2893 mFakePolicy->addDisabledDevice(mDevice->getId());
2894 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2895 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2896 ASSERT_FALSE(mDevice->isEnabled());
2897
2898 // Device should still be disabled even found the associated display.
2899 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2900 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2901 ASSERT_FALSE(mDevice->isEnabled());
2902}
2903
Christine Franks2a2293c2022-01-18 11:51:16 -08002904TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2905 mFakePolicy->clearViewports();
2906 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2908
2909 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2910 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2911 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2912 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2913 NO_PORT, ViewportType::INTERNAL);
2914 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2915 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2916 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2917}
2918
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919// --- InputMapperTest ---
2920
2921class InputMapperTest : public testing::Test {
2922protected:
2923 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002924 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 static const int32_t DEVICE_ID;
2926 static const int32_t DEVICE_GENERATION;
2927 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002928 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002929 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002931 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002932 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002933 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002934 std::unique_ptr<InstrumentedInputReader> mReader;
2935 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002937 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002938 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002940 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002941 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002942 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002943 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 }
2945
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002946 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002947 SetUp(DEVICE_CLASSES);
2948 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002949
Chris Yea52ade12020-08-27 16:49:20 -07002950 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002951 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002952 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002953 }
2954
2955 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002956 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 }
2958
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002959 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002960 if (!changes ||
2961 (changes &
2962 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2963 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002964 mReader->requestRefreshConfiguration(changes);
2965 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002966 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002967 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2968 }
2969
arthurhungdcef2dc2020-08-11 14:47:50 +08002970 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2971 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002972 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002973 InputDeviceIdentifier identifier;
2974 identifier.name = name;
2975 identifier.location = location;
2976 std::shared_ptr<InputDevice> device =
2977 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2978 identifier);
2979 mReader->pushNextDevice(device);
2980 mFakeEventHub->addDevice(eventHubId, name, classes);
2981 mReader->loopOnce();
2982 return device;
2983 }
2984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002985 template <class T, typename... Args>
2986 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002987 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002988 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002990 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002991 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 }
2993
2994 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002995 int32_t orientation, const std::string& uniqueId,
2996 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002997 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2998 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002999 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3000 }
3001
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003002 void clearViewports() {
3003 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004 }
3005
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003006 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3007 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 RawEvent event;
3009 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003011 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 event.type = type;
3013 event.code = code;
3014 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003015 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003016 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017 }
3018
3019 static void assertMotionRange(const InputDeviceInfo& info,
3020 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3021 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003022 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3024 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3025 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3026 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3027 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3029 }
3030
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003031 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3032 float size, float touchMajor, float touchMinor, float toolMajor,
3033 float toolMinor, float orientation, float distance,
3034 float scaledAxisEpsilon = 1.f) {
3035 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3036 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3038 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003039 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3040 scaledAxisEpsilon);
3041 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3042 scaledAxisEpsilon);
3043 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3046 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3048 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3049 }
3050
Michael Wright17db18e2020-06-26 20:51:44 +01003051 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003053 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003054 ASSERT_NEAR(x, actualX, 1);
3055 ASSERT_NEAR(y, actualY, 1);
3056 }
3057};
3058
3059const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003060const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003061const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3063const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003064const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3065 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003066const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
3068// --- SwitchInputMapperTest ---
3069
3070class SwitchInputMapperTest : public InputMapperTest {
3071protected:
3072};
3073
3074TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003077 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078}
3079
3080TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003083 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003086 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088}
3089
3090TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003097
3098 NotifySwitchArgs args;
3099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3100 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003101 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3102 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 args.switchMask);
3104 ASSERT_EQ(uint32_t(0), args.policyFlags);
3105}
3106
Chris Ye87143712020-11-10 05:05:58 +00003107// --- VibratorInputMapperTest ---
3108class VibratorInputMapperTest : public InputMapperTest {
3109protected:
3110 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3111};
3112
3113TEST_F(VibratorInputMapperTest, GetSources) {
3114 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3115
3116 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3117}
3118
3119TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3120 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3121
3122 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3123}
3124
3125TEST_F(VibratorInputMapperTest, Vibrate) {
3126 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003127 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003128 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3129
3130 VibrationElement pattern(2);
3131 VibrationSequence sequence(2);
3132 pattern.duration = std::chrono::milliseconds(200);
3133 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3134 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3135 sequence.addElement(pattern);
3136 pattern.duration = std::chrono::milliseconds(500);
3137 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3138 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3139 sequence.addElement(pattern);
3140
3141 std::vector<int64_t> timings = {0, 1};
3142 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3143
3144 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003145 // Start vibrating
3146 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003147 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003148 // Verify vibrator state listener was notified.
3149 mReader->loopOnce();
3150 NotifyVibratorStateArgs args;
3151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3152 ASSERT_EQ(DEVICE_ID, args.deviceId);
3153 ASSERT_TRUE(args.isOn);
3154 // Stop vibrating
3155 mapper.cancelVibrate(VIBRATION_TOKEN);
3156 ASSERT_FALSE(mapper.isVibrating());
3157 // Verify vibrator state listener was notified.
3158 mReader->loopOnce();
3159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003162}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163
Chris Yef59a2f42020-10-16 12:55:26 -07003164// --- SensorInputMapperTest ---
3165
3166class SensorInputMapperTest : public InputMapperTest {
3167protected:
3168 static const int32_t ACCEL_RAW_MIN;
3169 static const int32_t ACCEL_RAW_MAX;
3170 static const int32_t ACCEL_RAW_FUZZ;
3171 static const int32_t ACCEL_RAW_FLAT;
3172 static const int32_t ACCEL_RAW_RESOLUTION;
3173
3174 static const int32_t GYRO_RAW_MIN;
3175 static const int32_t GYRO_RAW_MAX;
3176 static const int32_t GYRO_RAW_FUZZ;
3177 static const int32_t GYRO_RAW_FLAT;
3178 static const int32_t GYRO_RAW_RESOLUTION;
3179
3180 static const float GRAVITY_MS2_UNIT;
3181 static const float DEGREE_RADIAN_UNIT;
3182
3183 void prepareAccelAxes();
3184 void prepareGyroAxes();
3185 void setAccelProperties();
3186 void setGyroProperties();
3187 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3188};
3189
3190const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3191const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3192const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3193const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3194const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3195
3196const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3197const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3198const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3199const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3200const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3201
3202const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3203const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3204
3205void SensorInputMapperTest::prepareAccelAxes() {
3206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3207 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3209 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212}
3213
3214void SensorInputMapperTest::prepareGyroAxes() {
3215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3216 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3218 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221}
3222
3223void SensorInputMapperTest::setAccelProperties() {
3224 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3225 /* sensorDataIndex */ 0);
3226 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3227 /* sensorDataIndex */ 1);
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 2);
3230 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3231 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3232 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3233 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3234 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3235}
3236
3237void SensorInputMapperTest::setGyroProperties() {
3238 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3239 /* sensorDataIndex */ 0);
3240 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3241 /* sensorDataIndex */ 1);
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 2);
3244 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3245 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3246 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3247 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3248 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3249}
3250
3251TEST_F(SensorInputMapperTest, GetSources) {
3252 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3253
3254 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3255}
3256
3257TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3258 setAccelProperties();
3259 prepareAccelAxes();
3260 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3261
3262 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3263 std::chrono::microseconds(10000),
3264 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003265 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003271
3272 NotifySensorArgs args;
3273 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3274 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3275 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3276
3277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3278 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3279 ASSERT_EQ(args.deviceId, DEVICE_ID);
3280 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3281 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3282 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3283 ASSERT_EQ(args.values, values);
3284 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3285}
3286
3287TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3288 setGyroProperties();
3289 prepareGyroAxes();
3290 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3291
3292 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3293 std::chrono::microseconds(10000),
3294 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003295 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003301
3302 NotifySensorArgs args;
3303 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3304 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3305 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3306
3307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3308 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3309 ASSERT_EQ(args.deviceId, DEVICE_ID);
3310 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3311 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3312 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3313 ASSERT_EQ(args.values, values);
3314 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3315}
3316
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317// --- KeyboardInputMapperTest ---
3318
3319class KeyboardInputMapperTest : public InputMapperTest {
3320protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003321 const std::string UNIQUE_ID = "local:0";
3322
3323 void prepareDisplay(int32_t orientation);
3324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326 int32_t originalKeyCode, int32_t rotatedKeyCode,
3327 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328};
3329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3331 * orientation.
3332 */
3333void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003334 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3335 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003336}
3337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 int32_t originalScanCode, int32_t originalKeyCode,
3340 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 NotifyKeyArgs args;
3342
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3345 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3346 ASSERT_EQ(originalScanCode, args.scanCode);
3347 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003348 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3353 ASSERT_EQ(originalScanCode, args.scanCode);
3354 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003355 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356}
3357
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 KeyboardInputMapper& mapper =
3360 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3361 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
3366TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3367 const int32_t USAGE_A = 0x070004;
3368 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003371 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3372 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 KeyboardInputMapper& mapper =
3376 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3377 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003378 // Initial metastate is AMETA_NONE.
3379 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380
3381 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 NotifyKeyArgs args;
3384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3385 ASSERT_EQ(DEVICE_ID, args.deviceId);
3386 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3387 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3388 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3389 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3390 ASSERT_EQ(KEY_HOME, args.scanCode);
3391 ASSERT_EQ(AMETA_NONE, args.metaState);
3392 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3393 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3394 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3395
3396 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3399 ASSERT_EQ(DEVICE_ID, args.deviceId);
3400 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3401 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3402 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3403 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3404 ASSERT_EQ(KEY_HOME, args.scanCode);
3405 ASSERT_EQ(AMETA_NONE, args.metaState);
3406 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3407 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3408 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3409
3410 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3414 ASSERT_EQ(DEVICE_ID, args.deviceId);
3415 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3416 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3417 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3418 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3419 ASSERT_EQ(0, args.scanCode);
3420 ASSERT_EQ(AMETA_NONE, args.metaState);
3421 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3422 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3423 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3424
3425 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3427 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3429 ASSERT_EQ(DEVICE_ID, args.deviceId);
3430 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3431 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3432 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3433 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3434 ASSERT_EQ(0, args.scanCode);
3435 ASSERT_EQ(AMETA_NONE, args.metaState);
3436 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3437 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3438 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3439
3440 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3444 ASSERT_EQ(DEVICE_ID, args.deviceId);
3445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3446 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3447 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3448 ASSERT_EQ(0, args.keyCode);
3449 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3450 ASSERT_EQ(AMETA_NONE, args.metaState);
3451 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3452 ASSERT_EQ(0U, args.policyFlags);
3453 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3454
3455 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3457 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(DEVICE_ID, args.deviceId);
3460 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3461 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3463 ASSERT_EQ(0, args.keyCode);
3464 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3465 ASSERT_EQ(AMETA_NONE, args.metaState);
3466 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3467 ASSERT_EQ(0U, args.policyFlags);
3468 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3469}
3470
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003471/**
3472 * Ensure that the readTime is set to the time when the EV_KEY is received.
3473 */
3474TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3476
3477 KeyboardInputMapper& mapper =
3478 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3480 NotifyKeyArgs args;
3481
3482 // Key down
3483 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3485 ASSERT_EQ(12, args.readTime);
3486
3487 // Key up
3488 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3490 ASSERT_EQ(15, args.readTime);
3491}
3492
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003496 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3498 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 KeyboardInputMapper& mapper =
3501 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Arthur Hung95f68612022-04-07 14:08:22 +08003504 // Initial metastate is AMETA_NONE.
3505 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 NotifyKeyArgs args;
3510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003513 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3518 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
3521 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003522 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526
3527 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003528 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3530 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003531 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003532 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533}
3534
3535TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003541 KeyboardInputMapper& mapper =
3542 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3543 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003545 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3547 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3548 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3549 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3554}
3555
3556TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3560 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003563 KeyboardInputMapper& mapper =
3564 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003567 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003568 ASSERT_NO_FATAL_FAILURE(
3569 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3571 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3573 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3575 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003577 clearViewports();
3578 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579 ASSERT_NO_FATAL_FAILURE(
3580 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3582 AKEYCODE_DPAD_UP, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3584 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3586 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003588 clearViewports();
3589 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590 ASSERT_NO_FATAL_FAILURE(
3591 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3593 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3594 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3595 AKEYCODE_DPAD_UP, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3597 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
3600 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003601 ASSERT_NO_FATAL_FAILURE(
3602 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3604 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3606 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3608 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609
3610 // Special case: if orientation changes while key is down, we still emit the same keycode
3611 // in the key up as we did in the key down.
3612 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003613 clearViewports();
3614 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3617 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3618 ASSERT_EQ(KEY_UP, args.scanCode);
3619 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3620
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003621 clearViewports();
3622 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3625 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3626 ASSERT_EQ(KEY_UP, args.scanCode);
3627 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3628}
3629
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3631 // If the keyboard is not orientation aware,
3632 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 KeyboardInputMapper& mapper =
3636 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3637 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638 NotifyKeyArgs args;
3639
3640 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3645 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3646
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003647 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3652 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3653}
3654
3655TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3656 // If the keyboard is orientation aware,
3657 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003659
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003661 KeyboardInputMapper& mapper =
3662 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3663 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 NotifyKeyArgs args;
3665
3666 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3667 // ^--- already checked by the previous test
3668
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003669 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003670 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3675 ASSERT_EQ(DISPLAY_ID, args.displayId);
3676
3677 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003678 clearViewports();
3679 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003680 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3685 ASSERT_EQ(newDisplayId, args.displayId);
3686}
3687
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003689 KeyboardInputMapper& mapper =
3690 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3691 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003693 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003696 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698}
3699
Philip Junker4af3b3d2021-12-14 10:36:55 +01003700TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3701 KeyboardInputMapper& mapper =
3702 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3703 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3704
3705 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3706 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3707 << "If a mapping is available, the result is equal to the mapping";
3708
3709 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3710 << "If no mapping is available, the result is the key location";
3711}
3712
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003714 KeyboardInputMapper& mapper =
3715 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3716 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723}
3724
3725TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 KeyboardInputMapper& mapper =
3727 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3728 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003733 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_TRUE(flags[0]);
3735 ASSERT_FALSE(flags[1]);
3736}
3737
3738TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3740 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3741 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 KeyboardInputMapper& mapper =
3747 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3748 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003749 // Initial metastate is AMETA_NONE.
3750 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
3752 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003753 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3754 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
3757 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003760 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3761 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3769 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
3789 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3794 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804}
3805
Chris Yea52ade12020-08-27 16:49:20 -07003806TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3808 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3809 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3810 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3811
3812 KeyboardInputMapper& mapper =
3813 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3814 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3815
Chris Yea52ade12020-08-27 16:49:20 -07003816 // Meta state should be AMETA_NONE after reset
3817 mapper.reset(ARBITRARY_TIME);
3818 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3819 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3820 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3822
3823 NotifyKeyArgs args;
3824 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3827 ASSERT_EQ(AMETA_NONE, args.metaState);
3828 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3830 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3831
3832 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(AMETA_NONE, args.metaState);
3836 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3837 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3838 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3839}
3840
Arthur Hung2c9a3342019-07-23 14:18:59 +08003841TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3842 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003843 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3844 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3845 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003847
3848 // keyboard 2.
3849 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003850 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003851 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003852 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003853 std::shared_ptr<InputDevice> device2 =
3854 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003855 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003856
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003857 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3858 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3859 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3860 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003861
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003862 KeyboardInputMapper& mapper =
3863 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3864 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003867 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003868 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3870 device2->reset(ARBITRARY_TIME);
3871
3872 // Prepared displays and associated info.
3873 constexpr uint8_t hdmi1 = 0;
3874 constexpr uint8_t hdmi2 = 1;
3875 const std::string SECONDARY_UNIQUE_ID = "local:1";
3876
3877 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3878 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3879
3880 // No associated display viewport found, should disable the device.
3881 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3883 ASSERT_FALSE(device2->isEnabled());
3884
3885 // Prepare second display.
3886 constexpr int32_t newDisplayId = 2;
3887 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003888 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003889 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003890 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003891 // Default device will reconfigure above, need additional reconfiguration for another device.
3892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3894
3895 // Device should be enabled after the associated display is found.
3896 ASSERT_TRUE(mDevice->isEnabled());
3897 ASSERT_TRUE(device2->isEnabled());
3898
3899 // Test pad key events
3900 ASSERT_NO_FATAL_FAILURE(
3901 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3902 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3903 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3904 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3905 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3907 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3908
3909 ASSERT_NO_FATAL_FAILURE(
3910 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3911 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3912 AKEYCODE_DPAD_RIGHT, newDisplayId));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3914 AKEYCODE_DPAD_DOWN, newDisplayId));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3916 AKEYCODE_DPAD_LEFT, newDisplayId));
3917}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
arthurhungc903df12020-08-11 15:08:42 +08003919TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3920 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3921 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3922 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3924 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3925 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3926
3927 KeyboardInputMapper& mapper =
3928 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3929 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003930 // Initial metastate is AMETA_NONE.
3931 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003932
3933 // Initialization should have turned all of the lights off.
3934 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3935 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3936 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3937
3938 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003941 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3942 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3943
3944 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003947 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3948 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3949
3950 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003953 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3954 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3955
3956 mFakeEventHub->removeDevice(EVENTHUB_ID);
3957 mReader->loopOnce();
3958
3959 // keyboard 2 should default toggle keys.
3960 const std::string USB2 = "USB2";
3961 const std::string DEVICE_NAME2 = "KEYBOARD2";
3962 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3963 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3964 std::shared_ptr<InputDevice> device2 =
3965 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003966 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003967 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3968 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3969 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3970 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3971 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3972 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3973
arthurhung6fe95782020-10-05 22:41:16 +08003974 KeyboardInputMapper& mapper2 =
3975 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3976 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003977 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3978 device2->reset(ARBITRARY_TIME);
3979
3980 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3981 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3982 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003983 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3984 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003985}
3986
Arthur Hungcb40a002021-08-03 14:31:01 +00003987TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3989 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3990 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3991
3992 // Suppose we have two mappers. (DPAD + KEYBOARD)
3993 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3994 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3995 KeyboardInputMapper& mapper =
3996 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3997 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003998 // Initial metastate is AMETA_NONE.
3999 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004000
4001 mReader->toggleCapsLockState(DEVICE_ID);
4002 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4003}
4004
Arthur Hungfb3cc112022-04-13 07:39:50 +00004005TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4006 // keyboard 1.
4007 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4008 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4009 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4013
4014 KeyboardInputMapper& mapper1 =
4015 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4016 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4017
4018 // keyboard 2.
4019 const std::string USB2 = "USB2";
4020 const std::string DEVICE_NAME2 = "KEYBOARD2";
4021 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4022 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4023 std::shared_ptr<InputDevice> device2 =
4024 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4025 ftl::Flags<InputDeviceClass>(0));
4026 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4027 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4028 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4029 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4030 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4031 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4032
4033 KeyboardInputMapper& mapper2 =
4034 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4035 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4036 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4037 device2->reset(ARBITRARY_TIME);
4038
Arthur Hung95f68612022-04-07 14:08:22 +08004039 // Initial metastate is AMETA_NONE.
4040 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4041 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4042
4043 // Toggle num lock on and off.
4044 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4045 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4047 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4048 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4049
4050 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4051 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4052 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4053 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4054 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4055
4056 // Toggle caps lock on and off.
4057 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4058 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4059 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4060 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4061 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4062
4063 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4064 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4065 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4066 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4067 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4068
4069 // Toggle scroll lock on and off.
4070 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4071 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4072 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4073 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4074 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4075
4076 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4077 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4078 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4079 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4080 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4081}
4082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004083// --- KeyboardInputMapperTest_ExternalDevice ---
4084
4085class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4086protected:
Chris Yea52ade12020-08-27 16:49:20 -07004087 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004088};
4089
4090TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004091 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4092 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004094 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4097 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 KeyboardInputMapper& mapper =
4100 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4101 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004104 NotifyKeyArgs args;
4105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4106 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4107
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004108 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4110 ASSERT_EQ(uint32_t(0), args.policyFlags);
4111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4114 ASSERT_EQ(uint32_t(0), args.policyFlags);
4115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4118 ASSERT_EQ(uint32_t(0), args.policyFlags);
4119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4122 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004124 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4126 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4127}
4128
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004130 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004131
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004135
Powei Fengd041c5d2019-05-03 17:11:33 -07004136 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004137 KeyboardInputMapper& mapper =
4138 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4139 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004142 NotifyKeyArgs args;
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4144 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4149
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(uint32_t(0), args.policyFlags);
4153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4165}
4166
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167// --- CursorInputMapperTest ---
4168
4169class CursorInputMapperTest : public InputMapperTest {
4170protected:
4171 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4172
Michael Wright17db18e2020-06-26 20:51:44 +01004173 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174
Chris Yea52ade12020-08-27 16:49:20 -07004175 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 InputMapperTest::SetUp();
4177
Michael Wright17db18e2020-06-26 20:51:44 +01004178 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004179 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 }
4181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004182 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4183 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004184
4185 void prepareDisplay(int32_t orientation) {
4186 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004187 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4189 orientation, uniqueId, NO_PORT, viewportType);
4190 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191
4192 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4193 float pressure) {
4194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4195 0.0f, 0.0f, 0.0f, EPSILON));
4196 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197};
4198
4199const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4202 int32_t originalY, int32_t rotatedX,
4203 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 NotifyMotionArgs args;
4205
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004211 ASSERT_NO_FATAL_FAILURE(
4212 assertCursorPointerCoords(args.pointerCoords[0],
4213 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4214 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215}
4216
4217TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222}
4223
4224TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229}
4230
4231TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
4235 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004239 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4240 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4242 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4243
4244 // When the bounds are set, then there should be a valid motion range.
4245 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4246
4247 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004248 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
4250 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4251 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4252 1, 800 - 1, 0.0f, 0.0f));
4253 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4254 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4255 2, 480 - 1, 0.0f, 0.0f));
4256 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4257 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4258 0.0f, 1.0f, 0.0f, 0.0f));
4259}
4260
4261TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004263 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264
4265 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004266 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
4268 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4269 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4270 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4271 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4272 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4273 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4274 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4275 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4276 0.0f, 1.0f, 0.0f, 0.0f));
4277}
4278
4279TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004281 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282
arthurhungdcef2dc2020-08-11 14:47:50 +08004283 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
4285 NotifyMotionArgs args;
4286
4287 // Button press.
4288 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4292 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4293 ASSERT_EQ(DEVICE_ID, args.deviceId);
4294 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4295 ASSERT_EQ(uint32_t(0), args.policyFlags);
4296 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4297 ASSERT_EQ(0, args.flags);
4298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4299 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4300 ASSERT_EQ(0, args.edgeFlags);
4301 ASSERT_EQ(uint32_t(1), args.pointerCount);
4302 ASSERT_EQ(0, args.pointerProperties[0].id);
4303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004304 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4306 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4307 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4308
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4310 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4311 ASSERT_EQ(DEVICE_ID, args.deviceId);
4312 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4313 ASSERT_EQ(uint32_t(0), args.policyFlags);
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4315 ASSERT_EQ(0, args.flags);
4316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4317 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4318 ASSERT_EQ(0, args.edgeFlags);
4319 ASSERT_EQ(uint32_t(1), args.pointerCount);
4320 ASSERT_EQ(0, args.pointerProperties[0].id);
4321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004322 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4324 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4325 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4326
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4329 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4331 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4332 ASSERT_EQ(DEVICE_ID, args.deviceId);
4333 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4334 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4336 ASSERT_EQ(0, args.flags);
4337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4338 ASSERT_EQ(0, args.buttonState);
4339 ASSERT_EQ(0, args.edgeFlags);
4340 ASSERT_EQ(uint32_t(1), args.pointerCount);
4341 ASSERT_EQ(0, args.pointerProperties[0].id);
4342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004343 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4345 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4346 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4347
4348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4349 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4350 ASSERT_EQ(DEVICE_ID, args.deviceId);
4351 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4352 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4354 ASSERT_EQ(0, args.flags);
4355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4356 ASSERT_EQ(0, args.buttonState);
4357 ASSERT_EQ(0, args.edgeFlags);
4358 ASSERT_EQ(uint32_t(1), args.pointerCount);
4359 ASSERT_EQ(0, args.pointerProperties[0].id);
4360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4363 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4364 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4365}
4366
4367TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370
4371 NotifyMotionArgs args;
4372
4373 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004378 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4379 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4380 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381
4382 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004387 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4388 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389}
4390
4391TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004393 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394
4395 NotifyMotionArgs args;
4396
4397 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004402 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004407
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004414
4415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418}
4419
4420TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423
4424 NotifyMotionArgs args;
4425
4426 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004433 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4434 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4435 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004439 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4440 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4441 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4450 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4451 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452
4453 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004458 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463}
4464
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004465TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004467 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4468 // need to be rotated.
4469 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004470 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004472 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4474 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4475 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4476 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4481}
4482
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004483TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004485 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4486 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004487 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004489 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4491 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4492 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4493 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004499 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4502 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004509 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4518
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004519 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4522 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528}
4529
4530TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533
4534 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4535 mFakePointerController->setPosition(100, 200);
4536 mFakePointerController->setButtonState(0);
4537
4538 NotifyMotionArgs motionArgs;
4539 NotifyKeyArgs keyArgs;
4540
4541 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4546 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4547 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004548 ASSERT_NO_FATAL_FAILURE(
4549 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4553 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4554 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(
4556 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 ASSERT_EQ(0, motionArgs.buttonState);
4563 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004564 ASSERT_NO_FATAL_FAILURE(
4565 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_EQ(0, motionArgs.buttonState);
4570 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004571 ASSERT_NO_FATAL_FAILURE(
4572 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004576 ASSERT_EQ(0, motionArgs.buttonState);
4577 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004578 ASSERT_NO_FATAL_FAILURE(
4579 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
4581 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4588 motionArgs.buttonState);
4589 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4590 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004591 ASSERT_NO_FATAL_FAILURE(
4592 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4596 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4598 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(
4600 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004601
4602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4605 motionArgs.buttonState);
4606 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4607 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4616 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(
4618 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4631 ASSERT_EQ(0, motionArgs.buttonState);
4632 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004633 ASSERT_NO_FATAL_FAILURE(
4634 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_EQ(0, motionArgs.buttonState);
4640 ASSERT_EQ(0, mFakePointerController->getButtonState());
4641 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004642 ASSERT_NO_FATAL_FAILURE(
4643 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004644
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4646 ASSERT_EQ(0, motionArgs.buttonState);
4647 ASSERT_EQ(0, mFakePointerController->getButtonState());
4648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004649 ASSERT_NO_FATAL_FAILURE(
4650 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
4652 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4656 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4657 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4662 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004663 ASSERT_NO_FATAL_FAILURE(
4664 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004665
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4668 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4669 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004670 ASSERT_NO_FATAL_FAILURE(
4671 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677 ASSERT_EQ(0, motionArgs.buttonState);
4678 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004679 ASSERT_NO_FATAL_FAILURE(
4680 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681
4682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684 ASSERT_EQ(0, motionArgs.buttonState);
4685 ASSERT_EQ(0, mFakePointerController->getButtonState());
4686
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004687 ASSERT_NO_FATAL_FAILURE(
4688 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4690 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4691 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4692
4693 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4697 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4698 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004699
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4703 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004704 ASSERT_NO_FATAL_FAILURE(
4705 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004706
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4709 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4710 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004711 ASSERT_NO_FATAL_FAILURE(
4712 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718 ASSERT_EQ(0, motionArgs.buttonState);
4719 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004720 ASSERT_NO_FATAL_FAILURE(
4721 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004722
4723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4725 ASSERT_EQ(0, motionArgs.buttonState);
4726 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004727 ASSERT_NO_FATAL_FAILURE(
4728 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4731 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4732 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4733
4734 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4738 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4739 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4744 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004745 ASSERT_NO_FATAL_FAILURE(
4746 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004747
4748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4750 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004752 ASSERT_NO_FATAL_FAILURE(
4753 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004758 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 ASSERT_EQ(0, motionArgs.buttonState);
4760 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004761 ASSERT_NO_FATAL_FAILURE(
4762 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004763
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4765 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4766 ASSERT_EQ(0, motionArgs.buttonState);
4767 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004768 ASSERT_NO_FATAL_FAILURE(
4769 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4772 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4773 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4774
4775 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4779 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4780 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004784 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4785 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004786 ASSERT_NO_FATAL_FAILURE(
4787 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788
4789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4791 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004793 ASSERT_NO_FATAL_FAILURE(
4794 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800 ASSERT_EQ(0, motionArgs.buttonState);
4801 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004802 ASSERT_NO_FATAL_FAILURE(
4803 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804
4805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4807 ASSERT_EQ(0, motionArgs.buttonState);
4808 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004809 ASSERT_NO_FATAL_FAILURE(
4810 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004811
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4813 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4814 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4815}
4816
4817TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004819 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820
4821 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4822 mFakePointerController->setPosition(100, 200);
4823 mFakePointerController->setButtonState(0);
4824
4825 NotifyMotionArgs args;
4826
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004831 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4834 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 +01004835 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004836}
4837
4838TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004839 addConfigurationProperty("cursor.mode", "pointer");
4840 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004841 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004842
4843 NotifyDeviceResetArgs resetArgs;
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4845 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4846 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4847
4848 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4849 mFakePointerController->setPosition(100, 200);
4850 mFakePointerController->setButtonState(0);
4851
4852 NotifyMotionArgs args;
4853
4854 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4859 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4862 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 +01004863 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004864
4865 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4869 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4872 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4874 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4877 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4878
4879 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4881 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4883 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4886 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4888 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4889 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4891 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4892
4893 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4898 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4901 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 +01004902 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004903
4904 // Disable pointer capture and check that the device generation got bumped
4905 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004906 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004907 mFakePolicy->setPointerCapture(false);
4908 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004909 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004910
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4912 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4913 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4914
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4919 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4922 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 +01004923 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924}
4925
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004926/**
4927 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4928 * pointer acceleration or speed processing should not be applied.
4929 */
4930TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4931 addConfigurationProperty("cursor.mode", "pointer");
4932 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4933 100.f /*high threshold*/, 10.f /*acceleration*/);
4934 mFakePolicy->setVelocityControlParams(testParams);
4935 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4936
4937 NotifyDeviceResetArgs resetArgs;
4938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4939 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4940 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4941
4942 NotifyMotionArgs args;
4943
4944 // Move and verify scale is applied.
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4949 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4951 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4952 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4953 ASSERT_GT(relX, 10);
4954 ASSERT_GT(relY, 20);
4955
4956 // Enable Pointer Capture
4957 mFakePolicy->setPointerCapture(true);
4958 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4959 NotifyPointerCaptureChangedArgs captureArgs;
4960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4961 ASSERT_TRUE(captureArgs.request.enable);
4962
4963 // Move and verify scale is not applied.
4964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4968 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4970 ASSERT_EQ(10, args.pointerCoords[0].getX());
4971 ASSERT_EQ(20, args.pointerCoords[0].getY());
4972}
4973
Prabir Pradhan208360b2022-06-24 18:37:04 +00004974TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4975 addConfigurationProperty("cursor.mode", "pointer");
4976 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4977
4978 NotifyDeviceResetArgs resetArgs;
4979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4980 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4981 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4982
4983 // Ensure the display is rotated.
4984 prepareDisplay(DISPLAY_ORIENTATION_90);
4985
4986 NotifyMotionArgs args;
4987
4988 // Verify that the coordinates are rotated.
4989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4993 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4995 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4996 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4997
4998 // Enable Pointer Capture.
4999 mFakePolicy->setPointerCapture(true);
5000 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5001 NotifyPointerCaptureChangedArgs captureArgs;
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5003 ASSERT_TRUE(captureArgs.request.enable);
5004
5005 // Move and verify rotation is not applied.
5006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5010 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5012 ASSERT_EQ(10, args.pointerCoords[0].getX());
5013 ASSERT_EQ(20, args.pointerCoords[0].getY());
5014}
5015
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005016TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005018
Garfield Tan888a6a42020-01-09 11:39:16 -08005019 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005020 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005021 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5022 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005023 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5024 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005025 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5026 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5027
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005028 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5029 mFakePointerController->setPosition(100, 200);
5030 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005031
5032 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5037 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5040 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 +01005041 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005042 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5043}
5044
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045// --- TouchInputMapperTest ---
5046
5047class TouchInputMapperTest : public InputMapperTest {
5048protected:
5049 static const int32_t RAW_X_MIN;
5050 static const int32_t RAW_X_MAX;
5051 static const int32_t RAW_Y_MIN;
5052 static const int32_t RAW_Y_MAX;
5053 static const int32_t RAW_TOUCH_MIN;
5054 static const int32_t RAW_TOUCH_MAX;
5055 static const int32_t RAW_TOOL_MIN;
5056 static const int32_t RAW_TOOL_MAX;
5057 static const int32_t RAW_PRESSURE_MIN;
5058 static const int32_t RAW_PRESSURE_MAX;
5059 static const int32_t RAW_ORIENTATION_MIN;
5060 static const int32_t RAW_ORIENTATION_MAX;
5061 static const int32_t RAW_DISTANCE_MIN;
5062 static const int32_t RAW_DISTANCE_MAX;
5063 static const int32_t RAW_TILT_MIN;
5064 static const int32_t RAW_TILT_MAX;
5065 static const int32_t RAW_ID_MIN;
5066 static const int32_t RAW_ID_MAX;
5067 static const int32_t RAW_SLOT_MIN;
5068 static const int32_t RAW_SLOT_MAX;
5069 static const float X_PRECISION;
5070 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005071 static const float X_PRECISION_VIRTUAL;
5072 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073
5074 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005075 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076
5077 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5078
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005079 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005080 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005081
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 enum Axes {
5083 POSITION = 1 << 0,
5084 TOUCH = 1 << 1,
5085 TOOL = 1 << 2,
5086 PRESSURE = 1 << 3,
5087 ORIENTATION = 1 << 4,
5088 MINOR = 1 << 5,
5089 ID = 1 << 6,
5090 DISTANCE = 1 << 7,
5091 TILT = 1 << 8,
5092 SLOT = 1 << 9,
5093 TOOL_TYPE = 1 << 10,
5094 };
5095
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005096 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5097 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005098 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005100 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 int32_t toRawX(float displayX);
5102 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005103 int32_t toRotatedRawX(float displayX);
5104 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005105 float toCookedX(float rawX, float rawY);
5106 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005108 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005110 float toDisplayY(int32_t rawY, int32_t displayHeight);
5111
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112};
5113
5114const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5115const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5116const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5117const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5118const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5119const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5120const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5121const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005122const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5123const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5125const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5126const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5128const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5129const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5130const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5132const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5134const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5135const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005136const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5137 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5138const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5139 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005140const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5141 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142
5143const float TouchInputMapperTest::GEOMETRIC_SCALE =
5144 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5145 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5146
5147const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5148 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5149 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5150};
5151
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005152void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005153 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5154 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005155}
5156
5157void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5158 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5159 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160}
5161
Santos Cordonfa5cf462017-04-05 10:37:00 -07005162void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005163 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5164 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5165 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005166}
5167
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005169 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5170 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5172 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173}
5174
Jason Gerecke489fda82012-09-07 17:19:40 -07005175void TouchInputMapperTest::prepareLocationCalibration() {
5176 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5177}
5178
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179int32_t TouchInputMapperTest::toRawX(float displayX) {
5180 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5181}
5182
5183int32_t TouchInputMapperTest::toRawY(float displayY) {
5184 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5185}
5186
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005187int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5188 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5189}
5190
5191int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5192 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5193}
5194
Jason Gerecke489fda82012-09-07 17:19:40 -07005195float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5196 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5197 return rawX;
5198}
5199
5200float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5201 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5202 return rawY;
5203}
5204
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005206 return toDisplayX(rawX, DISPLAY_WIDTH);
5207}
5208
5209float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5210 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211}
5212
5213float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005214 return toDisplayY(rawY, DISPLAY_HEIGHT);
5215}
5216
5217float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5218 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219}
5220
5221
5222// --- SingleTouchInputMapperTest ---
5223
5224class SingleTouchInputMapperTest : public TouchInputMapperTest {
5225protected:
5226 void prepareButtons();
5227 void prepareAxes(int axes);
5228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005229 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5230 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5231 void processUp(SingleTouchInputMapper& mappery);
5232 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5233 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5234 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5235 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5236 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5237 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238};
5239
5240void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005241 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242}
5243
5244void SingleTouchInputMapperTest::prepareAxes(int axes) {
5245 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248 }
5249 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5251 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 }
5253 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5255 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 }
5257 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5259 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 }
5261 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 }
5265}
5266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005267void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271}
5272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276}
5277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005278void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005282void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284}
5285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5287 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005289}
5290
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005291void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5296 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299}
5300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5302 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005304}
5305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 prepareButtons();
5312 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005315 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005316}
5317
5318TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005319 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5320 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
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_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326}
5327
5328TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 prepareButtons();
5330 prepareAxes(POSITION);
5331 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005332 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005334 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335}
5336
5337TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 prepareButtons();
5339 prepareAxes(POSITION);
5340 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005343 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344}
5345
5346TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347 addConfigurationProperty("touch.deviceType", "touchScreen");
5348 prepareDisplay(DISPLAY_ORIENTATION_0);
5349 prepareButtons();
5350 prepareAxes(POSITION);
5351 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005352 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353
5354 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005355 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005356
5357 // Virtual key is down.
5358 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5359 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5360 processDown(mapper, x, y);
5361 processSync(mapper);
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5363
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005364 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365
5366 // Virtual key is up.
5367 processUp(mapper);
5368 processSync(mapper);
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372}
5373
5374TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 addConfigurationProperty("touch.deviceType", "touchScreen");
5376 prepareDisplay(DISPLAY_ORIENTATION_0);
5377 prepareButtons();
5378 prepareAxes(POSITION);
5379 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381
5382 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005383 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384
5385 // Virtual key is down.
5386 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5387 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5388 processDown(mapper, x, y);
5389 processSync(mapper);
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5391
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005392 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
5394 // Virtual key is up.
5395 processUp(mapper);
5396 processSync(mapper);
5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005399 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400}
5401
5402TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 addConfigurationProperty("touch.deviceType", "touchScreen");
5404 prepareDisplay(DISPLAY_ORIENTATION_0);
5405 prepareButtons();
5406 prepareAxes(POSITION);
5407 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005408 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005411 ASSERT_TRUE(
5412 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 ASSERT_TRUE(flags[0]);
5414 ASSERT_FALSE(flags[1]);
5415}
5416
5417TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418 addConfigurationProperty("touch.deviceType", "touchScreen");
5419 prepareDisplay(DISPLAY_ORIENTATION_0);
5420 prepareButtons();
5421 prepareAxes(POSITION);
5422 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005424
arthurhungdcef2dc2020-08-11 14:47:50 +08005425 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426
5427 NotifyKeyArgs args;
5428
5429 // Press virtual key.
5430 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5431 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5432 processDown(mapper, x, y);
5433 processSync(mapper);
5434
5435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5436 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5437 ASSERT_EQ(DEVICE_ID, args.deviceId);
5438 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5439 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5441 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5442 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5443 ASSERT_EQ(KEY_HOME, args.scanCode);
5444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5445 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5446
5447 // Release virtual key.
5448 processUp(mapper);
5449 processSync(mapper);
5450
5451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5452 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5453 ASSERT_EQ(DEVICE_ID, args.deviceId);
5454 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5455 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5456 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5457 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5458 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5459 ASSERT_EQ(KEY_HOME, args.scanCode);
5460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5461 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5462
5463 // Should not have sent any motions.
5464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5465}
5466
5467TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 addConfigurationProperty("touch.deviceType", "touchScreen");
5469 prepareDisplay(DISPLAY_ORIENTATION_0);
5470 prepareButtons();
5471 prepareAxes(POSITION);
5472 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474
arthurhungdcef2dc2020-08-11 14:47:50 +08005475 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476
5477 NotifyKeyArgs keyArgs;
5478
5479 // Press virtual key.
5480 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5481 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5482 processDown(mapper, x, y);
5483 processSync(mapper);
5484
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5486 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5487 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5488 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5489 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5490 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5491 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5492 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5493 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5494 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5495 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5496
5497 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5498 // into the display area.
5499 y -= 100;
5500 processMove(mapper, x, y);
5501 processSync(mapper);
5502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5504 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5505 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5506 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5507 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5508 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5509 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5510 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5511 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5512 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5514 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5515
5516 NotifyMotionArgs motionArgs;
5517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5519 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5523 ASSERT_EQ(0, motionArgs.flags);
5524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5525 ASSERT_EQ(0, motionArgs.buttonState);
5526 ASSERT_EQ(0, motionArgs.edgeFlags);
5527 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5531 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5532 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5533 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5535
5536 // Keep moving out of bounds. Should generate a pointer move.
5537 y -= 50;
5538 processMove(mapper, x, y);
5539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5543 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5544 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5545 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5547 ASSERT_EQ(0, motionArgs.flags);
5548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5549 ASSERT_EQ(0, motionArgs.buttonState);
5550 ASSERT_EQ(0, motionArgs.edgeFlags);
5551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5555 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5556 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5557 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5559
5560 // Release out of bounds. Should generate a pointer up.
5561 processUp(mapper);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5566 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5567 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5568 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5570 ASSERT_EQ(0, motionArgs.flags);
5571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5572 ASSERT_EQ(0, motionArgs.buttonState);
5573 ASSERT_EQ(0, motionArgs.edgeFlags);
5574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5578 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5579 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5580 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5581 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5582
5583 // Should not have sent any more keys or motions.
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5586}
5587
5588TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 addConfigurationProperty("touch.deviceType", "touchScreen");
5590 prepareDisplay(DISPLAY_ORIENTATION_0);
5591 prepareButtons();
5592 prepareAxes(POSITION);
5593 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005594 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595
arthurhungdcef2dc2020-08-11 14:47:50 +08005596 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597
5598 NotifyMotionArgs motionArgs;
5599
5600 // Initially go down out of bounds.
5601 int32_t x = -10;
5602 int32_t y = -10;
5603 processDown(mapper, x, y);
5604 processSync(mapper);
5605
5606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5607
5608 // Move into the display area. Should generate a pointer down.
5609 x = 50;
5610 y = 75;
5611 processMove(mapper, x, y);
5612 processSync(mapper);
5613
5614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5616 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5617 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5618 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5619 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5620 ASSERT_EQ(0, motionArgs.flags);
5621 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5622 ASSERT_EQ(0, motionArgs.buttonState);
5623 ASSERT_EQ(0, motionArgs.edgeFlags);
5624 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5625 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5628 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5629 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5630 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5632
5633 // Release. Should generate a pointer up.
5634 processUp(mapper);
5635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5642 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5643 ASSERT_EQ(0, motionArgs.flags);
5644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5645 ASSERT_EQ(0, motionArgs.buttonState);
5646 ASSERT_EQ(0, motionArgs.edgeFlags);
5647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5651 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5655
5656 // Should not have sent any more keys or motions.
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5659}
5660
Santos Cordonfa5cf462017-04-05 10:37:00 -07005661TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005662 addConfigurationProperty("touch.deviceType", "touchScreen");
5663 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5664
5665 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5666 prepareButtons();
5667 prepareAxes(POSITION);
5668 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005669 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005670
arthurhungdcef2dc2020-08-11 14:47:50 +08005671 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005672
5673 NotifyMotionArgs motionArgs;
5674
5675 // Down.
5676 int32_t x = 100;
5677 int32_t y = 125;
5678 processDown(mapper, x, y);
5679 processSync(mapper);
5680
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5682 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5683 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5684 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5685 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5686 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5687 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5688 ASSERT_EQ(0, motionArgs.flags);
5689 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5690 ASSERT_EQ(0, motionArgs.buttonState);
5691 ASSERT_EQ(0, motionArgs.edgeFlags);
5692 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5696 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5697 1, 0, 0, 0, 0, 0, 0, 0));
5698 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5699 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5700 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5701
5702 // Move.
5703 x += 50;
5704 y += 75;
5705 processMove(mapper, x, y);
5706 processSync(mapper);
5707
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5710 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5711 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5712 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5713 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5715 ASSERT_EQ(0, motionArgs.flags);
5716 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5717 ASSERT_EQ(0, motionArgs.buttonState);
5718 ASSERT_EQ(0, motionArgs.edgeFlags);
5719 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5723 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5724 1, 0, 0, 0, 0, 0, 0, 0));
5725 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5726 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5728
5729 // Up.
5730 processUp(mapper);
5731 processSync(mapper);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5735 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5736 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5740 ASSERT_EQ(0, motionArgs.flags);
5741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5742 ASSERT_EQ(0, motionArgs.buttonState);
5743 ASSERT_EQ(0, motionArgs.edgeFlags);
5744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5749 1, 0, 0, 0, 0, 0, 0, 0));
5750 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5751 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5753
5754 // Should not have sent any more keys or motions.
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5757}
5758
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 addConfigurationProperty("touch.deviceType", "touchScreen");
5761 prepareDisplay(DISPLAY_ORIENTATION_0);
5762 prepareButtons();
5763 prepareAxes(POSITION);
5764 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005765 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766
arthurhungdcef2dc2020-08-11 14:47:50 +08005767 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768
5769 NotifyMotionArgs motionArgs;
5770
5771 // Down.
5772 int32_t x = 100;
5773 int32_t y = 125;
5774 processDown(mapper, x, y);
5775 processSync(mapper);
5776
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5779 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5780 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5781 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5783 ASSERT_EQ(0, motionArgs.flags);
5784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5785 ASSERT_EQ(0, motionArgs.buttonState);
5786 ASSERT_EQ(0, motionArgs.edgeFlags);
5787 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5791 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5792 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5793 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5795
5796 // Move.
5797 x += 50;
5798 y += 75;
5799 processMove(mapper, x, y);
5800 processSync(mapper);
5801
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5804 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5805 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5806 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5808 ASSERT_EQ(0, motionArgs.flags);
5809 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5810 ASSERT_EQ(0, motionArgs.buttonState);
5811 ASSERT_EQ(0, motionArgs.edgeFlags);
5812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5816 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5817 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5818 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5820
5821 // Up.
5822 processUp(mapper);
5823 processSync(mapper);
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5826 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5827 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5828 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5829 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5831 ASSERT_EQ(0, motionArgs.flags);
5832 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5833 ASSERT_EQ(0, motionArgs.buttonState);
5834 ASSERT_EQ(0, motionArgs.edgeFlags);
5835 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5839 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5840 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5841 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5842 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5843
5844 // Should not have sent any more keys or motions.
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5847}
5848
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005849TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 addConfigurationProperty("touch.deviceType", "touchScreen");
5851 prepareButtons();
5852 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005853 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5854 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005855 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856
5857 NotifyMotionArgs args;
5858
5859 // Rotation 90.
5860 prepareDisplay(DISPLAY_ORIENTATION_90);
5861 processDown(mapper, toRawX(50), toRawY(75));
5862 processSync(mapper);
5863
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5865 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5866 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5867
5868 processUp(mapper);
5869 processSync(mapper);
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5871}
5872
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005873TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 addConfigurationProperty("touch.deviceType", "touchScreen");
5875 prepareButtons();
5876 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005877 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5878 // orientation-aware are affected by display rotation.
5879 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005880 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881
5882 NotifyMotionArgs args;
5883
5884 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005885 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 prepareDisplay(DISPLAY_ORIENTATION_0);
5887 processDown(mapper, toRawX(50), toRawY(75));
5888 processSync(mapper);
5889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5891 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5892 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5893
5894 processUp(mapper);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5897
5898 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005899 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005901 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 processSync(mapper);
5903
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5906 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5907
5908 processUp(mapper);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5911
5912 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005913 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 prepareDisplay(DISPLAY_ORIENTATION_180);
5915 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5916 processSync(mapper);
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5919 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5920 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5921
5922 processUp(mapper);
5923 processSync(mapper);
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5925
5926 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005927 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005929 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 processSync(mapper);
5931
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5933 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5934 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5935
5936 processUp(mapper);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5939}
5940
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005941TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5942 addConfigurationProperty("touch.deviceType", "touchScreen");
5943 prepareButtons();
5944 prepareAxes(POSITION);
5945 addConfigurationProperty("touch.orientationAware", "1");
5946 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5947 clearViewports();
5948 prepareDisplay(DISPLAY_ORIENTATION_0);
5949 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5950 NotifyMotionArgs args;
5951
5952 // Orientation 0.
5953 processDown(mapper, toRawX(50), toRawY(75));
5954 processSync(mapper);
5955
5956 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5957 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5958 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5959
5960 processUp(mapper);
5961 processSync(mapper);
5962 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5963}
5964
5965TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5966 addConfigurationProperty("touch.deviceType", "touchScreen");
5967 prepareButtons();
5968 prepareAxes(POSITION);
5969 addConfigurationProperty("touch.orientationAware", "1");
5970 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5971 clearViewports();
5972 prepareDisplay(DISPLAY_ORIENTATION_0);
5973 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5974 NotifyMotionArgs args;
5975
5976 // Orientation 90.
5977 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5978 processSync(mapper);
5979
5980 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5981 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5982 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5983
5984 processUp(mapper);
5985 processSync(mapper);
5986 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5987}
5988
5989TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5990 addConfigurationProperty("touch.deviceType", "touchScreen");
5991 prepareButtons();
5992 prepareAxes(POSITION);
5993 addConfigurationProperty("touch.orientationAware", "1");
5994 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5995 clearViewports();
5996 prepareDisplay(DISPLAY_ORIENTATION_0);
5997 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5998 NotifyMotionArgs args;
5999
6000 // Orientation 180.
6001 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6002 processSync(mapper);
6003
6004 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6005 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6006 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6007
6008 processUp(mapper);
6009 processSync(mapper);
6010 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6011}
6012
6013TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6014 addConfigurationProperty("touch.deviceType", "touchScreen");
6015 prepareButtons();
6016 prepareAxes(POSITION);
6017 addConfigurationProperty("touch.orientationAware", "1");
6018 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6019 clearViewports();
6020 prepareDisplay(DISPLAY_ORIENTATION_0);
6021 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6022 NotifyMotionArgs args;
6023
6024 // Orientation 270.
6025 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6026 processSync(mapper);
6027
6028 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6029 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6030 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6031
6032 processUp(mapper);
6033 processSync(mapper);
6034 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6035}
6036
6037TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6038 addConfigurationProperty("touch.deviceType", "touchScreen");
6039 prepareButtons();
6040 prepareAxes(POSITION);
6041 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6042 // orientation-aware are affected by display rotation.
6043 addConfigurationProperty("touch.orientationAware", "0");
6044 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6045 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6046
6047 NotifyMotionArgs args;
6048
6049 // Orientation 90, Rotation 0.
6050 clearViewports();
6051 prepareDisplay(DISPLAY_ORIENTATION_0);
6052 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6053 processSync(mapper);
6054
6055 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6056 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6057 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6058
6059 processUp(mapper);
6060 processSync(mapper);
6061 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6062
6063 // Orientation 90, Rotation 90.
6064 clearViewports();
6065 prepareDisplay(DISPLAY_ORIENTATION_90);
6066 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6067 processSync(mapper);
6068
6069 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6070 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6071 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6072
6073 processUp(mapper);
6074 processSync(mapper);
6075 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6076
6077 // Orientation 90, Rotation 180.
6078 clearViewports();
6079 prepareDisplay(DISPLAY_ORIENTATION_180);
6080 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6081 processSync(mapper);
6082
6083 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6084 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6085 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6086
6087 processUp(mapper);
6088 processSync(mapper);
6089 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6090
6091 // Orientation 90, Rotation 270.
6092 clearViewports();
6093 prepareDisplay(DISPLAY_ORIENTATION_270);
6094 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6095 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6096 processSync(mapper);
6097
6098 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6099 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6100 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6101
6102 processUp(mapper);
6103 processSync(mapper);
6104 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6105}
6106
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 addConfigurationProperty("touch.deviceType", "touchScreen");
6109 prepareDisplay(DISPLAY_ORIENTATION_0);
6110 prepareButtons();
6111 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006112 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006113
6114 // These calculations are based on the input device calibration documentation.
6115 int32_t rawX = 100;
6116 int32_t rawY = 200;
6117 int32_t rawPressure = 10;
6118 int32_t rawToolMajor = 12;
6119 int32_t rawDistance = 2;
6120 int32_t rawTiltX = 30;
6121 int32_t rawTiltY = 110;
6122
6123 float x = toDisplayX(rawX);
6124 float y = toDisplayY(rawY);
6125 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6126 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6127 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6128 float distance = float(rawDistance);
6129
6130 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6131 float tiltScale = M_PI / 180;
6132 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6133 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6134 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6135 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6136
6137 processDown(mapper, rawX, rawY);
6138 processPressure(mapper, rawPressure);
6139 processToolMajor(mapper, rawToolMajor);
6140 processDistance(mapper, rawDistance);
6141 processTilt(mapper, rawTiltX, rawTiltY);
6142 processSync(mapper);
6143
6144 NotifyMotionArgs args;
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6147 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6148 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6149}
6150
Jason Gerecke489fda82012-09-07 17:19:40 -07006151TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006152 addConfigurationProperty("touch.deviceType", "touchScreen");
6153 prepareDisplay(DISPLAY_ORIENTATION_0);
6154 prepareLocationCalibration();
6155 prepareButtons();
6156 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006158
6159 int32_t rawX = 100;
6160 int32_t rawY = 200;
6161
6162 float x = toDisplayX(toCookedX(rawX, rawY));
6163 float y = toDisplayY(toCookedY(rawX, rawY));
6164
6165 processDown(mapper, rawX, rawY);
6166 processSync(mapper);
6167
6168 NotifyMotionArgs args;
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6171 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6172}
6173
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 addConfigurationProperty("touch.deviceType", "touchScreen");
6176 prepareDisplay(DISPLAY_ORIENTATION_0);
6177 prepareButtons();
6178 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006179 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006180
6181 NotifyMotionArgs motionArgs;
6182 NotifyKeyArgs keyArgs;
6183
6184 processDown(mapper, 100, 200);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6188 ASSERT_EQ(0, motionArgs.buttonState);
6189
6190 // press BTN_LEFT, release BTN_LEFT
6191 processKey(mapper, BTN_LEFT, 1);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6195 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6196
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6200
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 processKey(mapper, BTN_LEFT, 0);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6212 processKey(mapper, BTN_RIGHT, 1);
6213 processKey(mapper, BTN_MIDDLE, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6217 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6218 motionArgs.buttonState);
6219
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6222 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6223
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6227 motionArgs.buttonState);
6228
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 processKey(mapper, BTN_RIGHT, 0);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006234
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006238
6239 processKey(mapper, BTN_MIDDLE, 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(0, 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006248
6249 // press BTN_BACK, release BTN_BACK
6250 processKey(mapper, BTN_BACK, 1);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6254 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006258 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6259
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006263
6264 processKey(mapper, BTN_BACK, 0);
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006269
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
6273
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6276 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6277
6278 // press BTN_SIDE, release BTN_SIDE
6279 processKey(mapper, BTN_SIDE, 1);
6280 processSync(mapper);
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6282 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6283 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006287 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292
6293 processKey(mapper, BTN_SIDE, 0);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006297 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006298
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301 ASSERT_EQ(0, motionArgs.buttonState);
6302
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6304 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6305 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6306
6307 // press BTN_FORWARD, release BTN_FORWARD
6308 processKey(mapper, BTN_FORWARD, 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6311 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6312 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006313
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006316 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 processKey(mapper, BTN_FORWARD, 0);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006326 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
6331
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6333 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6334 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6335
6336 // press BTN_EXTRA, release BTN_EXTRA
6337 processKey(mapper, BTN_EXTRA, 1);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6340 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6341 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006345 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006350
6351 processKey(mapper, BTN_EXTRA, 0);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
6360
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6362 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6363 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6364
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6366
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367 // press BTN_STYLUS, release BTN_STYLUS
6368 processKey(mapper, BTN_STYLUS, 1);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006372 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377
6378 processKey(mapper, BTN_STYLUS, 0);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006383
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006387
6388 // press BTN_STYLUS2, release BTN_STYLUS2
6389 processKey(mapper, BTN_STYLUS2, 1);
6390 processSync(mapper);
6391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006393 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6394
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006398
6399 processKey(mapper, BTN_STYLUS2, 0);
6400 processSync(mapper);
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006403 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006408
6409 // release touch
6410 processUp(mapper);
6411 processSync(mapper);
6412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6413 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6414 ASSERT_EQ(0, motionArgs.buttonState);
6415}
6416
6417TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 addConfigurationProperty("touch.deviceType", "touchScreen");
6419 prepareDisplay(DISPLAY_ORIENTATION_0);
6420 prepareButtons();
6421 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006422 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423
6424 NotifyMotionArgs motionArgs;
6425
6426 // default tool type is finger
6427 processDown(mapper, 100, 200);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6432
6433 // eraser
6434 processKey(mapper, BTN_TOOL_RUBBER, 1);
6435 processSync(mapper);
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6439
6440 // stylus
6441 processKey(mapper, BTN_TOOL_RUBBER, 0);
6442 processKey(mapper, BTN_TOOL_PEN, 1);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6447
6448 // brush
6449 processKey(mapper, BTN_TOOL_PEN, 0);
6450 processKey(mapper, BTN_TOOL_BRUSH, 1);
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6455
6456 // pencil
6457 processKey(mapper, BTN_TOOL_BRUSH, 0);
6458 processKey(mapper, BTN_TOOL_PENCIL, 1);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6463
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006464 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 processKey(mapper, BTN_TOOL_PENCIL, 0);
6466 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6471
6472 // mouse
6473 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6474 processKey(mapper, BTN_TOOL_MOUSE, 1);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6479
6480 // lens
6481 processKey(mapper, BTN_TOOL_MOUSE, 0);
6482 processKey(mapper, BTN_TOOL_LENS, 1);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6487
6488 // double-tap
6489 processKey(mapper, BTN_TOOL_LENS, 0);
6490 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6491 processSync(mapper);
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6495
6496 // triple-tap
6497 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6498 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6503
6504 // quad-tap
6505 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6506 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6511
6512 // finger
6513 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6514 processKey(mapper, BTN_TOOL_FINGER, 1);
6515 processSync(mapper);
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6519
6520 // stylus trumps finger
6521 processKey(mapper, BTN_TOOL_PEN, 1);
6522 processSync(mapper);
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6526
6527 // eraser trumps stylus
6528 processKey(mapper, BTN_TOOL_RUBBER, 1);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6533
6534 // mouse trumps eraser
6535 processKey(mapper, BTN_TOOL_MOUSE, 1);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6540
6541 // back to default tool type
6542 processKey(mapper, BTN_TOOL_MOUSE, 0);
6543 processKey(mapper, BTN_TOOL_RUBBER, 0);
6544 processKey(mapper, BTN_TOOL_PEN, 0);
6545 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
6550}
6551
6552TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006553 addConfigurationProperty("touch.deviceType", "touchScreen");
6554 prepareDisplay(DISPLAY_ORIENTATION_0);
6555 prepareButtons();
6556 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006557 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006558 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559
6560 NotifyMotionArgs motionArgs;
6561
6562 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6563 processKey(mapper, BTN_TOOL_FINGER, 1);
6564 processMove(mapper, 100, 200);
6565 processSync(mapper);
6566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6569 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6570
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6574 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6575
6576 // move a little
6577 processMove(mapper, 150, 250);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6582 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6583
6584 // down when BTN_TOUCH is pressed, pressure defaults to 1
6585 processKey(mapper, BTN_TOUCH, 1);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6590 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6591
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6595 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6596
6597 // up when BTN_TOUCH is released, hover restored
6598 processKey(mapper, BTN_TOUCH, 0);
6599 processSync(mapper);
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6603 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6604
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6608 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6609
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6614
6615 // exit hover when pointer goes away
6616 processKey(mapper, BTN_TOOL_FINGER, 0);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6621 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6622}
6623
6624TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 addConfigurationProperty("touch.deviceType", "touchScreen");
6626 prepareDisplay(DISPLAY_ORIENTATION_0);
6627 prepareButtons();
6628 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630
6631 NotifyMotionArgs motionArgs;
6632
6633 // initially hovering because pressure is 0
6634 processDown(mapper, 100, 200);
6635 processPressure(mapper, 0);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6640 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6641
6642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6645 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6646
6647 // move a little
6648 processMove(mapper, 150, 250);
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6653 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6654
6655 // down when pressure is non-zero
6656 processPressure(mapper, RAW_PRESSURE_MAX);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6661 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6662
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6664 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6666 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6667
6668 // up when pressure becomes 0, hover restored
6669 processPressure(mapper, 0);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6672 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6674 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6675
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6680
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6682 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6684 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6685
6686 // exit hover when pointer goes away
6687 processUp(mapper);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6690 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6692 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6693}
6694
Prabir Pradhan5632d622021-09-06 07:57:20 -07006695// --- TouchDisplayProjectionTest ---
6696
6697class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6698public:
6699 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6700 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6701 // rotated equivalent of the given un-rotated physical display bounds.
6702 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6703 uint32_t inverseRotationFlags;
6704 auto width = DISPLAY_WIDTH;
6705 auto height = DISPLAY_HEIGHT;
6706 switch (orientation) {
6707 case DISPLAY_ORIENTATION_90:
6708 inverseRotationFlags = ui::Transform::ROT_270;
6709 std::swap(width, height);
6710 break;
6711 case DISPLAY_ORIENTATION_180:
6712 inverseRotationFlags = ui::Transform::ROT_180;
6713 break;
6714 case DISPLAY_ORIENTATION_270:
6715 inverseRotationFlags = ui::Transform::ROT_90;
6716 std::swap(width, height);
6717 break;
6718 case DISPLAY_ORIENTATION_0:
6719 inverseRotationFlags = ui::Transform::ROT_0;
6720 break;
6721 default:
6722 FAIL() << "Invalid orientation: " << orientation;
6723 }
6724
6725 const ui::Transform rotation(inverseRotationFlags, width, height);
6726 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6727
6728 std::optional<DisplayViewport> internalViewport =
6729 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6730 DisplayViewport& v = *internalViewport;
6731 v.displayId = DISPLAY_ID;
6732 v.orientation = orientation;
6733
6734 v.logicalLeft = 0;
6735 v.logicalTop = 0;
6736 v.logicalRight = 100;
6737 v.logicalBottom = 100;
6738
6739 v.physicalLeft = rotatedPhysicalDisplay.left;
6740 v.physicalTop = rotatedPhysicalDisplay.top;
6741 v.physicalRight = rotatedPhysicalDisplay.right;
6742 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6743
6744 v.deviceWidth = width;
6745 v.deviceHeight = height;
6746
6747 v.isActive = true;
6748 v.uniqueId = UNIQUE_ID;
6749 v.type = ViewportType::INTERNAL;
6750 mFakePolicy->updateViewport(v);
6751 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6752 }
6753
6754 void assertReceivedMove(const Point& point) {
6755 NotifyMotionArgs motionArgs;
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6760 1, 0, 0, 0, 0, 0, 0, 0));
6761 }
6762};
6763
6764TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6765 addConfigurationProperty("touch.deviceType", "touchScreen");
6766 prepareDisplay(DISPLAY_ORIENTATION_0);
6767
6768 prepareButtons();
6769 prepareAxes(POSITION);
6770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6771
6772 NotifyMotionArgs motionArgs;
6773
6774 // Configure the DisplayViewport such that the logical display maps to a subsection of
6775 // the display panel called the physical display. Here, the physical display is bounded by the
6776 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6777 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6778 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6779 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6780
6781 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6782 DISPLAY_ORIENTATION_270}) {
6783 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6784
6785 // Touches outside the physical display should be ignored, and should not generate any
6786 // events. Ensure touches at the following points that lie outside of the physical display
6787 // area do not generate any events.
6788 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6789 processDown(mapper, toRawX(point.x), toRawY(point.y));
6790 processSync(mapper);
6791 processUp(mapper);
6792 processSync(mapper);
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6794 << "Unexpected event generated for touch outside physical display at point: "
6795 << point.x << ", " << point.y;
6796 }
6797 }
6798}
6799
6800TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6801 addConfigurationProperty("touch.deviceType", "touchScreen");
6802 prepareDisplay(DISPLAY_ORIENTATION_0);
6803
6804 prepareButtons();
6805 prepareAxes(POSITION);
6806 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6807
6808 NotifyMotionArgs motionArgs;
6809
6810 // Configure the DisplayViewport such that the logical display maps to a subsection of
6811 // the display panel called the physical display. Here, the physical display is bounded by the
6812 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6813 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6814
6815 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6816 DISPLAY_ORIENTATION_270}) {
6817 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6818
6819 // Touches that start outside the physical display should be ignored until it enters the
6820 // physical display bounds, at which point it should generate a down event. Start a touch at
6821 // the point (5, 100), which is outside the physical display bounds.
6822 static const Point kOutsidePoint{5, 100};
6823 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6826
6827 // Move the touch into the physical display area. This should generate a pointer down.
6828 processMove(mapper, toRawX(11), toRawY(21));
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6831 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6832 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6833 ASSERT_NO_FATAL_FAILURE(
6834 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6835
6836 // Move the touch inside the physical display area. This should generate a pointer move.
6837 processMove(mapper, toRawX(69), toRawY(159));
6838 processSync(mapper);
6839 assertReceivedMove({69, 159});
6840
6841 // Move outside the physical display area. Since the pointer is already down, this should
6842 // now continue generating events.
6843 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6844 processSync(mapper);
6845 assertReceivedMove(kOutsidePoint);
6846
6847 // Release. This should generate a pointer up.
6848 processUp(mapper);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6853 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6854
6855 // Ensure no more events were generated.
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6858 }
6859}
6860
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861// --- MultiTouchInputMapperTest ---
6862
6863class MultiTouchInputMapperTest : public TouchInputMapperTest {
6864protected:
6865 void prepareAxes(int axes);
6866
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006867 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6868 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6869 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6870 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6871 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6872 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6873 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6874 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6875 void processId(MultiTouchInputMapper& mapper, int32_t id);
6876 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6877 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6878 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6879 void processMTSync(MultiTouchInputMapper& mapper);
6880 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881};
6882
6883void MultiTouchInputMapperTest::prepareAxes(int axes) {
6884 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006887 }
6888 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006889 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6890 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006891 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006892 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6893 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006894 }
6895 }
6896 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006897 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6898 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006901 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902 }
6903 }
6904 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006905 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6906 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 }
6908 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006909 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6910 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006911 }
6912 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6914 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915 }
6916 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6918 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919 }
6920 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6922 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006923 }
6924 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006925 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926 }
6927}
6928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006929void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6930 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933}
6934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006935void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6936 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938}
6939
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006940void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6941 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943}
6944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006945void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947}
6948
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006949void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951}
6952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006953void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6954 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956}
6957
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006958void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960}
6961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006962void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964}
6965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006966void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006968}
6969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006970void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972}
6973
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006974void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6979 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981}
6982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006983void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985}
6986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006987void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006988 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989}
6990
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992 addConfigurationProperty("touch.deviceType", "touchScreen");
6993 prepareDisplay(DISPLAY_ORIENTATION_0);
6994 prepareAxes(POSITION);
6995 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006996 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997
arthurhungdcef2dc2020-08-11 14:47:50 +08006998 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006999
7000 NotifyMotionArgs motionArgs;
7001
7002 // Two fingers down at once.
7003 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7004 processPosition(mapper, x1, y1);
7005 processMTSync(mapper);
7006 processPosition(mapper, x2, y2);
7007 processMTSync(mapper);
7008 processSync(mapper);
7009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7015 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7016 ASSERT_EQ(0, motionArgs.flags);
7017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7018 ASSERT_EQ(0, motionArgs.buttonState);
7019 ASSERT_EQ(0, motionArgs.edgeFlags);
7020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7024 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7028
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7030 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7031 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7032 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7033 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007034 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 ASSERT_EQ(0, motionArgs.flags);
7036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7037 ASSERT_EQ(0, motionArgs.buttonState);
7038 ASSERT_EQ(0, motionArgs.edgeFlags);
7039 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7042 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7045 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7047 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7048 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7049 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7051
7052 // Move.
7053 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7054 processPosition(mapper, x1, y1);
7055 processMTSync(mapper);
7056 processPosition(mapper, x2, y2);
7057 processMTSync(mapper);
7058 processSync(mapper);
7059
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7062 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7063 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7064 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7066 ASSERT_EQ(0, motionArgs.flags);
7067 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7068 ASSERT_EQ(0, motionArgs.buttonState);
7069 ASSERT_EQ(0, motionArgs.edgeFlags);
7070 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7071 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7073 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7076 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7078 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7079 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7080 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7082
7083 // First finger up.
7084 x2 += 15; y2 -= 20;
7085 processPosition(mapper, x2, y2);
7086 processMTSync(mapper);
7087 processSync(mapper);
7088
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007094 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 ASSERT_EQ(0, motionArgs.flags);
7096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7097 ASSERT_EQ(0, motionArgs.buttonState);
7098 ASSERT_EQ(0, motionArgs.edgeFlags);
7099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7108 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7109 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7110 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7111
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7114 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7115 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7116 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7118 ASSERT_EQ(0, motionArgs.flags);
7119 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7120 ASSERT_EQ(0, motionArgs.buttonState);
7121 ASSERT_EQ(0, motionArgs.edgeFlags);
7122 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7123 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7126 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7127 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7128 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7130
7131 // Move.
7132 x2 += 20; y2 -= 25;
7133 processPosition(mapper, x2, y2);
7134 processMTSync(mapper);
7135 processSync(mapper);
7136
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7140 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7141 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7143 ASSERT_EQ(0, motionArgs.flags);
7144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7145 ASSERT_EQ(0, motionArgs.buttonState);
7146 ASSERT_EQ(0, motionArgs.edgeFlags);
7147 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7148 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7151 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7152 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7153 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7154 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7155
7156 // New finger down.
7157 int32_t x3 = 700, y3 = 300;
7158 processPosition(mapper, x2, y2);
7159 processMTSync(mapper);
7160 processPosition(mapper, x3, y3);
7161 processMTSync(mapper);
7162 processSync(mapper);
7163
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7166 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007169 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170 ASSERT_EQ(0, motionArgs.flags);
7171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7172 ASSERT_EQ(0, motionArgs.buttonState);
7173 ASSERT_EQ(0, motionArgs.edgeFlags);
7174 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7177 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7186
7187 // Second finger up.
7188 x3 += 30; y3 -= 20;
7189 processPosition(mapper, x3, y3);
7190 processMTSync(mapper);
7191 processSync(mapper);
7192
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7196 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007198 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_EQ(0, motionArgs.flags);
7200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7201 ASSERT_EQ(0, motionArgs.buttonState);
7202 ASSERT_EQ(0, motionArgs.edgeFlags);
7203 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7206 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7209 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7211 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7212 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7213 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7218 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7222 ASSERT_EQ(0, motionArgs.flags);
7223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7224 ASSERT_EQ(0, motionArgs.buttonState);
7225 ASSERT_EQ(0, motionArgs.edgeFlags);
7226 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7230 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7231 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7232 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7234
7235 // Last finger up.
7236 processMTSync(mapper);
7237 processSync(mapper);
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7241 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7242 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7243 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7244 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7245 ASSERT_EQ(0, motionArgs.flags);
7246 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7247 ASSERT_EQ(0, motionArgs.buttonState);
7248 ASSERT_EQ(0, motionArgs.edgeFlags);
7249 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7250 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7257
7258 // Should not have sent any more keys or motions.
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7261}
7262
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007263TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7264 addConfigurationProperty("touch.deviceType", "touchScreen");
7265 prepareDisplay(DISPLAY_ORIENTATION_0);
7266
7267 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7268 /*fuzz*/ 0, /*resolution*/ 10);
7269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7270 /*fuzz*/ 0, /*resolution*/ 11);
7271 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7272 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7273 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7274 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7275 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7276 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7278 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7279
7280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7281
7282 // X and Y axes
7283 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7284 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7285 // Touch major and minor
7286 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7287 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7288 // Tool major and minor
7289 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7290 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7291}
7292
7293TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7294 addConfigurationProperty("touch.deviceType", "touchScreen");
7295 prepareDisplay(DISPLAY_ORIENTATION_0);
7296
7297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7298 /*fuzz*/ 0, /*resolution*/ 10);
7299 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7300 /*fuzz*/ 0, /*resolution*/ 11);
7301
7302 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7303
7304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7305
7306 // Touch major and minor
7307 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7308 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7309 // Tool major and minor
7310 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7311 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7312}
7313
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315 addConfigurationProperty("touch.deviceType", "touchScreen");
7316 prepareDisplay(DISPLAY_ORIENTATION_0);
7317 prepareAxes(POSITION | ID);
7318 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320
arthurhungdcef2dc2020-08-11 14:47:50 +08007321 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322
7323 NotifyMotionArgs motionArgs;
7324
7325 // Two fingers down at once.
7326 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7327 processPosition(mapper, x1, y1);
7328 processId(mapper, 1);
7329 processMTSync(mapper);
7330 processPosition(mapper, x2, y2);
7331 processId(mapper, 2);
7332 processMTSync(mapper);
7333 processSync(mapper);
7334
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7336 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7337 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7339 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7341 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7342
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007344 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007345 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7346 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7348 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7351 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7353 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7354
7355 // Move.
7356 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7357 processPosition(mapper, x1, y1);
7358 processId(mapper, 1);
7359 processMTSync(mapper);
7360 processPosition(mapper, x2, y2);
7361 processId(mapper, 2);
7362 processMTSync(mapper);
7363 processSync(mapper);
7364
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7367 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7370 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7373 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7376
7377 // First finger up.
7378 x2 += 15; y2 -= 20;
7379 processPosition(mapper, x2, y2);
7380 processId(mapper, 2);
7381 processMTSync(mapper);
7382 processSync(mapper);
7383
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007385 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007386 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7389 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7394 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7395
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7399 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7402 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7403
7404 // Move.
7405 x2 += 20; y2 -= 25;
7406 processPosition(mapper, x2, y2);
7407 processId(mapper, 2);
7408 processMTSync(mapper);
7409 processSync(mapper);
7410
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7413 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7414 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7418
7419 // New finger down.
7420 int32_t x3 = 700, y3 = 300;
7421 processPosition(mapper, x2, y2);
7422 processId(mapper, 2);
7423 processMTSync(mapper);
7424 processPosition(mapper, x3, y3);
7425 processId(mapper, 3);
7426 processMTSync(mapper);
7427 processSync(mapper);
7428
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007430 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007431 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7434 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7439 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7440
7441 // Second finger up.
7442 x3 += 30; y3 -= 20;
7443 processPosition(mapper, x3, y3);
7444 processId(mapper, 3);
7445 processMTSync(mapper);
7446 processSync(mapper);
7447
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007449 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007450 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7453 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7458 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7459
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7462 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7463 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7466 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7467
7468 // Last finger up.
7469 processMTSync(mapper);
7470 processSync(mapper);
7471
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7474 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7475 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7478 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7479
7480 // Should not have sent any more keys or motions.
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7483}
7484
7485TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007486 addConfigurationProperty("touch.deviceType", "touchScreen");
7487 prepareDisplay(DISPLAY_ORIENTATION_0);
7488 prepareAxes(POSITION | ID | SLOT);
7489 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007490 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491
arthurhungdcef2dc2020-08-11 14:47:50 +08007492 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493
7494 NotifyMotionArgs motionArgs;
7495
7496 // Two fingers down at once.
7497 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7498 processPosition(mapper, x1, y1);
7499 processId(mapper, 1);
7500 processSlot(mapper, 1);
7501 processPosition(mapper, x2, y2);
7502 processId(mapper, 2);
7503 processSync(mapper);
7504
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7511 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7512
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007514 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7518 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7521 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7523 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7524
7525 // Move.
7526 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7527 processSlot(mapper, 0);
7528 processPosition(mapper, x1, y1);
7529 processSlot(mapper, 1);
7530 processPosition(mapper, x2, y2);
7531 processSync(mapper);
7532
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7535 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7538 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7541 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7543 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7544
7545 // First finger up.
7546 x2 += 15; y2 -= 20;
7547 processSlot(mapper, 0);
7548 processId(mapper, -1);
7549 processSlot(mapper, 1);
7550 processPosition(mapper, x2, y2);
7551 processSync(mapper);
7552
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007554 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007555 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7558 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7561 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7563 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7564
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7568 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7571 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7572
7573 // Move.
7574 x2 += 20; y2 -= 25;
7575 processPosition(mapper, x2, y2);
7576 processSync(mapper);
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7581 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7585
7586 // New finger down.
7587 int32_t x3 = 700, y3 = 300;
7588 processPosition(mapper, x2, y2);
7589 processSlot(mapper, 0);
7590 processId(mapper, 3);
7591 processPosition(mapper, x3, y3);
7592 processSync(mapper);
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007595 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7597 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7599 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7604 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7605
7606 // Second finger up.
7607 x3 += 30; y3 -= 20;
7608 processSlot(mapper, 1);
7609 processId(mapper, -1);
7610 processSlot(mapper, 0);
7611 processPosition(mapper, x3, y3);
7612 processSync(mapper);
7613
7614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007615 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7617 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7619 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7620 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7622 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7624 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7633
7634 // Last finger up.
7635 processId(mapper, -1);
7636 processSync(mapper);
7637
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7644 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7645
7646 // Should not have sent any more keys or motions.
7647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7649}
7650
7651TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652 addConfigurationProperty("touch.deviceType", "touchScreen");
7653 prepareDisplay(DISPLAY_ORIENTATION_0);
7654 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656
7657 // These calculations are based on the input device calibration documentation.
7658 int32_t rawX = 100;
7659 int32_t rawY = 200;
7660 int32_t rawTouchMajor = 7;
7661 int32_t rawTouchMinor = 6;
7662 int32_t rawToolMajor = 9;
7663 int32_t rawToolMinor = 8;
7664 int32_t rawPressure = 11;
7665 int32_t rawDistance = 0;
7666 int32_t rawOrientation = 3;
7667 int32_t id = 5;
7668
7669 float x = toDisplayX(rawX);
7670 float y = toDisplayY(rawY);
7671 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7672 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7673 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7674 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7675 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7676 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7677 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7678 float distance = float(rawDistance);
7679
7680 processPosition(mapper, rawX, rawY);
7681 processTouchMajor(mapper, rawTouchMajor);
7682 processTouchMinor(mapper, rawTouchMinor);
7683 processToolMajor(mapper, rawToolMajor);
7684 processToolMinor(mapper, rawToolMinor);
7685 processPressure(mapper, rawPressure);
7686 processOrientation(mapper, rawOrientation);
7687 processDistance(mapper, rawDistance);
7688 processId(mapper, id);
7689 processMTSync(mapper);
7690 processSync(mapper);
7691
7692 NotifyMotionArgs args;
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7694 ASSERT_EQ(0, args.pointerProperties[0].id);
7695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7696 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7697 orientation, distance));
7698}
7699
7700TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701 addConfigurationProperty("touch.deviceType", "touchScreen");
7702 prepareDisplay(DISPLAY_ORIENTATION_0);
7703 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7704 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007705 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706
7707 // These calculations are based on the input device calibration documentation.
7708 int32_t rawX = 100;
7709 int32_t rawY = 200;
7710 int32_t rawTouchMajor = 140;
7711 int32_t rawTouchMinor = 120;
7712 int32_t rawToolMajor = 180;
7713 int32_t rawToolMinor = 160;
7714
7715 float x = toDisplayX(rawX);
7716 float y = toDisplayY(rawY);
7717 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7718 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7719 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7720 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7721 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7722
7723 processPosition(mapper, rawX, rawY);
7724 processTouchMajor(mapper, rawTouchMajor);
7725 processTouchMinor(mapper, rawTouchMinor);
7726 processToolMajor(mapper, rawToolMajor);
7727 processToolMinor(mapper, rawToolMinor);
7728 processMTSync(mapper);
7729 processSync(mapper);
7730
7731 NotifyMotionArgs args;
7732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7734 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7735}
7736
7737TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 addConfigurationProperty("touch.deviceType", "touchScreen");
7739 prepareDisplay(DISPLAY_ORIENTATION_0);
7740 prepareAxes(POSITION | TOUCH | TOOL);
7741 addConfigurationProperty("touch.size.calibration", "diameter");
7742 addConfigurationProperty("touch.size.scale", "10");
7743 addConfigurationProperty("touch.size.bias", "160");
7744 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746
7747 // These calculations are based on the input device calibration documentation.
7748 // Note: We only provide a single common touch/tool value because the device is assumed
7749 // not to emit separate values for each pointer (isSummed = 1).
7750 int32_t rawX = 100;
7751 int32_t rawY = 200;
7752 int32_t rawX2 = 150;
7753 int32_t rawY2 = 250;
7754 int32_t rawTouchMajor = 5;
7755 int32_t rawToolMajor = 8;
7756
7757 float x = toDisplayX(rawX);
7758 float y = toDisplayY(rawY);
7759 float x2 = toDisplayX(rawX2);
7760 float y2 = toDisplayY(rawY2);
7761 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7762 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7763 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7764
7765 processPosition(mapper, rawX, rawY);
7766 processTouchMajor(mapper, rawTouchMajor);
7767 processToolMajor(mapper, rawToolMajor);
7768 processMTSync(mapper);
7769 processPosition(mapper, rawX2, rawY2);
7770 processTouchMajor(mapper, rawTouchMajor);
7771 processToolMajor(mapper, rawToolMajor);
7772 processMTSync(mapper);
7773 processSync(mapper);
7774
7775 NotifyMotionArgs args;
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007780 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_EQ(size_t(2), args.pointerCount);
7782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7783 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7785 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7786}
7787
7788TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 addConfigurationProperty("touch.deviceType", "touchScreen");
7790 prepareDisplay(DISPLAY_ORIENTATION_0);
7791 prepareAxes(POSITION | TOUCH | TOOL);
7792 addConfigurationProperty("touch.size.calibration", "area");
7793 addConfigurationProperty("touch.size.scale", "43");
7794 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796
7797 // These calculations are based on the input device calibration documentation.
7798 int32_t rawX = 100;
7799 int32_t rawY = 200;
7800 int32_t rawTouchMajor = 5;
7801 int32_t rawToolMajor = 8;
7802
7803 float x = toDisplayX(rawX);
7804 float y = toDisplayY(rawY);
7805 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7806 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7807 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7808
7809 processPosition(mapper, rawX, rawY);
7810 processTouchMajor(mapper, rawTouchMajor);
7811 processToolMajor(mapper, rawToolMajor);
7812 processMTSync(mapper);
7813 processSync(mapper);
7814
7815 NotifyMotionArgs args;
7816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7818 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7819}
7820
7821TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 addConfigurationProperty("touch.deviceType", "touchScreen");
7823 prepareDisplay(DISPLAY_ORIENTATION_0);
7824 prepareAxes(POSITION | PRESSURE);
7825 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7826 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828
Michael Wrightaa449c92017-12-13 21:21:43 +00007829 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007830 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007831 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7832 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7833 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7834
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835 // These calculations are based on the input device calibration documentation.
7836 int32_t rawX = 100;
7837 int32_t rawY = 200;
7838 int32_t rawPressure = 60;
7839
7840 float x = toDisplayX(rawX);
7841 float y = toDisplayY(rawY);
7842 float pressure = float(rawPressure) * 0.01f;
7843
7844 processPosition(mapper, rawX, rawY);
7845 processPressure(mapper, rawPressure);
7846 processMTSync(mapper);
7847 processSync(mapper);
7848
7849 NotifyMotionArgs args;
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7852 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7853}
7854
7855TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
7857 prepareDisplay(DISPLAY_ORIENTATION_0);
7858 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860
7861 NotifyMotionArgs motionArgs;
7862 NotifyKeyArgs keyArgs;
7863
7864 processId(mapper, 1);
7865 processPosition(mapper, 100, 200);
7866 processSync(mapper);
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7869 ASSERT_EQ(0, motionArgs.buttonState);
7870
7871 // press BTN_LEFT, release BTN_LEFT
7872 processKey(mapper, BTN_LEFT, 1);
7873 processSync(mapper);
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7876 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7877
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7880 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7881
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882 processKey(mapper, BTN_LEFT, 0);
7883 processSync(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007887
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007890 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891
7892 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7893 processKey(mapper, BTN_RIGHT, 1);
7894 processKey(mapper, BTN_MIDDLE, 1);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7898 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7899 motionArgs.buttonState);
7900
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7903 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7904
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7907 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7908 motionArgs.buttonState);
7909
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 processKey(mapper, BTN_RIGHT, 0);
7911 processSync(mapper);
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007915
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007918 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919
7920 processKey(mapper, BTN_MIDDLE, 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_BACK, release BTN_BACK
7931 processKey(mapper, BTN_BACK, 1);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7934 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7935 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007936
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007939 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7940
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7943 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944
7945 processKey(mapper, BTN_BACK, 0);
7946 processSync(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007950
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007953 ASSERT_EQ(0, motionArgs.buttonState);
7954
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7956 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7957 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7958
7959 // press BTN_SIDE, release BTN_SIDE
7960 processKey(mapper, BTN_SIDE, 1);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7964 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007965
Michael Wrightd02c5b62014-02-10 15:10:22 -08007966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7972 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007973
7974 processKey(mapper, BTN_SIDE, 0);
7975 processSync(mapper);
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982 ASSERT_EQ(0, motionArgs.buttonState);
7983
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7985 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7986 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7987
7988 // press BTN_FORWARD, release BTN_FORWARD
7989 processKey(mapper, BTN_FORWARD, 1);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7992 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7993 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7998
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8000 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8001 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008002
8003 processKey(mapper, BTN_FORWARD, 0);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008008
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011 ASSERT_EQ(0, motionArgs.buttonState);
8012
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8014 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8015 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8016
8017 // press BTN_EXTRA, release BTN_EXTRA
8018 processKey(mapper, BTN_EXTRA, 1);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8021 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8022 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008023
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8030 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031
8032 processKey(mapper, BTN_EXTRA, 0);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_EQ(0, motionArgs.buttonState);
8041
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8043 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8044 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8045
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8047
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 // press BTN_STYLUS, release BTN_STYLUS
8049 processKey(mapper, BTN_STYLUS, 1);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8054
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8057 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058
8059 processKey(mapper, BTN_STYLUS, 0);
8060 processSync(mapper);
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008064
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068
8069 // press BTN_STYLUS2, release BTN_STYLUS2
8070 processKey(mapper, BTN_STYLUS2, 1);
8071 processSync(mapper);
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8075
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8078 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079
8080 processKey(mapper, BTN_STYLUS2, 0);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089
8090 // release touch
8091 processId(mapper, -1);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8095 ASSERT_EQ(0, motionArgs.buttonState);
8096}
8097
8098TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 addConfigurationProperty("touch.deviceType", "touchScreen");
8100 prepareDisplay(DISPLAY_ORIENTATION_0);
8101 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008102 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103
8104 NotifyMotionArgs motionArgs;
8105
8106 // default tool type is finger
8107 processId(mapper, 1);
8108 processPosition(mapper, 100, 200);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8113
8114 // eraser
8115 processKey(mapper, BTN_TOOL_RUBBER, 1);
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8120
8121 // stylus
8122 processKey(mapper, BTN_TOOL_RUBBER, 0);
8123 processKey(mapper, BTN_TOOL_PEN, 1);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8128
8129 // brush
8130 processKey(mapper, BTN_TOOL_PEN, 0);
8131 processKey(mapper, BTN_TOOL_BRUSH, 1);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8136
8137 // pencil
8138 processKey(mapper, BTN_TOOL_BRUSH, 0);
8139 processKey(mapper, BTN_TOOL_PENCIL, 1);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8144
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008145 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 processKey(mapper, BTN_TOOL_PENCIL, 0);
8147 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8152
8153 // mouse
8154 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8155 processKey(mapper, BTN_TOOL_MOUSE, 1);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8160
8161 // lens
8162 processKey(mapper, BTN_TOOL_MOUSE, 0);
8163 processKey(mapper, BTN_TOOL_LENS, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8168
8169 // double-tap
8170 processKey(mapper, BTN_TOOL_LENS, 0);
8171 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8176
8177 // triple-tap
8178 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8179 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8184
8185 // quad-tap
8186 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8187 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8192
8193 // finger
8194 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8195 processKey(mapper, BTN_TOOL_FINGER, 1);
8196 processSync(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8200
8201 // stylus trumps finger
8202 processKey(mapper, BTN_TOOL_PEN, 1);
8203 processSync(mapper);
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8207
8208 // eraser trumps stylus
8209 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
8214
8215 // mouse trumps eraser
8216 processKey(mapper, BTN_TOOL_MOUSE, 1);
8217 processSync(mapper);
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8221
8222 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8223 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8224 processSync(mapper);
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8228
8229 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8230 processToolType(mapper, MT_TOOL_PEN);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8235
8236 // back to default tool type
8237 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8238 processKey(mapper, BTN_TOOL_MOUSE, 0);
8239 processKey(mapper, BTN_TOOL_RUBBER, 0);
8240 processKey(mapper, BTN_TOOL_PEN, 0);
8241 processKey(mapper, BTN_TOOL_FINGER, 0);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8246}
8247
8248TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 addConfigurationProperty("touch.deviceType", "touchScreen");
8250 prepareDisplay(DISPLAY_ORIENTATION_0);
8251 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008252 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254
8255 NotifyMotionArgs motionArgs;
8256
8257 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8258 processId(mapper, 1);
8259 processPosition(mapper, 100, 200);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8264 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8269 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8270
8271 // move a little
8272 processPosition(mapper, 150, 250);
8273 processSync(mapper);
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8277 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8278
8279 // down when BTN_TOUCH is pressed, pressure defaults to 1
8280 processKey(mapper, BTN_TOUCH, 1);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8285 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8290 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8291
8292 // up when BTN_TOUCH is released, hover restored
8293 processKey(mapper, BTN_TOUCH, 0);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8299
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8303 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8304
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8309
8310 // exit hover when pointer goes away
8311 processId(mapper, -1);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8316 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8317}
8318
8319TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 addConfigurationProperty("touch.deviceType", "touchScreen");
8321 prepareDisplay(DISPLAY_ORIENTATION_0);
8322 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324
8325 NotifyMotionArgs motionArgs;
8326
8327 // initially hovering because pressure is 0
8328 processId(mapper, 1);
8329 processPosition(mapper, 100, 200);
8330 processPressure(mapper, 0);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8341
8342 // move a little
8343 processPosition(mapper, 150, 250);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8349
8350 // down when pressure becomes non-zero
8351 processPressure(mapper, RAW_PRESSURE_MAX);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8356 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8357
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8361 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8362
8363 // up when pressure becomes 0, hover restored
8364 processPressure(mapper, 0);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8370
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8375
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8380
8381 // exit hover when pointer goes away
8382 processId(mapper, -1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8387 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8388}
8389
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008390/**
8391 * Set the input device port <--> display port associations, and check that the
8392 * events are routed to the display that matches the display port.
8393 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8394 */
8395TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008396 const std::string usb2 = "USB2";
8397 const uint8_t hdmi1 = 0;
8398 const uint8_t hdmi2 = 1;
8399 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008400 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008401
8402 addConfigurationProperty("touch.deviceType", "touchScreen");
8403 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008404 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008405
8406 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8407 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8408
8409 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8410 // for this input device is specified, and the matching viewport is not present,
8411 // the input device should be disabled (at the mapper level).
8412
8413 // Add viewport for display 2 on hdmi2
8414 prepareSecondaryDisplay(type, hdmi2);
8415 // Send a touch event
8416 processPosition(mapper, 100, 100);
8417 processSync(mapper);
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8419
8420 // Add viewport for display 1 on hdmi1
8421 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8422 // Send a touch event again
8423 processPosition(mapper, 100, 100);
8424 processSync(mapper);
8425
8426 NotifyMotionArgs args;
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8428 ASSERT_EQ(DISPLAY_ID, args.displayId);
8429}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008431TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8432 addConfigurationProperty("touch.deviceType", "touchScreen");
8433 prepareAxes(POSITION);
8434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8435
8436 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8437
8438 prepareDisplay(DISPLAY_ORIENTATION_0);
8439 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8440
8441 // Send a touch event
8442 processPosition(mapper, 100, 100);
8443 processSync(mapper);
8444
8445 NotifyMotionArgs args;
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8447 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8448}
8449
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008450TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008451 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008452 std::shared_ptr<FakePointerController> fakePointerController =
8453 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008454 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008455 fakePointerController->setPosition(100, 200);
8456 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008457 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008458
Garfield Tan888a6a42020-01-09 11:39:16 -08008459 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008460 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008461
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008462 prepareDisplay(DISPLAY_ORIENTATION_0);
8463 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008465
8466 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008467 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008468
8469 NotifyMotionArgs motionArgs;
8470 processPosition(mapper, 100, 100);
8471 processSync(mapper);
8472
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8475 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8476}
8477
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008478/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008479 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8480 */
8481TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8482 addConfigurationProperty("touch.deviceType", "touchScreen");
8483 prepareAxes(POSITION);
8484 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8485
8486 prepareDisplay(DISPLAY_ORIENTATION_0);
8487 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8488 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8489 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8490 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8491
8492 NotifyMotionArgs args;
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8494 ASSERT_EQ(26, args.readTime);
8495
8496 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8497 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8498 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8501 ASSERT_EQ(33, args.readTime);
8502}
8503
8504/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008505 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8506 * events should not be delivered to the listener.
8507 */
8508TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8509 addConfigurationProperty("touch.deviceType", "touchScreen");
8510 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8511 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8512 ViewportType::INTERNAL);
8513 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8514 prepareAxes(POSITION);
8515 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8516
8517 NotifyMotionArgs motionArgs;
8518 processPosition(mapper, 100, 100);
8519 processSync(mapper);
8520
8521 mFakeListener->assertNotifyMotionWasNotCalled();
8522}
8523
Garfield Tanc734e4f2021-01-15 20:01:39 -08008524TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8525 addConfigurationProperty("touch.deviceType", "touchScreen");
8526 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8527 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8528 ViewportType::INTERNAL);
8529 std::optional<DisplayViewport> optionalDisplayViewport =
8530 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8531 ASSERT_TRUE(optionalDisplayViewport.has_value());
8532 DisplayViewport displayViewport = *optionalDisplayViewport;
8533
8534 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8535 prepareAxes(POSITION);
8536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8537
8538 // Finger down
8539 int32_t x = 100, y = 100;
8540 processPosition(mapper, x, y);
8541 processSync(mapper);
8542
8543 NotifyMotionArgs motionArgs;
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8546
8547 // Deactivate display viewport
8548 displayViewport.isActive = false;
8549 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8550 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8551
8552 // Finger move
8553 x += 10, y += 10;
8554 processPosition(mapper, x, y);
8555 processSync(mapper);
8556
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8559
8560 // Reactivate display viewport
8561 displayViewport.isActive = true;
8562 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8563 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8564
8565 // Finger move again
8566 x += 10, y += 10;
8567 processPosition(mapper, x, y);
8568 processSync(mapper);
8569
8570 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8571 // no pointer on the touch device.
8572 mFakeListener->assertNotifyMotionWasNotCalled();
8573}
8574
Arthur Hung7c645402019-01-25 17:45:42 +08008575TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8576 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008577 prepareAxes(POSITION | ID | SLOT);
8578 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008579 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008580
8581 // Create the second touch screen device, and enable multi fingers.
8582 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008583 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008584 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008585 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008586 std::shared_ptr<InputDevice> device2 =
8587 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008588 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008589
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008590 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8591 0 /*flat*/, 0 /*fuzz*/);
8592 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8593 0 /*flat*/, 0 /*fuzz*/);
8594 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8595 0 /*flat*/, 0 /*fuzz*/);
8596 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8597 0 /*flat*/, 0 /*fuzz*/);
8598 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8599 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8600 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008601
8602 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008603 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008604 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8605 device2->reset(ARBITRARY_TIME);
8606
8607 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008608 std::shared_ptr<FakePointerController> fakePointerController =
8609 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008610 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008611
8612 // Setup policy for associated displays and show touches.
8613 const uint8_t hdmi1 = 0;
8614 const uint8_t hdmi2 = 1;
8615 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8616 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8617 mFakePolicy->setShowTouches(true);
8618
8619 // Create displays.
8620 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008621 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008622
8623 // Default device will reconfigure above, need additional reconfiguration for another device.
8624 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008625 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8626 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008627
8628 // Two fingers down at default display.
8629 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8630 processPosition(mapper, x1, y1);
8631 processId(mapper, 1);
8632 processSlot(mapper, 1);
8633 processPosition(mapper, x2, y2);
8634 processId(mapper, 2);
8635 processSync(mapper);
8636
8637 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8638 fakePointerController->getSpots().find(DISPLAY_ID);
8639 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8640 ASSERT_EQ(size_t(2), iter->second.size());
8641
8642 // Two fingers down at second display.
8643 processPosition(mapper2, x1, y1);
8644 processId(mapper2, 1);
8645 processSlot(mapper2, 1);
8646 processPosition(mapper2, x2, y2);
8647 processId(mapper2, 2);
8648 processSync(mapper2);
8649
8650 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8651 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8652 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008653
8654 // Disable the show touches configuration and ensure the spots are cleared.
8655 mFakePolicy->setShowTouches(false);
8656 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8657 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8658
8659 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008660}
8661
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008662TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008663 prepareAxes(POSITION);
8664 addConfigurationProperty("touch.deviceType", "touchScreen");
8665 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008667
8668 NotifyMotionArgs motionArgs;
8669 // Unrotated video frame
8670 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8671 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008672 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008673 processPosition(mapper, 100, 200);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(frames, motionArgs.videoFrames);
8677
8678 // Subsequent touch events should not have any videoframes
8679 // This is implemented separately in FakeEventHub,
8680 // but that should match the behaviour of TouchVideoDevice.
8681 processPosition(mapper, 200, 200);
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8685}
8686
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008687TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008688 prepareAxes(POSITION);
8689 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008691 // Unrotated video frame
8692 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8693 NotifyMotionArgs motionArgs;
8694
8695 // Test all 4 orientations
8696 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008697 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8698 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8699 clearViewports();
8700 prepareDisplay(orientation);
8701 std::vector<TouchVideoFrame> frames{frame};
8702 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8703 processPosition(mapper, 100, 200);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8706 ASSERT_EQ(frames, motionArgs.videoFrames);
8707 }
8708}
8709
8710TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8711 prepareAxes(POSITION);
8712 addConfigurationProperty("touch.deviceType", "touchScreen");
8713 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8714 // orientation-aware are affected by display rotation.
8715 addConfigurationProperty("touch.orientationAware", "0");
8716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8717 // Unrotated video frame
8718 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8719 NotifyMotionArgs motionArgs;
8720
8721 // Test all 4 orientations
8722 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008723 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8724 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8725 clearViewports();
8726 prepareDisplay(orientation);
8727 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008728 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008729 processPosition(mapper, 100, 200);
8730 processSync(mapper);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008732 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8733 // compared to the display. This is so that when the window transform (which contains the
8734 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8735 // window's coordinate space.
8736 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008737 ASSERT_EQ(frames, motionArgs.videoFrames);
8738 }
8739}
8740
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008741TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008742 prepareAxes(POSITION);
8743 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008745 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8746 // so mix these.
8747 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8748 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8749 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8750 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8751 NotifyMotionArgs motionArgs;
8752
8753 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008754 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008755 processPosition(mapper, 100, 200);
8756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008758 ASSERT_EQ(frames, motionArgs.videoFrames);
8759}
8760
8761TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8762 prepareAxes(POSITION);
8763 addConfigurationProperty("touch.deviceType", "touchScreen");
8764 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8765 // orientation-aware are affected by display rotation.
8766 addConfigurationProperty("touch.orientationAware", "0");
8767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8768 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8769 // so mix these.
8770 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8771 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8772 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8773 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8774 NotifyMotionArgs motionArgs;
8775
8776 prepareDisplay(DISPLAY_ORIENTATION_90);
8777 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8778 processPosition(mapper, 100, 200);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8782 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8783 // compared to the display. This is so that when the window transform (which contains the
8784 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8785 // window's coordinate space.
8786 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8787 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008788 ASSERT_EQ(frames, motionArgs.videoFrames);
8789}
8790
Arthur Hung9da14732019-09-02 16:16:58 +08008791/**
8792 * If we had defined port associations, but the viewport is not ready, the touch device would be
8793 * expected to be disabled, and it should be enabled after the viewport has found.
8794 */
8795TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008796 constexpr uint8_t hdmi2 = 1;
8797 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008798 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008799
8800 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8801
8802 addConfigurationProperty("touch.deviceType", "touchScreen");
8803 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008805
8806 ASSERT_EQ(mDevice->isEnabled(), false);
8807
8808 // Add display on hdmi2, the device should be enabled and can receive touch event.
8809 prepareSecondaryDisplay(type, hdmi2);
8810 ASSERT_EQ(mDevice->isEnabled(), true);
8811
8812 // Send a touch event.
8813 processPosition(mapper, 100, 100);
8814 processSync(mapper);
8815
8816 NotifyMotionArgs args;
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8818 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8819}
8820
Arthur Hung421eb1c2020-01-16 00:09:42 +08008821TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008822 addConfigurationProperty("touch.deviceType", "touchScreen");
8823 prepareDisplay(DISPLAY_ORIENTATION_0);
8824 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008825 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008826
8827 NotifyMotionArgs motionArgs;
8828
8829 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8830 // finger down
8831 processId(mapper, 1);
8832 processPosition(mapper, x1, y1);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8837
8838 // finger move
8839 processId(mapper, 1);
8840 processPosition(mapper, x2, y2);
8841 processSync(mapper);
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8845
8846 // finger up.
8847 processId(mapper, -1);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8850 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8852
8853 // new finger down
8854 processId(mapper, 1);
8855 processPosition(mapper, x3, y3);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8858 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8860}
8861
8862/**
arthurhungcc7f9802020-04-30 17:55:40 +08008863 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8864 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008865 */
arthurhungcc7f9802020-04-30 17:55:40 +08008866TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008867 addConfigurationProperty("touch.deviceType", "touchScreen");
8868 prepareDisplay(DISPLAY_ORIENTATION_0);
8869 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008871
8872 NotifyMotionArgs motionArgs;
8873
8874 // default tool type is finger
8875 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008876 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008877 processPosition(mapper, x1, y1);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8881 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8882
8883 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8884 processToolType(mapper, MT_TOOL_PALM);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8888
8889 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008890 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008891 processPosition(mapper, x2, y2);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8894
8895 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008896 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008897 processSync(mapper);
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8899
8900 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008901 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008902 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008903 processPosition(mapper, x3, y3);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8908}
8909
arthurhungbf89a482020-04-17 17:37:55 +08008910/**
arthurhungcc7f9802020-04-30 17:55:40 +08008911 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8912 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008913 */
arthurhungcc7f9802020-04-30 17:55:40 +08008914TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008915 addConfigurationProperty("touch.deviceType", "touchScreen");
8916 prepareDisplay(DISPLAY_ORIENTATION_0);
8917 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8919
8920 NotifyMotionArgs motionArgs;
8921
8922 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008923 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8924 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008925 processPosition(mapper, x1, y1);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8930
8931 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008932 processSlot(mapper, SECOND_SLOT);
8933 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008934 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008937 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8939
8940 // If the tool type of the first finger changes to MT_TOOL_PALM,
8941 // we expect to receive ACTION_POINTER_UP with cancel flag.
8942 processSlot(mapper, FIRST_SLOT);
8943 processId(mapper, FIRST_TRACKING_ID);
8944 processToolType(mapper, MT_TOOL_PALM);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008947 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008948 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8949
8950 // The following MOVE events of second finger should be processed.
8951 processSlot(mapper, SECOND_SLOT);
8952 processId(mapper, SECOND_TRACKING_ID);
8953 processPosition(mapper, x2 + 1, y2 + 1);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8957 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8958
8959 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8960 // it. Second finger receive move.
8961 processSlot(mapper, FIRST_SLOT);
8962 processId(mapper, INVALID_TRACKING_ID);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8966 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8967
8968 // Second finger keeps moving.
8969 processSlot(mapper, SECOND_SLOT);
8970 processId(mapper, SECOND_TRACKING_ID);
8971 processPosition(mapper, x2 + 2, y2 + 2);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8975 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8976
8977 // Second finger up.
8978 processId(mapper, INVALID_TRACKING_ID);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8982 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8983}
8984
8985/**
8986 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8987 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8988 */
8989TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8990 addConfigurationProperty("touch.deviceType", "touchScreen");
8991 prepareDisplay(DISPLAY_ORIENTATION_0);
8992 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8993 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8994
8995 NotifyMotionArgs motionArgs;
8996
8997 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8998 // First finger down.
8999 processId(mapper, FIRST_TRACKING_ID);
9000 processPosition(mapper, x1, y1);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9005
9006 // Second finger down.
9007 processSlot(mapper, SECOND_SLOT);
9008 processId(mapper, SECOND_TRACKING_ID);
9009 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009012 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9014
arthurhungcc7f9802020-04-30 17:55:40 +08009015 // If the tool type of the first finger changes to MT_TOOL_PALM,
9016 // we expect to receive ACTION_POINTER_UP with cancel flag.
9017 processSlot(mapper, FIRST_SLOT);
9018 processId(mapper, FIRST_TRACKING_ID);
9019 processToolType(mapper, MT_TOOL_PALM);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009022 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009023 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9024
9025 // Second finger keeps moving.
9026 processSlot(mapper, SECOND_SLOT);
9027 processId(mapper, SECOND_TRACKING_ID);
9028 processPosition(mapper, x2 + 1, y2 + 1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9032
9033 // second finger becomes palm, receive cancel due to only 1 finger is active.
9034 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009035 processToolType(mapper, MT_TOOL_PALM);
9036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9038 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9039
arthurhungcc7f9802020-04-30 17:55:40 +08009040 // third finger down.
9041 processSlot(mapper, THIRD_SLOT);
9042 processId(mapper, THIRD_TRACKING_ID);
9043 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009044 processPosition(mapper, x3, y3);
9045 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009049 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9050
9051 // third finger move
9052 processId(mapper, THIRD_TRACKING_ID);
9053 processPosition(mapper, x3 + 1, y3 + 1);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9057
9058 // first finger up, third finger receive move.
9059 processSlot(mapper, FIRST_SLOT);
9060 processId(mapper, INVALID_TRACKING_ID);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9064 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9065
9066 // second finger up, third finger receive move.
9067 processSlot(mapper, SECOND_SLOT);
9068 processId(mapper, INVALID_TRACKING_ID);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9072 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9073
9074 // third finger up.
9075 processSlot(mapper, THIRD_SLOT);
9076 processId(mapper, INVALID_TRACKING_ID);
9077 processSync(mapper);
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9079 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9080 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9081}
9082
9083/**
9084 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9085 * and the active finger could still be allowed to receive the events
9086 */
9087TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9088 addConfigurationProperty("touch.deviceType", "touchScreen");
9089 prepareDisplay(DISPLAY_ORIENTATION_0);
9090 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9091 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9092
9093 NotifyMotionArgs motionArgs;
9094
9095 // default tool type is finger
9096 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9097 processId(mapper, FIRST_TRACKING_ID);
9098 processPosition(mapper, x1, y1);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9103
9104 // Second finger down.
9105 processSlot(mapper, SECOND_SLOT);
9106 processId(mapper, SECOND_TRACKING_ID);
9107 processPosition(mapper, x2, y2);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009110 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9112
9113 // If the tool type of the second finger changes to MT_TOOL_PALM,
9114 // we expect to receive ACTION_POINTER_UP with cancel flag.
9115 processId(mapper, SECOND_TRACKING_ID);
9116 processToolType(mapper, MT_TOOL_PALM);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009119 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009120 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9121
9122 // The following MOVE event should be processed.
9123 processSlot(mapper, FIRST_SLOT);
9124 processId(mapper, FIRST_TRACKING_ID);
9125 processPosition(mapper, x1 + 1, y1 + 1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9129 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9130
9131 // second finger up.
9132 processSlot(mapper, SECOND_SLOT);
9133 processId(mapper, INVALID_TRACKING_ID);
9134 processSync(mapper);
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9137
9138 // first finger keep moving
9139 processSlot(mapper, FIRST_SLOT);
9140 processId(mapper, FIRST_TRACKING_ID);
9141 processPosition(mapper, x1 + 2, y1 + 2);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9145
9146 // first finger up.
9147 processId(mapper, INVALID_TRACKING_ID);
9148 processSync(mapper);
9149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9150 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9151 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009152}
9153
Arthur Hung9ad18942021-06-19 02:04:46 +00009154/**
9155 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9156 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9157 * cause slot be valid again.
9158 */
9159TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9160 addConfigurationProperty("touch.deviceType", "touchScreen");
9161 prepareDisplay(DISPLAY_ORIENTATION_0);
9162 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9163 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9164
9165 NotifyMotionArgs motionArgs;
9166
9167 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9168 // First finger down.
9169 processId(mapper, FIRST_TRACKING_ID);
9170 processPosition(mapper, x1, y1);
9171 processPressure(mapper, RAW_PRESSURE_MAX);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9175 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9176
9177 // First finger move.
9178 processId(mapper, FIRST_TRACKING_ID);
9179 processPosition(mapper, x1 + 1, y1 + 1);
9180 processPressure(mapper, RAW_PRESSURE_MAX);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9184 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9185
9186 // Second finger down.
9187 processSlot(mapper, SECOND_SLOT);
9188 processId(mapper, SECOND_TRACKING_ID);
9189 processPosition(mapper, x2, y2);
9190 processPressure(mapper, RAW_PRESSURE_MAX);
9191 processSync(mapper);
9192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009193 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009194 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9195
9196 // second finger up with some unexpected data.
9197 processSlot(mapper, SECOND_SLOT);
9198 processId(mapper, INVALID_TRACKING_ID);
9199 processPosition(mapper, x2, y2);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009202 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009203 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9204
9205 // first finger up with some unexpected data.
9206 processSlot(mapper, FIRST_SLOT);
9207 processId(mapper, INVALID_TRACKING_ID);
9208 processPosition(mapper, x2, y2);
9209 processPressure(mapper, RAW_PRESSURE_MAX);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9213 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9214}
9215
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009216// --- MultiTouchInputMapperTest_ExternalDevice ---
9217
9218class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9219protected:
Chris Yea52ade12020-08-27 16:49:20 -07009220 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009221};
9222
9223/**
9224 * Expect fallback to internal viewport if device is external and external viewport is not present.
9225 */
9226TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9227 prepareAxes(POSITION);
9228 addConfigurationProperty("touch.deviceType", "touchScreen");
9229 prepareDisplay(DISPLAY_ORIENTATION_0);
9230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9231
9232 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9233
9234 NotifyMotionArgs motionArgs;
9235
9236 // Expect the event to be sent to the internal viewport,
9237 // because an external viewport is not present.
9238 processPosition(mapper, 100, 100);
9239 processSync(mapper);
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9241 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9242
9243 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009244 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009245 processPosition(mapper, 100, 100);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9249}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009250
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009251TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9252 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9253 std::shared_ptr<FakePointerController> fakePointerController =
9254 std::make_shared<FakePointerController>();
9255 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9256 fakePointerController->setPosition(0, 0);
9257 fakePointerController->setButtonState(0);
9258
9259 // prepare device and capture
9260 prepareDisplay(DISPLAY_ORIENTATION_0);
9261 prepareAxes(POSITION | ID | SLOT);
9262 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9263 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9264 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009265 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9267
9268 // captured touchpad should be a touchpad source
9269 NotifyDeviceResetArgs resetArgs;
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9271 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9272
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009273 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009274
9275 const InputDeviceInfo::MotionRange* relRangeX =
9276 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9277 ASSERT_NE(relRangeX, nullptr);
9278 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9279 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9280 const InputDeviceInfo::MotionRange* relRangeY =
9281 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9282 ASSERT_NE(relRangeY, nullptr);
9283 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9284 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9285
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009286 // run captured pointer tests - note that this is unscaled, so input listener events should be
9287 // identical to what the hardware sends (accounting for any
9288 // calibration).
9289 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009290 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009291 processId(mapper, 1);
9292 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9293 processKey(mapper, BTN_TOUCH, 1);
9294 processSync(mapper);
9295
9296 // expect coord[0] to contain initial location of touch 0
9297 NotifyMotionArgs args;
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9300 ASSERT_EQ(1U, args.pointerCount);
9301 ASSERT_EQ(0, args.pointerProperties[0].id);
9302 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9303 ASSERT_NO_FATAL_FAILURE(
9304 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9305
9306 // FINGER 1 DOWN
9307 processSlot(mapper, 1);
9308 processId(mapper, 2);
9309 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9310 processSync(mapper);
9311
9312 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009314 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009315 ASSERT_EQ(2U, args.pointerCount);
9316 ASSERT_EQ(0, args.pointerProperties[0].id);
9317 ASSERT_EQ(1, args.pointerProperties[1].id);
9318 ASSERT_NO_FATAL_FAILURE(
9319 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9320 ASSERT_NO_FATAL_FAILURE(
9321 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9322
9323 // FINGER 1 MOVE
9324 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9325 processSync(mapper);
9326
9327 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9328 // from move
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9331 ASSERT_NO_FATAL_FAILURE(
9332 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9333 ASSERT_NO_FATAL_FAILURE(
9334 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9335
9336 // FINGER 0 MOVE
9337 processSlot(mapper, 0);
9338 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9339 processSync(mapper);
9340
9341 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9344 ASSERT_NO_FATAL_FAILURE(
9345 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9346 ASSERT_NO_FATAL_FAILURE(
9347 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9348
9349 // BUTTON DOWN
9350 processKey(mapper, BTN_LEFT, 1);
9351 processSync(mapper);
9352
9353 // touchinputmapper design sends a move before button press
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9358
9359 // BUTTON UP
9360 processKey(mapper, BTN_LEFT, 0);
9361 processSync(mapper);
9362
9363 // touchinputmapper design sends a move after button release
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9368
9369 // FINGER 0 UP
9370 processId(mapper, -1);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9373 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9374
9375 // FINGER 1 MOVE
9376 processSlot(mapper, 1);
9377 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9378 processSync(mapper);
9379
9380 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9383 ASSERT_EQ(1U, args.pointerCount);
9384 ASSERT_EQ(1, args.pointerProperties[0].id);
9385 ASSERT_NO_FATAL_FAILURE(
9386 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9387
9388 // FINGER 1 UP
9389 processId(mapper, -1);
9390 processKey(mapper, BTN_TOUCH, 0);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9394
9395 // non captured touchpad should be a mouse source
9396 mFakePolicy->setPointerCapture(false);
9397 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9399 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9400}
9401
9402TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9403 std::shared_ptr<FakePointerController> fakePointerController =
9404 std::make_shared<FakePointerController>();
9405 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9406 fakePointerController->setPosition(0, 0);
9407 fakePointerController->setButtonState(0);
9408
9409 // prepare device and capture
9410 prepareDisplay(DISPLAY_ORIENTATION_0);
9411 prepareAxes(POSITION | ID | SLOT);
9412 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9413 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009414 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9416 // run uncaptured pointer tests - pushes out generic events
9417 // FINGER 0 DOWN
9418 processId(mapper, 3);
9419 processPosition(mapper, 100, 100);
9420 processKey(mapper, BTN_TOUCH, 1);
9421 processSync(mapper);
9422
9423 // start at (100,100), cursor should be at (0,0) * scale
9424 NotifyMotionArgs args;
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9427 ASSERT_NO_FATAL_FAILURE(
9428 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9429
9430 // FINGER 0 MOVE
9431 processPosition(mapper, 200, 200);
9432 processSync(mapper);
9433
9434 // compute scaling to help with touch position checking
9435 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9436 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9437 float scale =
9438 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9439
9440 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9444 0, 0, 0, 0, 0, 0, 0));
9445}
9446
9447TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9448 std::shared_ptr<FakePointerController> fakePointerController =
9449 std::make_shared<FakePointerController>();
9450
9451 prepareDisplay(DISPLAY_ORIENTATION_0);
9452 prepareAxes(POSITION | ID | SLOT);
9453 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009454 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009455 mFakePolicy->setPointerCapture(false);
9456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9457
9458 // uncaptured touchpad should be a pointer device
9459 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9460
9461 // captured touchpad should be a touchpad device
9462 mFakePolicy->setPointerCapture(true);
9463 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9464 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9465}
9466
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009467// --- JoystickInputMapperTest ---
9468
9469class JoystickInputMapperTest : public InputMapperTest {
9470protected:
9471 static const int32_t RAW_X_MIN;
9472 static const int32_t RAW_X_MAX;
9473 static const int32_t RAW_Y_MIN;
9474 static const int32_t RAW_Y_MAX;
9475
9476 void SetUp() override {
9477 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9478 }
9479 void prepareAxes() {
9480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9482 }
9483
9484 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9486 }
9487
9488 void processSync(JoystickInputMapper& mapper) {
9489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9490 }
9491
9492 void prepareVirtualDisplay(int32_t orientation) {
9493 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9494 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9495 NO_PORT, ViewportType::VIRTUAL);
9496 }
9497};
9498
9499const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9500const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9501const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9502const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9503
9504TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9505 prepareAxes();
9506 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9507
9508 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9509
9510 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9511
9512 // Send an axis event
9513 processAxis(mapper, ABS_X, 100);
9514 processSync(mapper);
9515
9516 NotifyMotionArgs args;
9517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9518 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9519
9520 // Send another axis event
9521 processAxis(mapper, ABS_Y, 100);
9522 processSync(mapper);
9523
9524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9525 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9526}
9527
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009528// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009529
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009530class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009531protected:
9532 static const char* DEVICE_NAME;
9533 static const char* DEVICE_LOCATION;
9534 static const int32_t DEVICE_ID;
9535 static const int32_t DEVICE_GENERATION;
9536 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009537 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009538 static const int32_t EVENTHUB_ID;
9539
9540 std::shared_ptr<FakeEventHub> mFakeEventHub;
9541 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009542 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009543 std::unique_ptr<InstrumentedInputReader> mReader;
9544 std::shared_ptr<InputDevice> mDevice;
9545
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009546 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009547 mFakeEventHub = std::make_unique<FakeEventHub>();
9548 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009549 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009550 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009551 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009552 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9553 }
9554
9555 void SetUp() override { SetUp(DEVICE_CLASSES); }
9556
9557 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009558 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009559 mFakePolicy.clear();
9560 }
9561
9562 void configureDevice(uint32_t changes) {
9563 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9564 mReader->requestRefreshConfiguration(changes);
9565 mReader->loopOnce();
9566 }
9567 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9568 }
9569
9570 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9571 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009572 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009573 InputDeviceIdentifier identifier;
9574 identifier.name = name;
9575 identifier.location = location;
9576 std::shared_ptr<InputDevice> device =
9577 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9578 identifier);
9579 mReader->pushNextDevice(device);
9580 mFakeEventHub->addDevice(eventHubId, name, classes);
9581 mReader->loopOnce();
9582 return device;
9583 }
9584
9585 template <class T, typename... Args>
9586 T& addControllerAndConfigure(Args... args) {
9587 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9588
9589 return controller;
9590 }
9591};
9592
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009593const char* PeripheralControllerTest::DEVICE_NAME = "device";
9594const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9595const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9596const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9597const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009598const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9599 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009600const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009601
9602// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009603class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009604protected:
9605 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009606 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009607 }
9608};
9609
9610TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009611 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009612
9613 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9614 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9615}
9616
9617TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009618 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009619
9620 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9621 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9622}
9623
9624// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009625class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009626protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009627 void SetUp() override {
9628 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9629 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009630};
9631
Chris Ye85758332021-05-16 23:05:17 -07009632TEST_F(LightControllerTest, MonoLight) {
9633 RawLightInfo infoMono = {.id = 1,
9634 .name = "Mono",
9635 .maxBrightness = 255,
9636 .flags = InputLightClass::BRIGHTNESS,
9637 .path = ""};
9638 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009639
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009640 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009641 InputDeviceInfo info;
9642 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009643 std::vector<InputDeviceLightInfo> lights = info.getLights();
9644 ASSERT_EQ(1U, lights.size());
9645 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009646
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009647 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9648 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009649}
9650
9651TEST_F(LightControllerTest, RGBLight) {
9652 RawLightInfo infoRed = {.id = 1,
9653 .name = "red",
9654 .maxBrightness = 255,
9655 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9656 .path = ""};
9657 RawLightInfo infoGreen = {.id = 2,
9658 .name = "green",
9659 .maxBrightness = 255,
9660 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9661 .path = ""};
9662 RawLightInfo infoBlue = {.id = 3,
9663 .name = "blue",
9664 .maxBrightness = 255,
9665 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9666 .path = ""};
9667 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9668 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9669 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9670
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009671 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009672 InputDeviceInfo info;
9673 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009674 std::vector<InputDeviceLightInfo> lights = info.getLights();
9675 ASSERT_EQ(1U, lights.size());
9676 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009677
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009678 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9679 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009680}
9681
9682TEST_F(LightControllerTest, MultiColorRGBLight) {
9683 RawLightInfo infoColor = {.id = 1,
9684 .name = "red",
9685 .maxBrightness = 255,
9686 .flags = InputLightClass::BRIGHTNESS |
9687 InputLightClass::MULTI_INTENSITY |
9688 InputLightClass::MULTI_INDEX,
9689 .path = ""};
9690
9691 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9692
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009693 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009694 InputDeviceInfo info;
9695 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009696 std::vector<InputDeviceLightInfo> lights = info.getLights();
9697 ASSERT_EQ(1U, lights.size());
9698 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009699
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009700 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9701 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009702}
9703
9704TEST_F(LightControllerTest, PlayerIdLight) {
9705 RawLightInfo info1 = {.id = 1,
9706 .name = "player1",
9707 .maxBrightness = 255,
9708 .flags = InputLightClass::BRIGHTNESS,
9709 .path = ""};
9710 RawLightInfo info2 = {.id = 2,
9711 .name = "player2",
9712 .maxBrightness = 255,
9713 .flags = InputLightClass::BRIGHTNESS,
9714 .path = ""};
9715 RawLightInfo info3 = {.id = 3,
9716 .name = "player3",
9717 .maxBrightness = 255,
9718 .flags = InputLightClass::BRIGHTNESS,
9719 .path = ""};
9720 RawLightInfo info4 = {.id = 4,
9721 .name = "player4",
9722 .maxBrightness = 255,
9723 .flags = InputLightClass::BRIGHTNESS,
9724 .path = ""};
9725 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9726 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9727 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9728 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9729
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009730 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009731 InputDeviceInfo info;
9732 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009733 std::vector<InputDeviceLightInfo> lights = info.getLights();
9734 ASSERT_EQ(1U, lights.size());
9735 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009736
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009737 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9738 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9739 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009740}
9741
Michael Wrightd02c5b62014-02-10 15:10:22 -08009742} // namespace android