blob: 13801125d2a0ae3d38ebe1f77093898ef78a0a33 [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
Chris Yea52ade12020-08-27 16:49:20 -0700222 void clearSpots() override {}
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
549 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
550 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.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("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
2755 String8 propertyValue;
2756 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2757 << "Device should have read configuration during configuration phase.";
2758 ASSERT_STREQ("value", propertyValue.string());
2759
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) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002956 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(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
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004974TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004976
Garfield Tan888a6a42020-01-09 11:39:16 -08004977 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004978 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004979 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4980 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004981 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4982 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004983 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4984 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4985
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004986 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4987 mFakePointerController->setPosition(100, 200);
4988 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004989
4990 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4995 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4996 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4998 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 +01004999 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005000 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5001}
5002
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003// --- TouchInputMapperTest ---
5004
5005class TouchInputMapperTest : public InputMapperTest {
5006protected:
5007 static const int32_t RAW_X_MIN;
5008 static const int32_t RAW_X_MAX;
5009 static const int32_t RAW_Y_MIN;
5010 static const int32_t RAW_Y_MAX;
5011 static const int32_t RAW_TOUCH_MIN;
5012 static const int32_t RAW_TOUCH_MAX;
5013 static const int32_t RAW_TOOL_MIN;
5014 static const int32_t RAW_TOOL_MAX;
5015 static const int32_t RAW_PRESSURE_MIN;
5016 static const int32_t RAW_PRESSURE_MAX;
5017 static const int32_t RAW_ORIENTATION_MIN;
5018 static const int32_t RAW_ORIENTATION_MAX;
5019 static const int32_t RAW_DISTANCE_MIN;
5020 static const int32_t RAW_DISTANCE_MAX;
5021 static const int32_t RAW_TILT_MIN;
5022 static const int32_t RAW_TILT_MAX;
5023 static const int32_t RAW_ID_MIN;
5024 static const int32_t RAW_ID_MAX;
5025 static const int32_t RAW_SLOT_MIN;
5026 static const int32_t RAW_SLOT_MAX;
5027 static const float X_PRECISION;
5028 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005029 static const float X_PRECISION_VIRTUAL;
5030 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031
5032 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005033 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034
5035 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5036
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005037 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005038 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005039
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 enum Axes {
5041 POSITION = 1 << 0,
5042 TOUCH = 1 << 1,
5043 TOOL = 1 << 2,
5044 PRESSURE = 1 << 3,
5045 ORIENTATION = 1 << 4,
5046 MINOR = 1 << 5,
5047 ID = 1 << 6,
5048 DISTANCE = 1 << 7,
5049 TILT = 1 << 8,
5050 SLOT = 1 << 9,
5051 TOOL_TYPE = 1 << 10,
5052 };
5053
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005054 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5055 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005056 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005058 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 int32_t toRawX(float displayX);
5060 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005061 int32_t toRotatedRawX(float displayX);
5062 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005063 float toCookedX(float rawX, float rawY);
5064 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005066 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005068 float toDisplayY(int32_t rawY, int32_t displayHeight);
5069
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070};
5071
5072const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5073const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5074const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5075const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5076const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5077const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5078const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5079const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005080const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5081const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5083const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5084const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5085const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5086const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5087const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5088const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5089const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5090const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5091const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5092const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5093const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005094const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5095 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5096const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5097 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005098const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5099 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100
5101const float TouchInputMapperTest::GEOMETRIC_SCALE =
5102 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5103 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5104
5105const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5106 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5107 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5108};
5109
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005110void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005111 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5112 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005113}
5114
5115void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5116 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5117 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118}
5119
Santos Cordonfa5cf462017-04-05 10:37:00 -07005120void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005121 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5122 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5123 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005124}
5125
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005127 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5128 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5129 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131}
5132
Jason Gerecke489fda82012-09-07 17:19:40 -07005133void TouchInputMapperTest::prepareLocationCalibration() {
5134 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5135}
5136
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137int32_t TouchInputMapperTest::toRawX(float displayX) {
5138 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5139}
5140
5141int32_t TouchInputMapperTest::toRawY(float displayY) {
5142 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5143}
5144
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005145int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5146 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5147}
5148
5149int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5150 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5151}
5152
Jason Gerecke489fda82012-09-07 17:19:40 -07005153float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5154 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5155 return rawX;
5156}
5157
5158float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5159 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5160 return rawY;
5161}
5162
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005164 return toDisplayX(rawX, DISPLAY_WIDTH);
5165}
5166
5167float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5168 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169}
5170
5171float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005172 return toDisplayY(rawY, DISPLAY_HEIGHT);
5173}
5174
5175float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5176 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
5179
5180// --- SingleTouchInputMapperTest ---
5181
5182class SingleTouchInputMapperTest : public TouchInputMapperTest {
5183protected:
5184 void prepareButtons();
5185 void prepareAxes(int axes);
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5188 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5189 void processUp(SingleTouchInputMapper& mappery);
5190 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5191 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5192 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5193 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5194 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5195 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196};
5197
5198void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005199 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200}
5201
5202void SingleTouchInputMapperTest::prepareAxes(int axes) {
5203 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206 }
5207 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005208 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5209 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210 }
5211 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5213 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214 }
5215 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5217 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 }
5219 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005220 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005222 }
5223}
5224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005225void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234}
5235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005236void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005238}
5239
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005240void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242}
5243
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005244void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5245 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247}
5248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005249void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251}
5252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005253void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5254 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257}
5258
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005259void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5260 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262}
5263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005264void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005266}
5267
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005269 prepareButtons();
5270 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274}
5275
5276TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005277 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5278 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 prepareButtons();
5280 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284}
5285
5286TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287 prepareButtons();
5288 prepareAxes(POSITION);
5289 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
5295TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 prepareButtons();
5297 prepareAxes(POSITION);
5298 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302}
5303
5304TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 addConfigurationProperty("touch.deviceType", "touchScreen");
5306 prepareDisplay(DISPLAY_ORIENTATION_0);
5307 prepareButtons();
5308 prepareAxes(POSITION);
5309 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005310 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311
5312 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314
5315 // Virtual key is down.
5316 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5317 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5318 processDown(mapper, x, y);
5319 processSync(mapper);
5320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005322 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323
5324 // Virtual key is up.
5325 processUp(mapper);
5326 processSync(mapper);
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
5332TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 addConfigurationProperty("touch.deviceType", "touchScreen");
5334 prepareDisplay(DISPLAY_ORIENTATION_0);
5335 prepareButtons();
5336 prepareAxes(POSITION);
5337 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339
5340 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005341 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342
5343 // Virtual key is down.
5344 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5345 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5346 processDown(mapper, x, y);
5347 processSync(mapper);
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5349
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005350 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351
5352 // Virtual key is up.
5353 processUp(mapper);
5354 processSync(mapper);
5355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005357 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358}
5359
5360TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 addConfigurationProperty("touch.deviceType", "touchScreen");
5362 prepareDisplay(DISPLAY_ORIENTATION_0);
5363 prepareButtons();
5364 prepareAxes(POSITION);
5365 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005366 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005367
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005369 ASSERT_TRUE(
5370 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371 ASSERT_TRUE(flags[0]);
5372 ASSERT_FALSE(flags[1]);
5373}
5374
5375TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 addConfigurationProperty("touch.deviceType", "touchScreen");
5377 prepareDisplay(DISPLAY_ORIENTATION_0);
5378 prepareButtons();
5379 prepareAxes(POSITION);
5380 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005381 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
arthurhungdcef2dc2020-08-11 14:47:50 +08005383 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384
5385 NotifyKeyArgs args;
5386
5387 // Press virtual key.
5388 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5389 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5390 processDown(mapper, x, y);
5391 processSync(mapper);
5392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5394 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5395 ASSERT_EQ(DEVICE_ID, args.deviceId);
5396 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5397 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5398 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5399 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5401 ASSERT_EQ(KEY_HOME, args.scanCode);
5402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5403 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5404
5405 // Release virtual key.
5406 processUp(mapper);
5407 processSync(mapper);
5408
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5410 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5411 ASSERT_EQ(DEVICE_ID, args.deviceId);
5412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5413 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5415 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5416 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5417 ASSERT_EQ(KEY_HOME, args.scanCode);
5418 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5419 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5420
5421 // Should not have sent any motions.
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5423}
5424
5425TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005426 addConfigurationProperty("touch.deviceType", "touchScreen");
5427 prepareDisplay(DISPLAY_ORIENTATION_0);
5428 prepareButtons();
5429 prepareAxes(POSITION);
5430 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005431 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432
arthurhungdcef2dc2020-08-11 14:47:50 +08005433 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434
5435 NotifyKeyArgs keyArgs;
5436
5437 // Press virtual key.
5438 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5439 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5440 processDown(mapper, x, y);
5441 processSync(mapper);
5442
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5444 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5445 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5446 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5447 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5448 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5449 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5450 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5451 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5453 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5454
5455 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5456 // into the display area.
5457 y -= 100;
5458 processMove(mapper, x, y);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5462 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5463 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5465 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5468 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5469 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5470 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5471 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5472 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5473
5474 NotifyMotionArgs motionArgs;
5475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5476 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5477 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5478 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5479 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5480 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5481 ASSERT_EQ(0, motionArgs.flags);
5482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5483 ASSERT_EQ(0, motionArgs.buttonState);
5484 ASSERT_EQ(0, motionArgs.edgeFlags);
5485 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5489 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5490 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5491 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5493
5494 // Keep moving out of bounds. Should generate a pointer move.
5495 y -= 50;
5496 processMove(mapper, x, y);
5497 processSync(mapper);
5498
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5501 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5502 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5503 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5505 ASSERT_EQ(0, motionArgs.flags);
5506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5507 ASSERT_EQ(0, motionArgs.buttonState);
5508 ASSERT_EQ(0, motionArgs.edgeFlags);
5509 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5513 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5514 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5515 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5517
5518 // Release out of bounds. Should generate a pointer up.
5519 processUp(mapper);
5520 processSync(mapper);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5524 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
5532 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5536 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5537 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5538 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5540
5541 // Should not have sent any more keys or motions.
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5544}
5545
5546TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 addConfigurationProperty("touch.deviceType", "touchScreen");
5548 prepareDisplay(DISPLAY_ORIENTATION_0);
5549 prepareButtons();
5550 prepareAxes(POSITION);
5551 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005552 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
arthurhungdcef2dc2020-08-11 14:47:50 +08005554 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 NotifyMotionArgs motionArgs;
5557
5558 // Initially go down out of bounds.
5559 int32_t x = -10;
5560 int32_t y = -10;
5561 processDown(mapper, x, y);
5562 processSync(mapper);
5563
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5565
5566 // Move into the display area. Should generate a pointer down.
5567 x = 50;
5568 y = 75;
5569 processMove(mapper, x, y);
5570 processSync(mapper);
5571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5574 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5576 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5578 ASSERT_EQ(0, motionArgs.flags);
5579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5580 ASSERT_EQ(0, motionArgs.buttonState);
5581 ASSERT_EQ(0, motionArgs.edgeFlags);
5582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5586 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5587 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5588 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5590
5591 // Release. Should generate a pointer up.
5592 processUp(mapper);
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5596 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5597 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5599 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5601 ASSERT_EQ(0, motionArgs.flags);
5602 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5603 ASSERT_EQ(0, motionArgs.buttonState);
5604 ASSERT_EQ(0, motionArgs.edgeFlags);
5605 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5609 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5610 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5611 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5612 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5613
5614 // Should not have sent any more keys or motions.
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5617}
5618
Santos Cordonfa5cf462017-04-05 10:37:00 -07005619TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005620 addConfigurationProperty("touch.deviceType", "touchScreen");
5621 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5622
5623 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5624 prepareButtons();
5625 prepareAxes(POSITION);
5626 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005627 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005628
arthurhungdcef2dc2020-08-11 14:47:50 +08005629 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005630
5631 NotifyMotionArgs motionArgs;
5632
5633 // Down.
5634 int32_t x = 100;
5635 int32_t y = 125;
5636 processDown(mapper, x, y);
5637 processSync(mapper);
5638
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5641 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5642 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5643 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5644 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5646 ASSERT_EQ(0, motionArgs.flags);
5647 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5648 ASSERT_EQ(0, motionArgs.buttonState);
5649 ASSERT_EQ(0, motionArgs.edgeFlags);
5650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5654 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5655 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Move.
5661 x += 50;
5662 y += 75;
5663 processMove(mapper, x, y);
5664 processSync(mapper);
5665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5667 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5668 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5669 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5670 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5671 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5673 ASSERT_EQ(0, motionArgs.flags);
5674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5675 ASSERT_EQ(0, motionArgs.buttonState);
5676 ASSERT_EQ(0, motionArgs.edgeFlags);
5677 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5681 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5682 1, 0, 0, 0, 0, 0, 0, 0));
5683 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5684 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5685 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5686
5687 // Up.
5688 processUp(mapper);
5689 processSync(mapper);
5690
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5694 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5695 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5696 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5698 ASSERT_EQ(0, motionArgs.flags);
5699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5700 ASSERT_EQ(0, motionArgs.buttonState);
5701 ASSERT_EQ(0, motionArgs.edgeFlags);
5702 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5703 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5706 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5707 1, 0, 0, 0, 0, 0, 0, 0));
5708 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5709 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5710 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5711
5712 // Should not have sent any more keys or motions.
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5715}
5716
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 addConfigurationProperty("touch.deviceType", "touchScreen");
5719 prepareDisplay(DISPLAY_ORIENTATION_0);
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005723 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
arthurhungdcef2dc2020-08-11 14:47:50 +08005725 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
5727 NotifyMotionArgs motionArgs;
5728
5729 // Down.
5730 int32_t x = 100;
5731 int32_t y = 125;
5732 processDown(mapper, x, y);
5733 processSync(mapper);
5734
5735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5737 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5738 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5739 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5741 ASSERT_EQ(0, motionArgs.flags);
5742 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5743 ASSERT_EQ(0, motionArgs.buttonState);
5744 ASSERT_EQ(0, motionArgs.edgeFlags);
5745 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5746 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5747 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5749 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5750 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5751 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5753
5754 // Move.
5755 x += 50;
5756 y += 75;
5757 processMove(mapper, x, y);
5758 processSync(mapper);
5759
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5762 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5764 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5766 ASSERT_EQ(0, motionArgs.flags);
5767 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5768 ASSERT_EQ(0, motionArgs.buttonState);
5769 ASSERT_EQ(0, motionArgs.edgeFlags);
5770 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5774 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5775 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5776 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5777 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5778
5779 // Up.
5780 processUp(mapper);
5781 processSync(mapper);
5782
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5789 ASSERT_EQ(0, motionArgs.flags);
5790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5791 ASSERT_EQ(0, motionArgs.buttonState);
5792 ASSERT_EQ(0, motionArgs.edgeFlags);
5793 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5795 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5797 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5798 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5799 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5801
5802 // Should not have sent any more keys or motions.
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5805}
5806
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005807TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 addConfigurationProperty("touch.deviceType", "touchScreen");
5809 prepareButtons();
5810 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005811 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5812 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005813 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814
5815 NotifyMotionArgs args;
5816
5817 // Rotation 90.
5818 prepareDisplay(DISPLAY_ORIENTATION_90);
5819 processDown(mapper, toRawX(50), toRawY(75));
5820 processSync(mapper);
5821
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5823 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5824 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5825
5826 processUp(mapper);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5829}
5830
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005831TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 addConfigurationProperty("touch.deviceType", "touchScreen");
5833 prepareButtons();
5834 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005835 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5836 // orientation-aware are affected by display rotation.
5837 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005838 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839
5840 NotifyMotionArgs args;
5841
5842 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005843 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844 prepareDisplay(DISPLAY_ORIENTATION_0);
5845 processDown(mapper, toRawX(50), toRawY(75));
5846 processSync(mapper);
5847
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5849 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5850 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5851
5852 processUp(mapper);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5855
5856 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005857 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005859 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 processSync(mapper);
5861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5863 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5864 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5865
5866 processUp(mapper);
5867 processSync(mapper);
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5869
5870 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005871 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 prepareDisplay(DISPLAY_ORIENTATION_180);
5873 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5874 processSync(mapper);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5877 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5878 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5879
5880 processUp(mapper);
5881 processSync(mapper);
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5883
5884 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005885 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005887 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 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
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005899TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5900 addConfigurationProperty("touch.deviceType", "touchScreen");
5901 prepareButtons();
5902 prepareAxes(POSITION);
5903 addConfigurationProperty("touch.orientationAware", "1");
5904 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5905 clearViewports();
5906 prepareDisplay(DISPLAY_ORIENTATION_0);
5907 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5908 NotifyMotionArgs args;
5909
5910 // Orientation 0.
5911 processDown(mapper, toRawX(50), toRawY(75));
5912 processSync(mapper);
5913
5914 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5915 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5916 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5917
5918 processUp(mapper);
5919 processSync(mapper);
5920 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5921}
5922
5923TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5924 addConfigurationProperty("touch.deviceType", "touchScreen");
5925 prepareButtons();
5926 prepareAxes(POSITION);
5927 addConfigurationProperty("touch.orientationAware", "1");
5928 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5929 clearViewports();
5930 prepareDisplay(DISPLAY_ORIENTATION_0);
5931 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5932 NotifyMotionArgs args;
5933
5934 // Orientation 90.
5935 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5936 processSync(mapper);
5937
5938 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5939 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5940 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5941
5942 processUp(mapper);
5943 processSync(mapper);
5944 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5945}
5946
5947TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5948 addConfigurationProperty("touch.deviceType", "touchScreen");
5949 prepareButtons();
5950 prepareAxes(POSITION);
5951 addConfigurationProperty("touch.orientationAware", "1");
5952 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5953 clearViewports();
5954 prepareDisplay(DISPLAY_ORIENTATION_0);
5955 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5956 NotifyMotionArgs args;
5957
5958 // Orientation 180.
5959 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5960 processSync(mapper);
5961
5962 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5963 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5964 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5965
5966 processUp(mapper);
5967 processSync(mapper);
5968 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5969}
5970
5971TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5972 addConfigurationProperty("touch.deviceType", "touchScreen");
5973 prepareButtons();
5974 prepareAxes(POSITION);
5975 addConfigurationProperty("touch.orientationAware", "1");
5976 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5977 clearViewports();
5978 prepareDisplay(DISPLAY_ORIENTATION_0);
5979 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5980 NotifyMotionArgs args;
5981
5982 // Orientation 270.
5983 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5984 processSync(mapper);
5985
5986 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5987 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5988 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5989
5990 processUp(mapper);
5991 processSync(mapper);
5992 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5993}
5994
5995TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5996 addConfigurationProperty("touch.deviceType", "touchScreen");
5997 prepareButtons();
5998 prepareAxes(POSITION);
5999 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6000 // orientation-aware are affected by display rotation.
6001 addConfigurationProperty("touch.orientationAware", "0");
6002 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6003 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6004
6005 NotifyMotionArgs args;
6006
6007 // Orientation 90, Rotation 0.
6008 clearViewports();
6009 prepareDisplay(DISPLAY_ORIENTATION_0);
6010 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6011 processSync(mapper);
6012
6013 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6014 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6015 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6016
6017 processUp(mapper);
6018 processSync(mapper);
6019 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6020
6021 // Orientation 90, Rotation 90.
6022 clearViewports();
6023 prepareDisplay(DISPLAY_ORIENTATION_90);
6024 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6025 processSync(mapper);
6026
6027 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6028 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6029 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6030
6031 processUp(mapper);
6032 processSync(mapper);
6033 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6034
6035 // Orientation 90, Rotation 180.
6036 clearViewports();
6037 prepareDisplay(DISPLAY_ORIENTATION_180);
6038 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6039 processSync(mapper);
6040
6041 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6042 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6043 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6044
6045 processUp(mapper);
6046 processSync(mapper);
6047 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6048
6049 // Orientation 90, Rotation 270.
6050 clearViewports();
6051 prepareDisplay(DISPLAY_ORIENTATION_270);
6052 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6053 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6054 processSync(mapper);
6055
6056 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6057 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6058 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6059
6060 processUp(mapper);
6061 processSync(mapper);
6062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6063}
6064
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 addConfigurationProperty("touch.deviceType", "touchScreen");
6067 prepareDisplay(DISPLAY_ORIENTATION_0);
6068 prepareButtons();
6069 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 // These calculations are based on the input device calibration documentation.
6073 int32_t rawX = 100;
6074 int32_t rawY = 200;
6075 int32_t rawPressure = 10;
6076 int32_t rawToolMajor = 12;
6077 int32_t rawDistance = 2;
6078 int32_t rawTiltX = 30;
6079 int32_t rawTiltY = 110;
6080
6081 float x = toDisplayX(rawX);
6082 float y = toDisplayY(rawY);
6083 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6084 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6085 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6086 float distance = float(rawDistance);
6087
6088 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6089 float tiltScale = M_PI / 180;
6090 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6091 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6092 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6093 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6094
6095 processDown(mapper, rawX, rawY);
6096 processPressure(mapper, rawPressure);
6097 processToolMajor(mapper, rawToolMajor);
6098 processDistance(mapper, rawDistance);
6099 processTilt(mapper, rawTiltX, rawTiltY);
6100 processSync(mapper);
6101
6102 NotifyMotionArgs args;
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6105 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6106 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6107}
6108
Jason Gerecke489fda82012-09-07 17:19:40 -07006109TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006110 addConfigurationProperty("touch.deviceType", "touchScreen");
6111 prepareDisplay(DISPLAY_ORIENTATION_0);
6112 prepareLocationCalibration();
6113 prepareButtons();
6114 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006116
6117 int32_t rawX = 100;
6118 int32_t rawY = 200;
6119
6120 float x = toDisplayX(toCookedX(rawX, rawY));
6121 float y = toDisplayY(toCookedY(rawX, rawY));
6122
6123 processDown(mapper, rawX, rawY);
6124 processSync(mapper);
6125
6126 NotifyMotionArgs args;
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6129 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6130}
6131
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 addConfigurationProperty("touch.deviceType", "touchScreen");
6134 prepareDisplay(DISPLAY_ORIENTATION_0);
6135 prepareButtons();
6136 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 NotifyMotionArgs motionArgs;
6140 NotifyKeyArgs keyArgs;
6141
6142 processDown(mapper, 100, 200);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6146 ASSERT_EQ(0, motionArgs.buttonState);
6147
6148 // press BTN_LEFT, release BTN_LEFT
6149 processKey(mapper, BTN_LEFT, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6154
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6158
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 processKey(mapper, BTN_LEFT, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6170 processKey(mapper, BTN_RIGHT, 1);
6171 processKey(mapper, BTN_MIDDLE, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6176 motionArgs.buttonState);
6177
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6180 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6181
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6184 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6185 motionArgs.buttonState);
6186
Michael Wrightd02c5b62014-02-10 15:10:22 -08006187 processKey(mapper, BTN_RIGHT, 0);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 processKey(mapper, BTN_MIDDLE, 0);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006202
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 // press BTN_BACK, release BTN_BACK
6208 processKey(mapper, BTN_BACK, 1);
6209 processSync(mapper);
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6212 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 processKey(mapper, BTN_BACK, 0);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(0, motionArgs.buttonState);
6231
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6234 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6235
6236 // press BTN_SIDE, release BTN_SIDE
6237 processKey(mapper, BTN_SIDE, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6241 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006242
Michael Wrightd02c5b62014-02-10 15:10:22 -08006243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006245 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 processKey(mapper, BTN_SIDE, 0);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006256
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259 ASSERT_EQ(0, motionArgs.buttonState);
6260
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6263 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6264
6265 // press BTN_FORWARD, release BTN_FORWARD
6266 processKey(mapper, BTN_FORWARD, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6269 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6270 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279
6280 processKey(mapper, BTN_FORWARD, 0);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006283 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006285
6286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288 ASSERT_EQ(0, motionArgs.buttonState);
6289
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6291 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6292 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6293
6294 // press BTN_EXTRA, release BTN_EXTRA
6295 processKey(mapper, BTN_EXTRA, 1);
6296 processSync(mapper);
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6298 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6299 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6307 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 processKey(mapper, BTN_EXTRA, 0);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317 ASSERT_EQ(0, motionArgs.buttonState);
6318
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6321 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6322
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6324
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 // press BTN_STYLUS, release BTN_STYLUS
6326 processKey(mapper, BTN_STYLUS, 1);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6333 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6334 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006335
6336 processKey(mapper, BTN_STYLUS, 0);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006341
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
6346 // press BTN_STYLUS2, release BTN_STYLUS2
6347 processKey(mapper, BTN_STYLUS2, 1);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006351 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6352
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356
6357 processKey(mapper, BTN_STYLUS2, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006362
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006365 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366
6367 // release touch
6368 processUp(mapper);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6372 ASSERT_EQ(0, motionArgs.buttonState);
6373}
6374
6375TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 addConfigurationProperty("touch.deviceType", "touchScreen");
6377 prepareDisplay(DISPLAY_ORIENTATION_0);
6378 prepareButtons();
6379 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006380 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 NotifyMotionArgs motionArgs;
6383
6384 // default tool type is finger
6385 processDown(mapper, 100, 200);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6390
6391 // eraser
6392 processKey(mapper, BTN_TOOL_RUBBER, 1);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6396 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6397
6398 // stylus
6399 processKey(mapper, BTN_TOOL_RUBBER, 0);
6400 processKey(mapper, BTN_TOOL_PEN, 1);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6405
6406 // brush
6407 processKey(mapper, BTN_TOOL_PEN, 0);
6408 processKey(mapper, BTN_TOOL_BRUSH, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6413
6414 // pencil
6415 processKey(mapper, BTN_TOOL_BRUSH, 0);
6416 processKey(mapper, BTN_TOOL_PENCIL, 1);
6417 processSync(mapper);
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6421
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006422 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006423 processKey(mapper, BTN_TOOL_PENCIL, 0);
6424 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6429
6430 // mouse
6431 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6432 processKey(mapper, BTN_TOOL_MOUSE, 1);
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6437
6438 // lens
6439 processKey(mapper, BTN_TOOL_MOUSE, 0);
6440 processKey(mapper, BTN_TOOL_LENS, 1);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6445
6446 // double-tap
6447 processKey(mapper, BTN_TOOL_LENS, 0);
6448 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6449 processSync(mapper);
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6453
6454 // triple-tap
6455 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6456 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6461
6462 // quad-tap
6463 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6464 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6469
6470 // finger
6471 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6472 processKey(mapper, BTN_TOOL_FINGER, 1);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6477
6478 // stylus trumps finger
6479 processKey(mapper, BTN_TOOL_PEN, 1);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6484
6485 // eraser trumps stylus
6486 processKey(mapper, BTN_TOOL_RUBBER, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6491
6492 // mouse trumps eraser
6493 processKey(mapper, BTN_TOOL_MOUSE, 1);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6498
6499 // back to default tool type
6500 processKey(mapper, BTN_TOOL_MOUSE, 0);
6501 processKey(mapper, BTN_TOOL_RUBBER, 0);
6502 processKey(mapper, BTN_TOOL_PEN, 0);
6503 processKey(mapper, BTN_TOOL_FINGER, 0);
6504 processSync(mapper);
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6508}
6509
6510TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006511 addConfigurationProperty("touch.deviceType", "touchScreen");
6512 prepareDisplay(DISPLAY_ORIENTATION_0);
6513 prepareButtons();
6514 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006515 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006516 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006517
6518 NotifyMotionArgs motionArgs;
6519
6520 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6521 processKey(mapper, BTN_TOOL_FINGER, 1);
6522 processMove(mapper, 100, 200);
6523 processSync(mapper);
6524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6527 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6532 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6533
6534 // move a little
6535 processMove(mapper, 150, 250);
6536 processSync(mapper);
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6540 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6541
6542 // down when BTN_TOUCH is pressed, pressure defaults to 1
6543 processKey(mapper, BTN_TOUCH, 1);
6544 processSync(mapper);
6545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6548 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6549
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6553 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6554
6555 // up when BTN_TOUCH is released, hover restored
6556 processKey(mapper, BTN_TOUCH, 0);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6561 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6562
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6567
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6572
6573 // exit hover when pointer goes away
6574 processKey(mapper, BTN_TOOL_FINGER, 0);
6575 processSync(mapper);
6576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6579 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6580}
6581
6582TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583 addConfigurationProperty("touch.deviceType", "touchScreen");
6584 prepareDisplay(DISPLAY_ORIENTATION_0);
6585 prepareButtons();
6586 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006587 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588
6589 NotifyMotionArgs motionArgs;
6590
6591 // initially hovering because pressure is 0
6592 processDown(mapper, 100, 200);
6593 processPressure(mapper, 0);
6594 processSync(mapper);
6595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6598 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6599
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6603 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6604
6605 // move a little
6606 processMove(mapper, 150, 250);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6611 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6612
6613 // down when pressure is non-zero
6614 processPressure(mapper, RAW_PRESSURE_MAX);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6619 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6620
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6624 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6625
6626 // up when pressure becomes 0, hover restored
6627 processPressure(mapper, 0);
6628 processSync(mapper);
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6632 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6633
6634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6638
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6643
6644 // exit hover when pointer goes away
6645 processUp(mapper);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6650 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6651}
6652
Prabir Pradhan5632d622021-09-06 07:57:20 -07006653// --- TouchDisplayProjectionTest ---
6654
6655class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6656public:
6657 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6658 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6659 // rotated equivalent of the given un-rotated physical display bounds.
6660 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6661 uint32_t inverseRotationFlags;
6662 auto width = DISPLAY_WIDTH;
6663 auto height = DISPLAY_HEIGHT;
6664 switch (orientation) {
6665 case DISPLAY_ORIENTATION_90:
6666 inverseRotationFlags = ui::Transform::ROT_270;
6667 std::swap(width, height);
6668 break;
6669 case DISPLAY_ORIENTATION_180:
6670 inverseRotationFlags = ui::Transform::ROT_180;
6671 break;
6672 case DISPLAY_ORIENTATION_270:
6673 inverseRotationFlags = ui::Transform::ROT_90;
6674 std::swap(width, height);
6675 break;
6676 case DISPLAY_ORIENTATION_0:
6677 inverseRotationFlags = ui::Transform::ROT_0;
6678 break;
6679 default:
6680 FAIL() << "Invalid orientation: " << orientation;
6681 }
6682
6683 const ui::Transform rotation(inverseRotationFlags, width, height);
6684 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6685
6686 std::optional<DisplayViewport> internalViewport =
6687 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6688 DisplayViewport& v = *internalViewport;
6689 v.displayId = DISPLAY_ID;
6690 v.orientation = orientation;
6691
6692 v.logicalLeft = 0;
6693 v.logicalTop = 0;
6694 v.logicalRight = 100;
6695 v.logicalBottom = 100;
6696
6697 v.physicalLeft = rotatedPhysicalDisplay.left;
6698 v.physicalTop = rotatedPhysicalDisplay.top;
6699 v.physicalRight = rotatedPhysicalDisplay.right;
6700 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6701
6702 v.deviceWidth = width;
6703 v.deviceHeight = height;
6704
6705 v.isActive = true;
6706 v.uniqueId = UNIQUE_ID;
6707 v.type = ViewportType::INTERNAL;
6708 mFakePolicy->updateViewport(v);
6709 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6710 }
6711
6712 void assertReceivedMove(const Point& point) {
6713 NotifyMotionArgs motionArgs;
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6716 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6718 1, 0, 0, 0, 0, 0, 0, 0));
6719 }
6720};
6721
6722TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6723 addConfigurationProperty("touch.deviceType", "touchScreen");
6724 prepareDisplay(DISPLAY_ORIENTATION_0);
6725
6726 prepareButtons();
6727 prepareAxes(POSITION);
6728 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6729
6730 NotifyMotionArgs motionArgs;
6731
6732 // Configure the DisplayViewport such that the logical display maps to a subsection of
6733 // the display panel called the physical display. Here, the physical display is bounded by the
6734 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6735 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6736 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6737 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6738
6739 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6740 DISPLAY_ORIENTATION_270}) {
6741 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6742
6743 // Touches outside the physical display should be ignored, and should not generate any
6744 // events. Ensure touches at the following points that lie outside of the physical display
6745 // area do not generate any events.
6746 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6747 processDown(mapper, toRawX(point.x), toRawY(point.y));
6748 processSync(mapper);
6749 processUp(mapper);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6752 << "Unexpected event generated for touch outside physical display at point: "
6753 << point.x << ", " << point.y;
6754 }
6755 }
6756}
6757
6758TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6759 addConfigurationProperty("touch.deviceType", "touchScreen");
6760 prepareDisplay(DISPLAY_ORIENTATION_0);
6761
6762 prepareButtons();
6763 prepareAxes(POSITION);
6764 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6765
6766 NotifyMotionArgs motionArgs;
6767
6768 // Configure the DisplayViewport such that the logical display maps to a subsection of
6769 // the display panel called the physical display. Here, the physical display is bounded by the
6770 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6771 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6772
6773 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6774 DISPLAY_ORIENTATION_270}) {
6775 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6776
6777 // Touches that start outside the physical display should be ignored until it enters the
6778 // physical display bounds, at which point it should generate a down event. Start a touch at
6779 // the point (5, 100), which is outside the physical display bounds.
6780 static const Point kOutsidePoint{5, 100};
6781 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6782 processSync(mapper);
6783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6784
6785 // Move the touch into the physical display area. This should generate a pointer down.
6786 processMove(mapper, toRawX(11), toRawY(21));
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6791 ASSERT_NO_FATAL_FAILURE(
6792 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6793
6794 // Move the touch inside the physical display area. This should generate a pointer move.
6795 processMove(mapper, toRawX(69), toRawY(159));
6796 processSync(mapper);
6797 assertReceivedMove({69, 159});
6798
6799 // Move outside the physical display area. Since the pointer is already down, this should
6800 // now continue generating events.
6801 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6802 processSync(mapper);
6803 assertReceivedMove(kOutsidePoint);
6804
6805 // Release. This should generate a pointer up.
6806 processUp(mapper);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6811 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6812
6813 // Ensure no more events were generated.
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6816 }
6817}
6818
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819// --- MultiTouchInputMapperTest ---
6820
6821class MultiTouchInputMapperTest : public TouchInputMapperTest {
6822protected:
6823 void prepareAxes(int axes);
6824
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006825 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6826 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6827 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6828 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6829 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6830 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6831 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6832 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6833 void processId(MultiTouchInputMapper& mapper, int32_t id);
6834 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6835 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6836 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6837 void processMTSync(MultiTouchInputMapper& mapper);
6838 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006839};
6840
6841void MultiTouchInputMapperTest::prepareAxes(int axes) {
6842 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006843 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6844 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845 }
6846 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6848 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006849 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006850 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6851 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852 }
6853 }
6854 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006855 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6856 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006858 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006859 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860 }
6861 }
6862 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006863 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6864 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865 }
6866 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006867 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6868 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869 }
6870 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6872 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006873 }
6874 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006875 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6876 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877 }
6878 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006879 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6880 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 }
6882 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884 }
6885}
6886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006887void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6888 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006891}
6892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006893void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6894 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006896}
6897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006898void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6899 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006901}
6902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006903void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905}
6906
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006907void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909}
6910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006911void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6912 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914}
6915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006916void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918}
6919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006920void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006922}
6923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006924void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926}
6927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006928void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930}
6931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006932void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934}
6935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006936void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6937 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006938 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939}
6940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006941void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943}
6944
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006945void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947}
6948
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006950 addConfigurationProperty("touch.deviceType", "touchScreen");
6951 prepareDisplay(DISPLAY_ORIENTATION_0);
6952 prepareAxes(POSITION);
6953 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006954 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955
arthurhungdcef2dc2020-08-11 14:47:50 +08006956 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957
6958 NotifyMotionArgs motionArgs;
6959
6960 // Two fingers down at once.
6961 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6962 processPosition(mapper, x1, y1);
6963 processMTSync(mapper);
6964 processPosition(mapper, x2, y2);
6965 processMTSync(mapper);
6966 processSync(mapper);
6967
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6970 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6971 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6972 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6974 ASSERT_EQ(0, motionArgs.flags);
6975 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6976 ASSERT_EQ(0, motionArgs.buttonState);
6977 ASSERT_EQ(0, motionArgs.edgeFlags);
6978 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6979 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6982 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6983 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6984 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6986
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6989 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6991 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006992 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 ASSERT_EQ(0, motionArgs.flags);
6994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6995 ASSERT_EQ(0, motionArgs.buttonState);
6996 ASSERT_EQ(0, motionArgs.edgeFlags);
6997 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7000 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7003 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7005 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7009
7010 // Move.
7011 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7012 processPosition(mapper, x1, y1);
7013 processMTSync(mapper);
7014 processPosition(mapper, x2, y2);
7015 processMTSync(mapper);
7016 processSync(mapper);
7017
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7019 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7020 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7021 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7022 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7024 ASSERT_EQ(0, motionArgs.flags);
7025 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7026 ASSERT_EQ(0, motionArgs.buttonState);
7027 ASSERT_EQ(0, motionArgs.edgeFlags);
7028 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7029 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7031 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7032 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7034 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7036 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7037 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7038 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7039 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7040
7041 // First finger up.
7042 x2 += 15; y2 -= 20;
7043 processPosition(mapper, x2, y2);
7044 processMTSync(mapper);
7045 processSync(mapper);
7046
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7049 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7050 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7051 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007052 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 ASSERT_EQ(0, motionArgs.flags);
7054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7055 ASSERT_EQ(0, motionArgs.buttonState);
7056 ASSERT_EQ(0, motionArgs.edgeFlags);
7057 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7060 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7063 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7065 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7066 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7067 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7069
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7071 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7072 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7073 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7074 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7076 ASSERT_EQ(0, motionArgs.flags);
7077 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7078 ASSERT_EQ(0, motionArgs.buttonState);
7079 ASSERT_EQ(0, motionArgs.edgeFlags);
7080 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7081 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7082 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7085 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7086 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7087 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7088
7089 // Move.
7090 x2 += 20; y2 -= 25;
7091 processPosition(mapper, x2, y2);
7092 processMTSync(mapper);
7093 processSync(mapper);
7094
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7097 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7098 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7099 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7101 ASSERT_EQ(0, motionArgs.flags);
7102 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7103 ASSERT_EQ(0, motionArgs.buttonState);
7104 ASSERT_EQ(0, motionArgs.edgeFlags);
7105 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7106 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7109 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7110 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7111 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7113
7114 // New finger down.
7115 int32_t x3 = 700, y3 = 300;
7116 processPosition(mapper, x2, y2);
7117 processMTSync(mapper);
7118 processPosition(mapper, x3, y3);
7119 processMTSync(mapper);
7120 processSync(mapper);
7121
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7123 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7124 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7125 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7126 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007127 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.flags);
7129 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7130 ASSERT_EQ(0, motionArgs.buttonState);
7131 ASSERT_EQ(0, motionArgs.edgeFlags);
7132 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7133 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7135 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7138 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7140 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7141 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7142 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7143 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7144
7145 // Second finger up.
7146 x3 += 30; y3 -= 20;
7147 processPosition(mapper, x3, y3);
7148 processMTSync(mapper);
7149 processSync(mapper);
7150
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7153 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7154 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7155 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007156 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 ASSERT_EQ(0, motionArgs.flags);
7158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7159 ASSERT_EQ(0, motionArgs.buttonState);
7160 ASSERT_EQ(0, motionArgs.edgeFlags);
7161 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7162 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7164 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7166 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7167 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7169 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7173
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7175 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7176 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7177 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7178 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7180 ASSERT_EQ(0, motionArgs.flags);
7181 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7182 ASSERT_EQ(0, motionArgs.buttonState);
7183 ASSERT_EQ(0, motionArgs.edgeFlags);
7184 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7185 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7188 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7189 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7190 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7191 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7192
7193 // Last finger up.
7194 processMTSync(mapper);
7195 processSync(mapper);
7196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7199 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7200 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7201 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7202 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7203 ASSERT_EQ(0, motionArgs.flags);
7204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7205 ASSERT_EQ(0, motionArgs.buttonState);
7206 ASSERT_EQ(0, motionArgs.edgeFlags);
7207 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7208 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7211 toDisplayX(x3), toDisplayY(y3), 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 // Should not have sent any more keys or motions.
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7219}
7220
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007221TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7222 addConfigurationProperty("touch.deviceType", "touchScreen");
7223 prepareDisplay(DISPLAY_ORIENTATION_0);
7224
7225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7226 /*fuzz*/ 0, /*resolution*/ 10);
7227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7228 /*fuzz*/ 0, /*resolution*/ 11);
7229 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7230 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7231 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7232 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7233 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7234 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7235 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7236 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7237
7238 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7239
7240 // X and Y axes
7241 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7242 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7243 // Touch major and minor
7244 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7245 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7246 // Tool major and minor
7247 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7248 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7249}
7250
7251TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7252 addConfigurationProperty("touch.deviceType", "touchScreen");
7253 prepareDisplay(DISPLAY_ORIENTATION_0);
7254
7255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7256 /*fuzz*/ 0, /*resolution*/ 10);
7257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7258 /*fuzz*/ 0, /*resolution*/ 11);
7259
7260 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7261
7262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7263
7264 // Touch major and minor
7265 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7266 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7267 // Tool major and minor
7268 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7269 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7270}
7271
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 addConfigurationProperty("touch.deviceType", "touchScreen");
7274 prepareDisplay(DISPLAY_ORIENTATION_0);
7275 prepareAxes(POSITION | ID);
7276 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278
arthurhungdcef2dc2020-08-11 14:47:50 +08007279 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007280
7281 NotifyMotionArgs motionArgs;
7282
7283 // Two fingers down at once.
7284 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7285 processPosition(mapper, x1, y1);
7286 processId(mapper, 1);
7287 processMTSync(mapper);
7288 processPosition(mapper, x2, y2);
7289 processId(mapper, 2);
7290 processMTSync(mapper);
7291 processSync(mapper);
7292
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7295 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7296 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7299 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007302 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7304 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7306 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7309 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7311 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7312
7313 // Move.
7314 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7315 processPosition(mapper, x1, y1);
7316 processId(mapper, 1);
7317 processMTSync(mapper);
7318 processPosition(mapper, x2, y2);
7319 processId(mapper, 2);
7320 processMTSync(mapper);
7321 processSync(mapper);
7322
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7333 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7334
7335 // First finger up.
7336 x2 += 15; y2 -= 20;
7337 processPosition(mapper, x2, y2);
7338 processId(mapper, 2);
7339 processMTSync(mapper);
7340 processSync(mapper);
7341
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007343 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7347 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7350 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7353
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7357 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7361
7362 // Move.
7363 x2 += 20; y2 -= 25;
7364 processPosition(mapper, x2, y2);
7365 processId(mapper, 2);
7366 processMTSync(mapper);
7367 processSync(mapper);
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7376
7377 // New finger down.
7378 int32_t x3 = 700, y3 = 300;
7379 processPosition(mapper, x2, y2);
7380 processId(mapper, 2);
7381 processMTSync(mapper);
7382 processPosition(mapper, x3, y3);
7383 processId(mapper, 3);
7384 processMTSync(mapper);
7385 processSync(mapper);
7386
7387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007388 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7392 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7397 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7398
7399 // Second finger up.
7400 x3 += 30; y3 -= 20;
7401 processPosition(mapper, x3, y3);
7402 processId(mapper, 3);
7403 processMTSync(mapper);
7404 processSync(mapper);
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007407 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7411 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7414 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7416 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7417
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7420 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7425
7426 // Last finger up.
7427 processMTSync(mapper);
7428 processSync(mapper);
7429
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7437
7438 // Should not have sent any more keys or motions.
7439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7441}
7442
7443TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444 addConfigurationProperty("touch.deviceType", "touchScreen");
7445 prepareDisplay(DISPLAY_ORIENTATION_0);
7446 prepareAxes(POSITION | ID | SLOT);
7447 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007448 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449
arthurhungdcef2dc2020-08-11 14:47:50 +08007450 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007451
7452 NotifyMotionArgs motionArgs;
7453
7454 // Two fingers down at once.
7455 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7456 processPosition(mapper, x1, y1);
7457 processId(mapper, 1);
7458 processSlot(mapper, 1);
7459 processPosition(mapper, x2, y2);
7460 processId(mapper, 2);
7461 processSync(mapper);
7462
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7465 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7470
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007472 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7476 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7479 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7481 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7482
7483 // Move.
7484 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7485 processSlot(mapper, 0);
7486 processPosition(mapper, x1, y1);
7487 processSlot(mapper, 1);
7488 processPosition(mapper, x2, y2);
7489 processSync(mapper);
7490
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7493 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7496 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7499 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7501 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7502
7503 // First finger up.
7504 x2 += 15; y2 -= 20;
7505 processSlot(mapper, 0);
7506 processId(mapper, -1);
7507 processSlot(mapper, 1);
7508 processPosition(mapper, x2, y2);
7509 processSync(mapper);
7510
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007512 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7516 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7522
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7525 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7526 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 // Move.
7532 x2 += 20; y2 -= 25;
7533 processPosition(mapper, x2, y2);
7534 processSync(mapper);
7535
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7539 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7543
7544 // New finger down.
7545 int32_t x3 = 700, y3 = 300;
7546 processPosition(mapper, x2, y2);
7547 processSlot(mapper, 0);
7548 processId(mapper, 3);
7549 processPosition(mapper, x3, y3);
7550 processSync(mapper);
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007553 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7560 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7563
7564 // Second finger up.
7565 x3 += 30; y3 -= 20;
7566 processSlot(mapper, 1);
7567 processId(mapper, -1);
7568 processSlot(mapper, 0);
7569 processPosition(mapper, x3, y3);
7570 processSync(mapper);
7571
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007573 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7577 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7580 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7582 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7586 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7590 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7591
7592 // Last finger up.
7593 processId(mapper, -1);
7594 processSync(mapper);
7595
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7597 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7598 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7602 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7603
7604 // Should not have sent any more keys or motions.
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7607}
7608
7609TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 addConfigurationProperty("touch.deviceType", "touchScreen");
7611 prepareDisplay(DISPLAY_ORIENTATION_0);
7612 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007613 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614
7615 // These calculations are based on the input device calibration documentation.
7616 int32_t rawX = 100;
7617 int32_t rawY = 200;
7618 int32_t rawTouchMajor = 7;
7619 int32_t rawTouchMinor = 6;
7620 int32_t rawToolMajor = 9;
7621 int32_t rawToolMinor = 8;
7622 int32_t rawPressure = 11;
7623 int32_t rawDistance = 0;
7624 int32_t rawOrientation = 3;
7625 int32_t id = 5;
7626
7627 float x = toDisplayX(rawX);
7628 float y = toDisplayY(rawY);
7629 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7630 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7631 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7632 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7633 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7634 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7635 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7636 float distance = float(rawDistance);
7637
7638 processPosition(mapper, rawX, rawY);
7639 processTouchMajor(mapper, rawTouchMajor);
7640 processTouchMinor(mapper, rawTouchMinor);
7641 processToolMajor(mapper, rawToolMajor);
7642 processToolMinor(mapper, rawToolMinor);
7643 processPressure(mapper, rawPressure);
7644 processOrientation(mapper, rawOrientation);
7645 processDistance(mapper, rawDistance);
7646 processId(mapper, id);
7647 processMTSync(mapper);
7648 processSync(mapper);
7649
7650 NotifyMotionArgs args;
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7652 ASSERT_EQ(0, args.pointerProperties[0].id);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7654 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7655 orientation, distance));
7656}
7657
7658TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 addConfigurationProperty("touch.deviceType", "touchScreen");
7660 prepareDisplay(DISPLAY_ORIENTATION_0);
7661 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7662 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007663 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664
7665 // These calculations are based on the input device calibration documentation.
7666 int32_t rawX = 100;
7667 int32_t rawY = 200;
7668 int32_t rawTouchMajor = 140;
7669 int32_t rawTouchMinor = 120;
7670 int32_t rawToolMajor = 180;
7671 int32_t rawToolMinor = 160;
7672
7673 float x = toDisplayX(rawX);
7674 float y = toDisplayY(rawY);
7675 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7676 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7677 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7678 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7679 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7680
7681 processPosition(mapper, rawX, rawY);
7682 processTouchMajor(mapper, rawTouchMajor);
7683 processTouchMinor(mapper, rawTouchMinor);
7684 processToolMajor(mapper, rawToolMajor);
7685 processToolMinor(mapper, rawToolMinor);
7686 processMTSync(mapper);
7687 processSync(mapper);
7688
7689 NotifyMotionArgs args;
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7692 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7693}
7694
7695TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 addConfigurationProperty("touch.deviceType", "touchScreen");
7697 prepareDisplay(DISPLAY_ORIENTATION_0);
7698 prepareAxes(POSITION | TOUCH | TOOL);
7699 addConfigurationProperty("touch.size.calibration", "diameter");
7700 addConfigurationProperty("touch.size.scale", "10");
7701 addConfigurationProperty("touch.size.bias", "160");
7702 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007704
7705 // These calculations are based on the input device calibration documentation.
7706 // Note: We only provide a single common touch/tool value because the device is assumed
7707 // not to emit separate values for each pointer (isSummed = 1).
7708 int32_t rawX = 100;
7709 int32_t rawY = 200;
7710 int32_t rawX2 = 150;
7711 int32_t rawY2 = 250;
7712 int32_t rawTouchMajor = 5;
7713 int32_t rawToolMajor = 8;
7714
7715 float x = toDisplayX(rawX);
7716 float y = toDisplayY(rawY);
7717 float x2 = toDisplayX(rawX2);
7718 float y2 = toDisplayY(rawY2);
7719 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7720 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7721 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7722
7723 processPosition(mapper, rawX, rawY);
7724 processTouchMajor(mapper, rawTouchMajor);
7725 processToolMajor(mapper, rawToolMajor);
7726 processMTSync(mapper);
7727 processPosition(mapper, rawX2, rawY2);
7728 processTouchMajor(mapper, rawTouchMajor);
7729 processToolMajor(mapper, rawToolMajor);
7730 processMTSync(mapper);
7731 processSync(mapper);
7732
7733 NotifyMotionArgs args;
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7736
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007738 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 ASSERT_EQ(size_t(2), args.pointerCount);
7740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7741 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7743 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7744}
7745
7746TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007747 addConfigurationProperty("touch.deviceType", "touchScreen");
7748 prepareDisplay(DISPLAY_ORIENTATION_0);
7749 prepareAxes(POSITION | TOUCH | TOOL);
7750 addConfigurationProperty("touch.size.calibration", "area");
7751 addConfigurationProperty("touch.size.scale", "43");
7752 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007753 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754
7755 // These calculations are based on the input device calibration documentation.
7756 int32_t rawX = 100;
7757 int32_t rawY = 200;
7758 int32_t rawTouchMajor = 5;
7759 int32_t rawToolMajor = 8;
7760
7761 float x = toDisplayX(rawX);
7762 float y = toDisplayY(rawY);
7763 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7764 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7765 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7766
7767 processPosition(mapper, rawX, rawY);
7768 processTouchMajor(mapper, rawTouchMajor);
7769 processToolMajor(mapper, rawToolMajor);
7770 processMTSync(mapper);
7771 processSync(mapper);
7772
7773 NotifyMotionArgs args;
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7776 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7777}
7778
7779TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 addConfigurationProperty("touch.deviceType", "touchScreen");
7781 prepareDisplay(DISPLAY_ORIENTATION_0);
7782 prepareAxes(POSITION | PRESSURE);
7783 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7784 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007785 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786
Michael Wrightaa449c92017-12-13 21:21:43 +00007787 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007788 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007789 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7790 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7791 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7792
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 // These calculations are based on the input device calibration documentation.
7794 int32_t rawX = 100;
7795 int32_t rawY = 200;
7796 int32_t rawPressure = 60;
7797
7798 float x = toDisplayX(rawX);
7799 float y = toDisplayY(rawY);
7800 float pressure = float(rawPressure) * 0.01f;
7801
7802 processPosition(mapper, rawX, rawY);
7803 processPressure(mapper, rawPressure);
7804 processMTSync(mapper);
7805 processSync(mapper);
7806
7807 NotifyMotionArgs args;
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7810 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7811}
7812
7813TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 addConfigurationProperty("touch.deviceType", "touchScreen");
7815 prepareDisplay(DISPLAY_ORIENTATION_0);
7816 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007817 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818
7819 NotifyMotionArgs motionArgs;
7820 NotifyKeyArgs keyArgs;
7821
7822 processId(mapper, 1);
7823 processPosition(mapper, 100, 200);
7824 processSync(mapper);
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7827 ASSERT_EQ(0, motionArgs.buttonState);
7828
7829 // press BTN_LEFT, release BTN_LEFT
7830 processKey(mapper, BTN_LEFT, 1);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7834 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7835
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7838 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7839
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 processKey(mapper, BTN_LEFT, 0);
7841 processSync(mapper);
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007845
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007848 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849
7850 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7851 processKey(mapper, BTN_RIGHT, 1);
7852 processKey(mapper, BTN_MIDDLE, 1);
7853 processSync(mapper);
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7856 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7857 motionArgs.buttonState);
7858
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7860 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7861 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7865 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7866 motionArgs.buttonState);
7867
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868 processKey(mapper, BTN_RIGHT, 0);
7869 processSync(mapper);
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007871 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007873
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007876 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007877
7878 processKey(mapper, BTN_MIDDLE, 0);
7879 processSync(mapper);
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007886 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 // press BTN_BACK, release BTN_BACK
7889 processKey(mapper, BTN_BACK, 1);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7892 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7893 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007894
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007897 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7898
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7901 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 processKey(mapper, BTN_BACK, 0);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007908
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_EQ(0, motionArgs.buttonState);
7912
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7914 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7915 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7916
7917 // press BTN_SIDE, release BTN_SIDE
7918 processKey(mapper, BTN_SIDE, 1);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7921 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7922 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007926 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7927
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7930 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931
7932 processKey(mapper, BTN_SIDE, 0);
7933 processSync(mapper);
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007937
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007940 ASSERT_EQ(0, motionArgs.buttonState);
7941
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7943 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7944 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7945
7946 // press BTN_FORWARD, release BTN_FORWARD
7947 processKey(mapper, BTN_FORWARD, 1);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7950 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7951 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007952
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007955 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7956
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7959 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960
7961 processKey(mapper, BTN_FORWARD, 0);
7962 processSync(mapper);
7963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007964 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007966
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007969 ASSERT_EQ(0, motionArgs.buttonState);
7970
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7972 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7973 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7974
7975 // press BTN_EXTRA, release BTN_EXTRA
7976 processKey(mapper, BTN_EXTRA, 1);
7977 processSync(mapper);
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7979 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7980 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007981
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7985
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7988 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007989
7990 processKey(mapper, BTN_EXTRA, 0);
7991 processSync(mapper);
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007995
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007998 ASSERT_EQ(0, motionArgs.buttonState);
7999
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8001 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8002 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8003
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8005
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 // press BTN_STYLUS, release BTN_STYLUS
8007 processKey(mapper, BTN_STYLUS, 1);
8008 processSync(mapper);
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8015 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016
8017 processKey(mapper, BTN_STYLUS, 0);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008022
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008025 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026
8027 // press BTN_STYLUS2, release BTN_STYLUS2
8028 processKey(mapper, BTN_STYLUS2, 1);
8029 processSync(mapper);
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008032 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037
8038 processKey(mapper, BTN_STYLUS2, 0);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047
8048 // release touch
8049 processId(mapper, -1);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8053 ASSERT_EQ(0, motionArgs.buttonState);
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 prepareDisplay(DISPLAY_ORIENTATION_0);
8059 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061
8062 NotifyMotionArgs motionArgs;
8063
8064 // default tool type is finger
8065 processId(mapper, 1);
8066 processPosition(mapper, 100, 200);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8071
8072 // eraser
8073 processKey(mapper, BTN_TOOL_RUBBER, 1);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8078
8079 // stylus
8080 processKey(mapper, BTN_TOOL_RUBBER, 0);
8081 processKey(mapper, BTN_TOOL_PEN, 1);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8086
8087 // brush
8088 processKey(mapper, BTN_TOOL_PEN, 0);
8089 processKey(mapper, BTN_TOOL_BRUSH, 1);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8094
8095 // pencil
8096 processKey(mapper, BTN_TOOL_BRUSH, 0);
8097 processKey(mapper, BTN_TOOL_PENCIL, 1);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8102
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008103 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 processKey(mapper, BTN_TOOL_PENCIL, 0);
8105 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8110
8111 // mouse
8112 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8113 processKey(mapper, BTN_TOOL_MOUSE, 1);
8114 processSync(mapper);
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8118
8119 // lens
8120 processKey(mapper, BTN_TOOL_MOUSE, 0);
8121 processKey(mapper, BTN_TOOL_LENS, 1);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8126
8127 // double-tap
8128 processKey(mapper, BTN_TOOL_LENS, 0);
8129 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8134
8135 // triple-tap
8136 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8137 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8142
8143 // quad-tap
8144 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8145 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8150
8151 // finger
8152 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8153 processKey(mapper, BTN_TOOL_FINGER, 1);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8158
8159 // stylus trumps finger
8160 processKey(mapper, BTN_TOOL_PEN, 1);
8161 processSync(mapper);
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8165
8166 // eraser trumps stylus
8167 processKey(mapper, BTN_TOOL_RUBBER, 1);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8172
8173 // mouse trumps eraser
8174 processKey(mapper, BTN_TOOL_MOUSE, 1);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8179
8180 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8181 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8186
8187 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8188 processToolType(mapper, MT_TOOL_PEN);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8193
8194 // back to default tool type
8195 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8196 processKey(mapper, BTN_TOOL_MOUSE, 0);
8197 processKey(mapper, BTN_TOOL_RUBBER, 0);
8198 processKey(mapper, BTN_TOOL_PEN, 0);
8199 processKey(mapper, BTN_TOOL_FINGER, 0);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8204}
8205
8206TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 addConfigurationProperty("touch.deviceType", "touchScreen");
8208 prepareDisplay(DISPLAY_ORIENTATION_0);
8209 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008210 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212
8213 NotifyMotionArgs motionArgs;
8214
8215 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8216 processId(mapper, 1);
8217 processPosition(mapper, 100, 200);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8222 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8223
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8227 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8228
8229 // move a little
8230 processPosition(mapper, 150, 250);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8235 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8236
8237 // down when BTN_TOUCH is pressed, pressure defaults to 1
8238 processKey(mapper, BTN_TOUCH, 1);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8243 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8248 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8249
8250 // up when BTN_TOUCH is released, hover restored
8251 processKey(mapper, BTN_TOUCH, 0);
8252 processSync(mapper);
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8256 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8257
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8261 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8262
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8266 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8267
8268 // exit hover when pointer goes away
8269 processId(mapper, -1);
8270 processSync(mapper);
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8275}
8276
8277TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 addConfigurationProperty("touch.deviceType", "touchScreen");
8279 prepareDisplay(DISPLAY_ORIENTATION_0);
8280 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008281 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282
8283 NotifyMotionArgs motionArgs;
8284
8285 // initially hovering because pressure is 0
8286 processId(mapper, 1);
8287 processPosition(mapper, 100, 200);
8288 processPressure(mapper, 0);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8293 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8294
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8299
8300 // move a little
8301 processPosition(mapper, 150, 250);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8306 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8307
8308 // down when pressure becomes non-zero
8309 processPressure(mapper, RAW_PRESSURE_MAX);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8314 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8315
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8319 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8320
8321 // up when pressure becomes 0, hover restored
8322 processPressure(mapper, 0);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8327 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8328
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8332 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8337 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8338
8339 // exit hover when pointer goes away
8340 processId(mapper, -1);
8341 processSync(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8346}
8347
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008348/**
8349 * Set the input device port <--> display port associations, and check that the
8350 * events are routed to the display that matches the display port.
8351 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8352 */
8353TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008354 const std::string usb2 = "USB2";
8355 const uint8_t hdmi1 = 0;
8356 const uint8_t hdmi2 = 1;
8357 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008358 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008359
8360 addConfigurationProperty("touch.deviceType", "touchScreen");
8361 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008363
8364 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8365 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8366
8367 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8368 // for this input device is specified, and the matching viewport is not present,
8369 // the input device should be disabled (at the mapper level).
8370
8371 // Add viewport for display 2 on hdmi2
8372 prepareSecondaryDisplay(type, hdmi2);
8373 // Send a touch event
8374 processPosition(mapper, 100, 100);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8377
8378 // Add viewport for display 1 on hdmi1
8379 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8380 // Send a touch event again
8381 processPosition(mapper, 100, 100);
8382 processSync(mapper);
8383
8384 NotifyMotionArgs args;
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8386 ASSERT_EQ(DISPLAY_ID, args.displayId);
8387}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008389TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8390 addConfigurationProperty("touch.deviceType", "touchScreen");
8391 prepareAxes(POSITION);
8392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8393
8394 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8395
8396 prepareDisplay(DISPLAY_ORIENTATION_0);
8397 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8398
8399 // Send a touch event
8400 processPosition(mapper, 100, 100);
8401 processSync(mapper);
8402
8403 NotifyMotionArgs args;
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8405 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8406}
8407
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008408TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008409 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008410 std::shared_ptr<FakePointerController> fakePointerController =
8411 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008412 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008413 fakePointerController->setPosition(100, 200);
8414 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008415 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008416
Garfield Tan888a6a42020-01-09 11:39:16 -08008417 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008418 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008419
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008420 prepareDisplay(DISPLAY_ORIENTATION_0);
8421 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008422 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008423
8424 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008425 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008426
8427 NotifyMotionArgs motionArgs;
8428 processPosition(mapper, 100, 100);
8429 processSync(mapper);
8430
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8433 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8434}
8435
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008436/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008437 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8438 */
8439TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8440 addConfigurationProperty("touch.deviceType", "touchScreen");
8441 prepareAxes(POSITION);
8442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8443
8444 prepareDisplay(DISPLAY_ORIENTATION_0);
8445 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8446 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8447 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8448 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8449
8450 NotifyMotionArgs args;
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8452 ASSERT_EQ(26, args.readTime);
8453
8454 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8455 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8456 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8457
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8459 ASSERT_EQ(33, args.readTime);
8460}
8461
8462/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008463 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8464 * events should not be delivered to the listener.
8465 */
8466TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8467 addConfigurationProperty("touch.deviceType", "touchScreen");
8468 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8469 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8470 ViewportType::INTERNAL);
8471 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8472 prepareAxes(POSITION);
8473 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8474
8475 NotifyMotionArgs motionArgs;
8476 processPosition(mapper, 100, 100);
8477 processSync(mapper);
8478
8479 mFakeListener->assertNotifyMotionWasNotCalled();
8480}
8481
Garfield Tanc734e4f2021-01-15 20:01:39 -08008482TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8483 addConfigurationProperty("touch.deviceType", "touchScreen");
8484 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8485 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8486 ViewportType::INTERNAL);
8487 std::optional<DisplayViewport> optionalDisplayViewport =
8488 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8489 ASSERT_TRUE(optionalDisplayViewport.has_value());
8490 DisplayViewport displayViewport = *optionalDisplayViewport;
8491
8492 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8493 prepareAxes(POSITION);
8494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8495
8496 // Finger down
8497 int32_t x = 100, y = 100;
8498 processPosition(mapper, x, y);
8499 processSync(mapper);
8500
8501 NotifyMotionArgs motionArgs;
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8503 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8504
8505 // Deactivate display viewport
8506 displayViewport.isActive = false;
8507 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8508 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8509
8510 // Finger move
8511 x += 10, y += 10;
8512 processPosition(mapper, x, y);
8513 processSync(mapper);
8514
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8516 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8517
8518 // Reactivate display viewport
8519 displayViewport.isActive = true;
8520 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8521 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8522
8523 // Finger move again
8524 x += 10, y += 10;
8525 processPosition(mapper, x, y);
8526 processSync(mapper);
8527
8528 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8529 // no pointer on the touch device.
8530 mFakeListener->assertNotifyMotionWasNotCalled();
8531}
8532
Arthur Hung7c645402019-01-25 17:45:42 +08008533TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8534 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008535 prepareAxes(POSITION | ID | SLOT);
8536 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008537 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008538
8539 // Create the second touch screen device, and enable multi fingers.
8540 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008541 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008542 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008543 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008544 std::shared_ptr<InputDevice> device2 =
8545 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008546 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008547
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008548 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8549 0 /*flat*/, 0 /*fuzz*/);
8550 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8551 0 /*flat*/, 0 /*fuzz*/);
8552 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8553 0 /*flat*/, 0 /*fuzz*/);
8554 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8555 0 /*flat*/, 0 /*fuzz*/);
8556 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8557 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8558 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008559
8560 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008561 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008562 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8563 device2->reset(ARBITRARY_TIME);
8564
8565 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008566 std::shared_ptr<FakePointerController> fakePointerController =
8567 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008568 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008569
8570 // Setup policy for associated displays and show touches.
8571 const uint8_t hdmi1 = 0;
8572 const uint8_t hdmi2 = 1;
8573 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8574 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8575 mFakePolicy->setShowTouches(true);
8576
8577 // Create displays.
8578 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008579 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008580
8581 // Default device will reconfigure above, need additional reconfiguration for another device.
8582 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008583 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008584
8585 // Two fingers down at default display.
8586 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8587 processPosition(mapper, x1, y1);
8588 processId(mapper, 1);
8589 processSlot(mapper, 1);
8590 processPosition(mapper, x2, y2);
8591 processId(mapper, 2);
8592 processSync(mapper);
8593
8594 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8595 fakePointerController->getSpots().find(DISPLAY_ID);
8596 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8597 ASSERT_EQ(size_t(2), iter->second.size());
8598
8599 // Two fingers down at second display.
8600 processPosition(mapper2, x1, y1);
8601 processId(mapper2, 1);
8602 processSlot(mapper2, 1);
8603 processPosition(mapper2, x2, y2);
8604 processId(mapper2, 2);
8605 processSync(mapper2);
8606
8607 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8608 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8609 ASSERT_EQ(size_t(2), iter->second.size());
8610}
8611
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008612TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008613 prepareAxes(POSITION);
8614 addConfigurationProperty("touch.deviceType", "touchScreen");
8615 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008617
8618 NotifyMotionArgs motionArgs;
8619 // Unrotated video frame
8620 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8621 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008622 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008623 processPosition(mapper, 100, 200);
8624 processSync(mapper);
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(frames, motionArgs.videoFrames);
8627
8628 // Subsequent touch events should not have any videoframes
8629 // This is implemented separately in FakeEventHub,
8630 // but that should match the behaviour of TouchVideoDevice.
8631 processPosition(mapper, 200, 200);
8632 processSync(mapper);
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8634 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8635}
8636
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008637TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008638 prepareAxes(POSITION);
8639 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008641 // Unrotated video frame
8642 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8643 NotifyMotionArgs motionArgs;
8644
8645 // Test all 4 orientations
8646 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008647 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8648 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8649 clearViewports();
8650 prepareDisplay(orientation);
8651 std::vector<TouchVideoFrame> frames{frame};
8652 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8653 processPosition(mapper, 100, 200);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(frames, motionArgs.videoFrames);
8657 }
8658}
8659
8660TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8661 prepareAxes(POSITION);
8662 addConfigurationProperty("touch.deviceType", "touchScreen");
8663 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8664 // orientation-aware are affected by display rotation.
8665 addConfigurationProperty("touch.orientationAware", "0");
8666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8667 // Unrotated video frame
8668 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8669 NotifyMotionArgs motionArgs;
8670
8671 // Test all 4 orientations
8672 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008673 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8674 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8675 clearViewports();
8676 prepareDisplay(orientation);
8677 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008678 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008679 processPosition(mapper, 100, 200);
8680 processSync(mapper);
8681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008682 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8683 // compared to the display. This is so that when the window transform (which contains the
8684 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8685 // window's coordinate space.
8686 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008687 ASSERT_EQ(frames, motionArgs.videoFrames);
8688 }
8689}
8690
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008691TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008692 prepareAxes(POSITION);
8693 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008694 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008695 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8696 // so mix these.
8697 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8698 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8699 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8700 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8701 NotifyMotionArgs motionArgs;
8702
8703 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008704 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008705 processPosition(mapper, 100, 200);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008708 ASSERT_EQ(frames, motionArgs.videoFrames);
8709}
8710
8711TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8712 prepareAxes(POSITION);
8713 addConfigurationProperty("touch.deviceType", "touchScreen");
8714 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8715 // orientation-aware are affected by display rotation.
8716 addConfigurationProperty("touch.orientationAware", "0");
8717 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8718 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8719 // so mix these.
8720 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8721 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8722 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8723 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8724 NotifyMotionArgs motionArgs;
8725
8726 prepareDisplay(DISPLAY_ORIENTATION_90);
8727 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8728 processPosition(mapper, 100, 200);
8729 processSync(mapper);
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8731 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8732 // 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 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8737 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008738 ASSERT_EQ(frames, motionArgs.videoFrames);
8739}
8740
Arthur Hung9da14732019-09-02 16:16:58 +08008741/**
8742 * If we had defined port associations, but the viewport is not ready, the touch device would be
8743 * expected to be disabled, and it should be enabled after the viewport has found.
8744 */
8745TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008746 constexpr uint8_t hdmi2 = 1;
8747 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008748 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008749
8750 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8751
8752 addConfigurationProperty("touch.deviceType", "touchScreen");
8753 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008754 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008755
8756 ASSERT_EQ(mDevice->isEnabled(), false);
8757
8758 // Add display on hdmi2, the device should be enabled and can receive touch event.
8759 prepareSecondaryDisplay(type, hdmi2);
8760 ASSERT_EQ(mDevice->isEnabled(), true);
8761
8762 // Send a touch event.
8763 processPosition(mapper, 100, 100);
8764 processSync(mapper);
8765
8766 NotifyMotionArgs args;
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8768 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8769}
8770
Arthur Hung421eb1c2020-01-16 00:09:42 +08008771TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008772 addConfigurationProperty("touch.deviceType", "touchScreen");
8773 prepareDisplay(DISPLAY_ORIENTATION_0);
8774 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008775 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008776
8777 NotifyMotionArgs motionArgs;
8778
8779 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8780 // finger down
8781 processId(mapper, 1);
8782 processPosition(mapper, x1, y1);
8783 processSync(mapper);
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8787
8788 // finger move
8789 processId(mapper, 1);
8790 processPosition(mapper, x2, y2);
8791 processSync(mapper);
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8795
8796 // finger up.
8797 processId(mapper, -1);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8802
8803 // new finger down
8804 processId(mapper, 1);
8805 processPosition(mapper, x3, y3);
8806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8810}
8811
8812/**
arthurhungcc7f9802020-04-30 17:55:40 +08008813 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8814 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008815 */
arthurhungcc7f9802020-04-30 17:55:40 +08008816TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008817 addConfigurationProperty("touch.deviceType", "touchScreen");
8818 prepareDisplay(DISPLAY_ORIENTATION_0);
8819 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008821
8822 NotifyMotionArgs motionArgs;
8823
8824 // default tool type is finger
8825 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008826 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008827 processPosition(mapper, x1, y1);
8828 processSync(mapper);
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8832
8833 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8834 processToolType(mapper, MT_TOOL_PALM);
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8838
8839 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008840 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008841 processPosition(mapper, x2, y2);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8844
8845 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008846 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008847 processSync(mapper);
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8849
8850 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008851 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008852 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008853 processPosition(mapper, x3, y3);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8858}
8859
arthurhungbf89a482020-04-17 17:37:55 +08008860/**
arthurhungcc7f9802020-04-30 17:55:40 +08008861 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8862 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008863 */
arthurhungcc7f9802020-04-30 17:55:40 +08008864TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008865 addConfigurationProperty("touch.deviceType", "touchScreen");
8866 prepareDisplay(DISPLAY_ORIENTATION_0);
8867 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8868 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8869
8870 NotifyMotionArgs motionArgs;
8871
8872 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008873 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8874 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008875 processPosition(mapper, x1, y1);
8876 processSync(mapper);
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8880
8881 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008882 processSlot(mapper, SECOND_SLOT);
8883 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008884 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008887 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8889
8890 // If the tool type of the first finger changes to MT_TOOL_PALM,
8891 // we expect to receive ACTION_POINTER_UP with cancel flag.
8892 processSlot(mapper, FIRST_SLOT);
8893 processId(mapper, FIRST_TRACKING_ID);
8894 processToolType(mapper, MT_TOOL_PALM);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008897 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008898 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8899
8900 // The following MOVE events of second finger should be processed.
8901 processSlot(mapper, SECOND_SLOT);
8902 processId(mapper, SECOND_TRACKING_ID);
8903 processPosition(mapper, x2 + 1, y2 + 1);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8906 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8907 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8908
8909 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8910 // it. Second finger receive move.
8911 processSlot(mapper, FIRST_SLOT);
8912 processId(mapper, INVALID_TRACKING_ID);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8916 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8917
8918 // Second finger keeps moving.
8919 processSlot(mapper, SECOND_SLOT);
8920 processId(mapper, SECOND_TRACKING_ID);
8921 processPosition(mapper, x2 + 2, y2 + 2);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8925 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8926
8927 // Second finger up.
8928 processId(mapper, INVALID_TRACKING_ID);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8932 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8933}
8934
8935/**
8936 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8937 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8938 */
8939TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8940 addConfigurationProperty("touch.deviceType", "touchScreen");
8941 prepareDisplay(DISPLAY_ORIENTATION_0);
8942 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8943 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8944
8945 NotifyMotionArgs motionArgs;
8946
8947 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8948 // First finger down.
8949 processId(mapper, FIRST_TRACKING_ID);
8950 processPosition(mapper, x1, y1);
8951 processSync(mapper);
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8955
8956 // Second finger down.
8957 processSlot(mapper, SECOND_SLOT);
8958 processId(mapper, SECOND_TRACKING_ID);
8959 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008962 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8964
arthurhungcc7f9802020-04-30 17:55:40 +08008965 // If the tool type of the first finger changes to MT_TOOL_PALM,
8966 // we expect to receive ACTION_POINTER_UP with cancel flag.
8967 processSlot(mapper, FIRST_SLOT);
8968 processId(mapper, FIRST_TRACKING_ID);
8969 processToolType(mapper, MT_TOOL_PALM);
8970 processSync(mapper);
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008972 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008973 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8974
8975 // Second finger keeps moving.
8976 processSlot(mapper, SECOND_SLOT);
8977 processId(mapper, SECOND_TRACKING_ID);
8978 processPosition(mapper, x2 + 1, y2 + 1);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8982
8983 // second finger becomes palm, receive cancel due to only 1 finger is active.
8984 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008985 processToolType(mapper, MT_TOOL_PALM);
8986 processSync(mapper);
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8988 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8989
arthurhungcc7f9802020-04-30 17:55:40 +08008990 // third finger down.
8991 processSlot(mapper, THIRD_SLOT);
8992 processId(mapper, THIRD_TRACKING_ID);
8993 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008994 processPosition(mapper, x3, y3);
8995 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008999 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9000
9001 // third finger move
9002 processId(mapper, THIRD_TRACKING_ID);
9003 processPosition(mapper, x3 + 1, y3 + 1);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9007
9008 // first finger up, third finger receive move.
9009 processSlot(mapper, FIRST_SLOT);
9010 processId(mapper, INVALID_TRACKING_ID);
9011 processSync(mapper);
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9014 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9015
9016 // second finger up, third finger receive move.
9017 processSlot(mapper, SECOND_SLOT);
9018 processId(mapper, INVALID_TRACKING_ID);
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9022 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9023
9024 // third finger up.
9025 processSlot(mapper, THIRD_SLOT);
9026 processId(mapper, INVALID_TRACKING_ID);
9027 processSync(mapper);
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9030 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9031}
9032
9033/**
9034 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9035 * and the active finger could still be allowed to receive the events
9036 */
9037TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9038 addConfigurationProperty("touch.deviceType", "touchScreen");
9039 prepareDisplay(DISPLAY_ORIENTATION_0);
9040 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9042
9043 NotifyMotionArgs motionArgs;
9044
9045 // default tool type is finger
9046 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9047 processId(mapper, FIRST_TRACKING_ID);
9048 processPosition(mapper, x1, y1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9053
9054 // Second finger down.
9055 processSlot(mapper, SECOND_SLOT);
9056 processId(mapper, SECOND_TRACKING_ID);
9057 processPosition(mapper, x2, y2);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009060 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9062
9063 // If the tool type of the second finger changes to MT_TOOL_PALM,
9064 // we expect to receive ACTION_POINTER_UP with cancel flag.
9065 processId(mapper, SECOND_TRACKING_ID);
9066 processToolType(mapper, MT_TOOL_PALM);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009069 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009070 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9071
9072 // The following MOVE event should be processed.
9073 processSlot(mapper, FIRST_SLOT);
9074 processId(mapper, FIRST_TRACKING_ID);
9075 processPosition(mapper, x1 + 1, y1 + 1);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9079 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9080
9081 // second finger up.
9082 processSlot(mapper, SECOND_SLOT);
9083 processId(mapper, INVALID_TRACKING_ID);
9084 processSync(mapper);
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9087
9088 // first finger keep moving
9089 processSlot(mapper, FIRST_SLOT);
9090 processId(mapper, FIRST_TRACKING_ID);
9091 processPosition(mapper, x1 + 2, y1 + 2);
9092 processSync(mapper);
9093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9095
9096 // first finger up.
9097 processId(mapper, INVALID_TRACKING_ID);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9101 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009102}
9103
Arthur Hung9ad18942021-06-19 02:04:46 +00009104/**
9105 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9106 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9107 * cause slot be valid again.
9108 */
9109TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9110 addConfigurationProperty("touch.deviceType", "touchScreen");
9111 prepareDisplay(DISPLAY_ORIENTATION_0);
9112 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9113 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9114
9115 NotifyMotionArgs motionArgs;
9116
9117 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9118 // First finger down.
9119 processId(mapper, FIRST_TRACKING_ID);
9120 processPosition(mapper, x1, y1);
9121 processPressure(mapper, RAW_PRESSURE_MAX);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9125 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9126
9127 // First finger move.
9128 processId(mapper, FIRST_TRACKING_ID);
9129 processPosition(mapper, x1 + 1, y1 + 1);
9130 processPressure(mapper, RAW_PRESSURE_MAX);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9134 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9135
9136 // Second finger down.
9137 processSlot(mapper, SECOND_SLOT);
9138 processId(mapper, SECOND_TRACKING_ID);
9139 processPosition(mapper, x2, y2);
9140 processPressure(mapper, RAW_PRESSURE_MAX);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009143 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009144 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9145
9146 // second finger up with some unexpected data.
9147 processSlot(mapper, SECOND_SLOT);
9148 processId(mapper, INVALID_TRACKING_ID);
9149 processPosition(mapper, x2, y2);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009152 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009153 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9154
9155 // first finger up with some unexpected data.
9156 processSlot(mapper, FIRST_SLOT);
9157 processId(mapper, INVALID_TRACKING_ID);
9158 processPosition(mapper, x2, y2);
9159 processPressure(mapper, RAW_PRESSURE_MAX);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9163 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9164}
9165
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009166// --- MultiTouchInputMapperTest_ExternalDevice ---
9167
9168class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9169protected:
Chris Yea52ade12020-08-27 16:49:20 -07009170 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009171};
9172
9173/**
9174 * Expect fallback to internal viewport if device is external and external viewport is not present.
9175 */
9176TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9177 prepareAxes(POSITION);
9178 addConfigurationProperty("touch.deviceType", "touchScreen");
9179 prepareDisplay(DISPLAY_ORIENTATION_0);
9180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9181
9182 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9183
9184 NotifyMotionArgs motionArgs;
9185
9186 // Expect the event to be sent to the internal viewport,
9187 // because an external viewport is not present.
9188 processPosition(mapper, 100, 100);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9192
9193 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009194 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009195 processPosition(mapper, 100, 100);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9199}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009200
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009201TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9202 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9203 std::shared_ptr<FakePointerController> fakePointerController =
9204 std::make_shared<FakePointerController>();
9205 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9206 fakePointerController->setPosition(0, 0);
9207 fakePointerController->setButtonState(0);
9208
9209 // prepare device and capture
9210 prepareDisplay(DISPLAY_ORIENTATION_0);
9211 prepareAxes(POSITION | ID | SLOT);
9212 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9213 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9214 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009215 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009216 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9217
9218 // captured touchpad should be a touchpad source
9219 NotifyDeviceResetArgs resetArgs;
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9221 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9222
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009223 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009224
9225 const InputDeviceInfo::MotionRange* relRangeX =
9226 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9227 ASSERT_NE(relRangeX, nullptr);
9228 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9229 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9230 const InputDeviceInfo::MotionRange* relRangeY =
9231 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9232 ASSERT_NE(relRangeY, nullptr);
9233 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9234 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9235
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009236 // run captured pointer tests - note that this is unscaled, so input listener events should be
9237 // identical to what the hardware sends (accounting for any
9238 // calibration).
9239 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009240 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009241 processId(mapper, 1);
9242 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9243 processKey(mapper, BTN_TOUCH, 1);
9244 processSync(mapper);
9245
9246 // expect coord[0] to contain initial location of touch 0
9247 NotifyMotionArgs args;
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9250 ASSERT_EQ(1U, args.pointerCount);
9251 ASSERT_EQ(0, args.pointerProperties[0].id);
9252 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9253 ASSERT_NO_FATAL_FAILURE(
9254 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9255
9256 // FINGER 1 DOWN
9257 processSlot(mapper, 1);
9258 processId(mapper, 2);
9259 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9260 processSync(mapper);
9261
9262 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009264 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009265 ASSERT_EQ(2U, args.pointerCount);
9266 ASSERT_EQ(0, args.pointerProperties[0].id);
9267 ASSERT_EQ(1, args.pointerProperties[1].id);
9268 ASSERT_NO_FATAL_FAILURE(
9269 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9270 ASSERT_NO_FATAL_FAILURE(
9271 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9272
9273 // FINGER 1 MOVE
9274 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9275 processSync(mapper);
9276
9277 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9278 // from move
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9281 ASSERT_NO_FATAL_FAILURE(
9282 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9283 ASSERT_NO_FATAL_FAILURE(
9284 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9285
9286 // FINGER 0 MOVE
9287 processSlot(mapper, 0);
9288 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9289 processSync(mapper);
9290
9291 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9294 ASSERT_NO_FATAL_FAILURE(
9295 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9296 ASSERT_NO_FATAL_FAILURE(
9297 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9298
9299 // BUTTON DOWN
9300 processKey(mapper, BTN_LEFT, 1);
9301 processSync(mapper);
9302
9303 // touchinputmapper design sends a move before button press
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9308
9309 // BUTTON UP
9310 processKey(mapper, BTN_LEFT, 0);
9311 processSync(mapper);
9312
9313 // touchinputmapper design sends a move after button release
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9318
9319 // FINGER 0 UP
9320 processId(mapper, -1);
9321 processSync(mapper);
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9323 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9324
9325 // FINGER 1 MOVE
9326 processSlot(mapper, 1);
9327 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9328 processSync(mapper);
9329
9330 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9333 ASSERT_EQ(1U, args.pointerCount);
9334 ASSERT_EQ(1, args.pointerProperties[0].id);
9335 ASSERT_NO_FATAL_FAILURE(
9336 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9337
9338 // FINGER 1 UP
9339 processId(mapper, -1);
9340 processKey(mapper, BTN_TOUCH, 0);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9344
9345 // non captured touchpad should be a mouse source
9346 mFakePolicy->setPointerCapture(false);
9347 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9349 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9350}
9351
9352TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9353 std::shared_ptr<FakePointerController> fakePointerController =
9354 std::make_shared<FakePointerController>();
9355 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9356 fakePointerController->setPosition(0, 0);
9357 fakePointerController->setButtonState(0);
9358
9359 // prepare device and capture
9360 prepareDisplay(DISPLAY_ORIENTATION_0);
9361 prepareAxes(POSITION | ID | SLOT);
9362 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9363 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009364 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009365 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9366 // run uncaptured pointer tests - pushes out generic events
9367 // FINGER 0 DOWN
9368 processId(mapper, 3);
9369 processPosition(mapper, 100, 100);
9370 processKey(mapper, BTN_TOUCH, 1);
9371 processSync(mapper);
9372
9373 // start at (100,100), cursor should be at (0,0) * scale
9374 NotifyMotionArgs args;
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9377 ASSERT_NO_FATAL_FAILURE(
9378 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9379
9380 // FINGER 0 MOVE
9381 processPosition(mapper, 200, 200);
9382 processSync(mapper);
9383
9384 // compute scaling to help with touch position checking
9385 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9386 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9387 float scale =
9388 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9389
9390 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9394 0, 0, 0, 0, 0, 0, 0));
9395}
9396
9397TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9398 std::shared_ptr<FakePointerController> fakePointerController =
9399 std::make_shared<FakePointerController>();
9400
9401 prepareDisplay(DISPLAY_ORIENTATION_0);
9402 prepareAxes(POSITION | ID | SLOT);
9403 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009404 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009405 mFakePolicy->setPointerCapture(false);
9406 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9407
9408 // uncaptured touchpad should be a pointer device
9409 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9410
9411 // captured touchpad should be a touchpad device
9412 mFakePolicy->setPointerCapture(true);
9413 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9414 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9415}
9416
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009417// --- JoystickInputMapperTest ---
9418
9419class JoystickInputMapperTest : public InputMapperTest {
9420protected:
9421 static const int32_t RAW_X_MIN;
9422 static const int32_t RAW_X_MAX;
9423 static const int32_t RAW_Y_MIN;
9424 static const int32_t RAW_Y_MAX;
9425
9426 void SetUp() override {
9427 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9428 }
9429 void prepareAxes() {
9430 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9432 }
9433
9434 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9436 }
9437
9438 void processSync(JoystickInputMapper& mapper) {
9439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9440 }
9441
9442 void prepareVirtualDisplay(int32_t orientation) {
9443 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9444 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9445 NO_PORT, ViewportType::VIRTUAL);
9446 }
9447};
9448
9449const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9450const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9451const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9452const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9453
9454TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9455 prepareAxes();
9456 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9457
9458 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9459
9460 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9461
9462 // Send an axis event
9463 processAxis(mapper, ABS_X, 100);
9464 processSync(mapper);
9465
9466 NotifyMotionArgs args;
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9468 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9469
9470 // Send another axis event
9471 processAxis(mapper, ABS_Y, 100);
9472 processSync(mapper);
9473
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9475 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9476}
9477
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009478// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009479
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009480class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009481protected:
9482 static const char* DEVICE_NAME;
9483 static const char* DEVICE_LOCATION;
9484 static const int32_t DEVICE_ID;
9485 static const int32_t DEVICE_GENERATION;
9486 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009487 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009488 static const int32_t EVENTHUB_ID;
9489
9490 std::shared_ptr<FakeEventHub> mFakeEventHub;
9491 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009492 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009493 std::unique_ptr<InstrumentedInputReader> mReader;
9494 std::shared_ptr<InputDevice> mDevice;
9495
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009496 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009497 mFakeEventHub = std::make_unique<FakeEventHub>();
9498 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009499 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009500 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009501 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009502 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9503 }
9504
9505 void SetUp() override { SetUp(DEVICE_CLASSES); }
9506
9507 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009508 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009509 mFakePolicy.clear();
9510 }
9511
9512 void configureDevice(uint32_t changes) {
9513 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9514 mReader->requestRefreshConfiguration(changes);
9515 mReader->loopOnce();
9516 }
9517 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9518 }
9519
9520 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9521 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009522 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009523 InputDeviceIdentifier identifier;
9524 identifier.name = name;
9525 identifier.location = location;
9526 std::shared_ptr<InputDevice> device =
9527 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9528 identifier);
9529 mReader->pushNextDevice(device);
9530 mFakeEventHub->addDevice(eventHubId, name, classes);
9531 mReader->loopOnce();
9532 return device;
9533 }
9534
9535 template <class T, typename... Args>
9536 T& addControllerAndConfigure(Args... args) {
9537 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9538
9539 return controller;
9540 }
9541};
9542
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009543const char* PeripheralControllerTest::DEVICE_NAME = "device";
9544const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9545const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9546const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9547const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009548const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9549 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009550const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009551
9552// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009553class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009554protected:
9555 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009556 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009557 }
9558};
9559
9560TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009561 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009562
9563 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9564 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9565}
9566
9567TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009568 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009569
9570 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9571 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9572}
9573
9574// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009575class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009576protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009577 void SetUp() override {
9578 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9579 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009580};
9581
Chris Ye85758332021-05-16 23:05:17 -07009582TEST_F(LightControllerTest, MonoLight) {
9583 RawLightInfo infoMono = {.id = 1,
9584 .name = "Mono",
9585 .maxBrightness = 255,
9586 .flags = InputLightClass::BRIGHTNESS,
9587 .path = ""};
9588 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009589
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009590 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009591 InputDeviceInfo info;
9592 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009593 std::vector<InputDeviceLightInfo> lights = info.getLights();
9594 ASSERT_EQ(1U, lights.size());
9595 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009596
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009597 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9598 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009599}
9600
9601TEST_F(LightControllerTest, RGBLight) {
9602 RawLightInfo infoRed = {.id = 1,
9603 .name = "red",
9604 .maxBrightness = 255,
9605 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9606 .path = ""};
9607 RawLightInfo infoGreen = {.id = 2,
9608 .name = "green",
9609 .maxBrightness = 255,
9610 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9611 .path = ""};
9612 RawLightInfo infoBlue = {.id = 3,
9613 .name = "blue",
9614 .maxBrightness = 255,
9615 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9616 .path = ""};
9617 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9618 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9619 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9620
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009621 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009622 InputDeviceInfo info;
9623 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009624 std::vector<InputDeviceLightInfo> lights = info.getLights();
9625 ASSERT_EQ(1U, lights.size());
9626 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009627
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009628 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9629 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009630}
9631
9632TEST_F(LightControllerTest, MultiColorRGBLight) {
9633 RawLightInfo infoColor = {.id = 1,
9634 .name = "red",
9635 .maxBrightness = 255,
9636 .flags = InputLightClass::BRIGHTNESS |
9637 InputLightClass::MULTI_INTENSITY |
9638 InputLightClass::MULTI_INDEX,
9639 .path = ""};
9640
9641 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9642
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009643 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009644 InputDeviceInfo info;
9645 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009646 std::vector<InputDeviceLightInfo> lights = info.getLights();
9647 ASSERT_EQ(1U, lights.size());
9648 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009649
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009650 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9651 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009652}
9653
9654TEST_F(LightControllerTest, PlayerIdLight) {
9655 RawLightInfo info1 = {.id = 1,
9656 .name = "player1",
9657 .maxBrightness = 255,
9658 .flags = InputLightClass::BRIGHTNESS,
9659 .path = ""};
9660 RawLightInfo info2 = {.id = 2,
9661 .name = "player2",
9662 .maxBrightness = 255,
9663 .flags = InputLightClass::BRIGHTNESS,
9664 .path = ""};
9665 RawLightInfo info3 = {.id = 3,
9666 .name = "player3",
9667 .maxBrightness = 255,
9668 .flags = InputLightClass::BRIGHTNESS,
9669 .path = ""};
9670 RawLightInfo info4 = {.id = 4,
9671 .name = "player4",
9672 .maxBrightness = 255,
9673 .flags = InputLightClass::BRIGHTNESS,
9674 .path = ""};
9675 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9676 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9677 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9678 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9679
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009680 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009681 InputDeviceInfo info;
9682 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009683 std::vector<InputDeviceLightInfo> lights = info.getLights();
9684 ASSERT_EQ(1U, lights.size());
9685 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009686
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009687 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9688 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9689 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009690}
9691
Michael Wrightd02c5b62014-02-10 15:10:22 -08009692} // namespace android