blob: a2d9c8d4d715059b0600bcefc5b8efc0a6a1bcb8 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000036#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070037#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050039#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048using std::chrono_literals::operator""ms;
49
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Prabir Pradhan197e0862022-07-01 14:28:00 +0000222 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
HQ Liue6983c72022-04-19 22:14:56 +0000376 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
377
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800378 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
379
HQ Liue6983c72022-04-19 22:14:56 +0000380 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
381
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000382 void setVelocityControlParams(const VelocityControlParameters& params) {
383 mConfig.pointerVelocityControlParameters = params;
384 mConfig.wheelVelocityControlParameters = params;
385 }
386
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000388 uint32_t mNextPointerCaptureSequenceNumber = 0;
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 *outConfig = mConfig;
392 }
393
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000394 std::shared_ptr<PointerControllerInterface> obtainPointerController(
395 int32_t /*deviceId*/) override {
396 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800397 }
398
Chris Yea52ade12020-08-27 16:49:20 -0700399 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700400 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800401 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700402 mInputDevicesChanged = true;
403 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800404 }
405
Chris Yea52ade12020-08-27 16:49:20 -0700406 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
407 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700408 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 }
410
Chris Yea52ade12020-08-27 16:49:20 -0700411 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800412
413 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
414 std::unique_lock<std::mutex> lock(mLock);
415 base::ScopedLockAssertion assumeLocked(mLock);
416
417 const bool devicesChanged =
418 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
419 return mInputDevicesChanged;
420 });
421 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
422 mInputDevicesChanged = false;
423 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800424};
425
Michael Wrightd02c5b62014-02-10 15:10:22 -0800426// --- FakeEventHub ---
427
428class FakeEventHub : public EventHubInterface {
429 struct KeyInfo {
430 int32_t keyCode;
431 uint32_t flags;
432 };
433
Chris Yef59a2f42020-10-16 12:55:26 -0700434 struct SensorInfo {
435 InputDeviceSensorType sensorType;
436 int32_t sensorDataIndex;
437 };
438
Michael Wrightd02c5b62014-02-10 15:10:22 -0800439 struct Device {
440 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700441 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442 PropertyMap configuration;
443 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
444 KeyedVector<int, bool> relativeAxes;
445 KeyedVector<int32_t, int32_t> keyCodeStates;
446 KeyedVector<int32_t, int32_t> scanCodeStates;
447 KeyedVector<int32_t, int32_t> switchStates;
448 KeyedVector<int32_t, int32_t> absoluteAxisValue;
449 KeyedVector<int32_t, KeyInfo> keysByScanCode;
450 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
451 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100452 // fake mapping which would normally come from keyCharacterMap
453 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700454 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
455 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800456 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700457 bool enabled;
458
459 status_t enable() {
460 enabled = true;
461 return OK;
462 }
463
464 status_t disable() {
465 enabled = false;
466 return OK;
467 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800468
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700469 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800470 };
471
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700472 std::mutex mLock;
473 std::condition_variable mEventsCondition;
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100476 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000477 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600478 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000479 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800480 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
481 // Simulates a device light brightness, from light id to light brightness.
482 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
483 // Simulates a device light intensities, from light id to light intensities map.
484 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
485 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800486
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700487public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 virtual ~FakeEventHub() {
489 for (size_t i = 0; i < mDevices.size(); i++) {
490 delete mDevices.valueAt(i);
491 }
492 }
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 FakeEventHub() { }
495
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700496 void addDevice(int32_t deviceId, const std::string& name,
497 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800498 Device* device = new Device(classes);
499 device->identifier.name = name;
500 mDevices.add(deviceId, device);
501
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000502 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800503 }
504
505 void removeDevice(int32_t deviceId) {
506 delete mDevices.valueFor(deviceId);
507 mDevices.removeItem(deviceId);
508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000509 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 }
511
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 bool isDeviceEnabled(int32_t deviceId) {
513 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700514 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700515 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
516 return false;
517 }
518 return device->enabled;
519 }
520
521 status_t enableDevice(int32_t deviceId) {
522 status_t result;
523 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700524 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700525 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
526 return BAD_VALUE;
527 }
528 if (device->enabled) {
529 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
530 return OK;
531 }
532 result = device->enable();
533 return result;
534 }
535
536 status_t disableDevice(int32_t deviceId) {
537 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700538 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700539 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
540 return BAD_VALUE;
541 }
542 if (!device->enabled) {
543 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
544 return OK;
545 }
546 return device->disable();
547 }
548
Michael Wrightd02c5b62014-02-10 15:10:22 -0800549 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000550 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800551 }
552
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700553 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800554 Device* device = getDevice(deviceId);
555 device->configuration.addProperty(key, value);
556 }
557
558 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
559 Device* device = getDevice(deviceId);
560 device->configuration.addAll(configuration);
561 }
562
563 void addAbsoluteAxis(int32_t deviceId, int axis,
564 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
565 Device* device = getDevice(deviceId);
566
567 RawAbsoluteAxisInfo info;
568 info.valid = true;
569 info.minValue = minValue;
570 info.maxValue = maxValue;
571 info.flat = flat;
572 info.fuzz = fuzz;
573 info.resolution = resolution;
574 device->absoluteAxes.add(axis, info);
575 }
576
577 void addRelativeAxis(int32_t deviceId, int32_t axis) {
578 Device* device = getDevice(deviceId);
579 device->relativeAxes.add(axis, true);
580 }
581
582 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
583 Device* device = getDevice(deviceId);
584 device->keyCodeStates.replaceValueFor(keyCode, state);
585 }
586
587 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
588 Device* device = getDevice(deviceId);
589 device->scanCodeStates.replaceValueFor(scanCode, state);
590 }
591
592 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
593 Device* device = getDevice(deviceId);
594 device->switchStates.replaceValueFor(switchCode, state);
595 }
596
597 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
598 Device* device = getDevice(deviceId);
599 device->absoluteAxisValue.replaceValueFor(axis, value);
600 }
601
602 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
603 int32_t keyCode, uint32_t flags) {
604 Device* device = getDevice(deviceId);
605 KeyInfo info;
606 info.keyCode = keyCode;
607 info.flags = flags;
608 if (scanCode) {
609 device->keysByScanCode.add(scanCode, info);
610 }
611 if (usageCode) {
612 device->keysByUsageCode.add(usageCode, info);
613 }
614 }
615
Philip Junker4af3b3d2021-12-14 10:36:55 +0100616 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
617 Device* device = getDevice(deviceId);
618 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
619 }
620
Michael Wrightd02c5b62014-02-10 15:10:22 -0800621 void addLed(int32_t deviceId, int32_t led, bool initialState) {
622 Device* device = getDevice(deviceId);
623 device->leds.add(led, initialState);
624 }
625
Chris Yef59a2f42020-10-16 12:55:26 -0700626 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
627 int32_t sensorDataIndex) {
628 Device* device = getDevice(deviceId);
629 SensorInfo info;
630 info.sensorType = sensorType;
631 info.sensorDataIndex = sensorDataIndex;
632 device->sensorsByAbsCode.emplace(absCode, info);
633 }
634
635 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
636 Device* device = getDevice(deviceId);
637 typename BitArray<MSC_MAX>::Buffer buffer;
638 buffer[mscEvent / 32] = 1 << mscEvent % 32;
639 device->mscBitmask.loadFromBuffer(buffer);
640 }
641
Chris Ye3fdbfef2021-01-06 18:45:18 -0800642 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
643 mRawLightInfos.emplace(rawId, std::move(info));
644 }
645
646 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
647 mLightBrightness.emplace(rawId, brightness);
648 }
649
650 void fakeLightIntensities(int32_t rawId,
651 const std::unordered_map<LightColor, int32_t> intensities) {
652 mLightIntensities.emplace(rawId, std::move(intensities));
653 }
654
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 bool getLedState(int32_t deviceId, int32_t led) {
656 Device* device = getDevice(deviceId);
657 return device->leds.valueFor(led);
658 }
659
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 return mExcludedDevices;
662 }
663
664 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
665 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800666 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 }
668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000669 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
670 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700671 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672 RawEvent event;
673 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000674 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800675 event.deviceId = deviceId;
676 event.type = type;
677 event.code = code;
678 event.value = value;
679 mEvents.push_back(event);
680
681 if (type == EV_ABS) {
682 setAbsoluteAxisValue(deviceId, code, value);
683 }
684 }
685
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600686 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
687 std::vector<TouchVideoFrame>> videoFrames) {
688 mVideoFrames = std::move(videoFrames);
689 }
690
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700692 std::unique_lock<std::mutex> lock(mLock);
693 base::ScopedLockAssertion assumeLocked(mLock);
694 const bool queueIsEmpty =
695 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
696 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
697 if (!queueIsEmpty) {
698 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
702private:
703 Device* getDevice(int32_t deviceId) const {
704 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100705 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800706 }
707
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700708 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700710 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 }
712
Chris Yea52ade12020-08-27 16:49:20 -0700713 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 Device* device = getDevice(deviceId);
715 return device ? device->identifier : InputDeviceIdentifier();
716 }
717
Chris Yea52ade12020-08-27 16:49:20 -0700718 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719
Chris Yea52ade12020-08-27 16:49:20 -0700720 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 Device* device = getDevice(deviceId);
722 if (device) {
723 *outConfiguration = device->configuration;
724 }
725 }
726
Chris Yea52ade12020-08-27 16:49:20 -0700727 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
728 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800729 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800730 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 ssize_t index = device->absoluteAxes.indexOfKey(axis);
732 if (index >= 0) {
733 *outAxisInfo = device->absoluteAxes.valueAt(index);
734 return OK;
735 }
736 }
737 outAxisInfo->clear();
738 return -1;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 Device* device = getDevice(deviceId);
743 if (device) {
744 return device->relativeAxes.indexOfKey(axis) >= 0;
745 }
746 return false;
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Chris Yef59a2f42020-10-16 12:55:26 -0700751 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
752 Device* device = getDevice(deviceId);
753 if (device) {
754 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
755 }
756 return false;
757 }
758
Chris Yea52ade12020-08-27 16:49:20 -0700759 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
760 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761 Device* device = getDevice(deviceId);
762 if (device) {
763 const KeyInfo* key = getKey(device, scanCode, usageCode);
764 if (key) {
765 if (outKeycode) {
766 *outKeycode = key->keyCode;
767 }
768 if (outFlags) {
769 *outFlags = key->flags;
770 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700771 if (outMetaState) {
772 *outMetaState = metaState;
773 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774 return OK;
775 }
776 }
777 return NAME_NOT_FOUND;
778 }
779
780 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
781 if (usageCode) {
782 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
783 if (index >= 0) {
784 return &device->keysByUsageCode.valueAt(index);
785 }
786 }
787 if (scanCode) {
788 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
789 if (index >= 0) {
790 return &device->keysByScanCode.valueAt(index);
791 }
792 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700793 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794 }
795
Chris Yea52ade12020-08-27 16:49:20 -0700796 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
Chris Yef59a2f42020-10-16 12:55:26 -0700798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
799 int32_t absCode) {
800 Device* device = getDevice(deviceId);
801 if (!device) {
802 return Errorf("Sensor device not found.");
803 }
804 auto it = device->sensorsByAbsCode.find(absCode);
805 if (it == device->sensorsByAbsCode.end()) {
806 return Errorf("Sensor map not found.");
807 }
808 const SensorInfo& info = it->second;
809 return std::make_pair(info.sensorType, info.sensorDataIndex);
810 }
811
Chris Yea52ade12020-08-27 16:49:20 -0700812 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800813 mExcludedDevices = devices;
814 }
815
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000816 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
817 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000819 const size_t filledSize = std::min(mEvents.size(), bufferSize);
820 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
821
822 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700823 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000824 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600828 auto it = mVideoFrames.find(deviceId);
829 if (it != mVideoFrames.end()) {
830 std::vector<TouchVideoFrame> frames = std::move(it->second);
831 mVideoFrames.erase(deviceId);
832 return frames;
833 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800834 return {};
835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 Device* device = getDevice(deviceId);
839 if (device) {
840 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
841 if (index >= 0) {
842 return device->scanCodeStates.valueAt(index);
843 }
844 }
845 return AKEY_STATE_UNKNOWN;
846 }
847
Chris Yea52ade12020-08-27 16:49:20 -0700848 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 Device* device = getDevice(deviceId);
850 if (device) {
851 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
852 if (index >= 0) {
853 return device->keyCodeStates.valueAt(index);
854 }
855 }
856 return AKEY_STATE_UNKNOWN;
857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860 Device* device = getDevice(deviceId);
861 if (device) {
862 ssize_t index = device->switchStates.indexOfKey(sw);
863 if (index >= 0) {
864 return device->switchStates.valueAt(index);
865 }
866 }
867 return AKEY_STATE_UNKNOWN;
868 }
869
Chris Yea52ade12020-08-27 16:49:20 -0700870 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
871 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 Device* device = getDevice(deviceId);
873 if (device) {
874 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
875 if (index >= 0) {
876 *outValue = device->absoluteAxisValue.valueAt(index);
877 return OK;
878 }
879 }
880 *outValue = 0;
881 return -1;
882 }
883
Philip Junker4af3b3d2021-12-14 10:36:55 +0100884 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
885 Device* device = getDevice(deviceId);
886 if (!device) {
887 return AKEYCODE_UNKNOWN;
888 }
889 auto it = device->keyCodeMapping.find(locationKeyCode);
890 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
891 }
892
Chris Yea52ade12020-08-27 16:49:20 -0700893 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700894 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700895 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800896 bool result = false;
897 Device* device = getDevice(deviceId);
898 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700899 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700900 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
902 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
903 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 }
905 }
906 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
907 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
908 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 }
910 }
911 }
912 }
913 return result;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 if (device) {
919 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
920 return index >= 0;
921 }
922 return false;
923 }
924
Arthur Hungcb40a002021-08-03 14:31:01 +0000925 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
926 Device* device = getDevice(deviceId);
927 if (!device) {
928 return false;
929 }
930 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
931 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
932 return true;
933 }
934 }
935 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
936 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
937 return true;
938 }
939 }
940 return false;
941 }
942
Chris Yea52ade12020-08-27 16:49:20 -0700943 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 Device* device = getDevice(deviceId);
945 return device && device->leds.indexOfKey(led) >= 0;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 Device* device = getDevice(deviceId);
950 if (device) {
951 ssize_t index = device->leds.indexOfKey(led);
952 if (index >= 0) {
953 device->leds.replaceValueAt(led, on);
954 } else {
955 ADD_FAILURE()
956 << "Attempted to set the state of an LED that the EventHub declared "
957 "was not present. led=" << led;
958 }
959 }
960 }
961
Chris Yea52ade12020-08-27 16:49:20 -0700962 void getVirtualKeyDefinitions(
963 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 outVirtualKeys.clear();
965
966 Device* device = getDevice(deviceId);
967 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800968 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800969 }
970 }
971
Chris Yea52ade12020-08-27 16:49:20 -0700972 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700973 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800974 }
975
Chris Yea52ade12020-08-27 16:49:20 -0700976 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800977 return false;
978 }
979
Chris Yea52ade12020-08-27 16:49:20 -0700980 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Chris Yea52ade12020-08-27 16:49:20 -0700982 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Chris Ye87143712020-11-10 05:05:58 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
985
Chris Yee2b1e5c2021-03-10 22:45:12 -0800986 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
987 return BATTERY_CAPACITY;
988 }
Kim Low03ea0352020-11-06 12:45:07 -0800989
Chris Yee2b1e5c2021-03-10 22:45:12 -0800990 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
991 return BATTERY_STATUS;
992 }
993
994 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
995
996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
997 return std::nullopt;
998 }
Kim Low03ea0352020-11-06 12:45:07 -0800999
Chris Ye3fdbfef2021-01-06 18:45:18 -08001000 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
1001 std::vector<int32_t> ids;
1002 for (const auto& [rawId, info] : mRawLightInfos) {
1003 ids.push_back(rawId);
1004 }
1005 return ids;
1006 }
1007
1008 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
1009 auto it = mRawLightInfos.find(lightId);
1010 if (it == mRawLightInfos.end()) {
1011 return std::nullopt;
1012 }
1013 return it->second;
1014 }
1015
1016 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1017 mLightBrightness.emplace(lightId, brightness);
1018 }
1019
1020 void setLightIntensities(int32_t deviceId, int32_t lightId,
1021 std::unordered_map<LightColor, int32_t> intensities) override {
1022 mLightIntensities.emplace(lightId, intensities);
1023 };
1024
1025 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1026 auto lightIt = mLightBrightness.find(lightId);
1027 if (lightIt == mLightBrightness.end()) {
1028 return std::nullopt;
1029 }
1030 return lightIt->second;
1031 }
1032
1033 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1034 int32_t deviceId, int32_t lightId) override {
1035 auto lightIt = mLightIntensities.find(lightId);
1036 if (lightIt == mLightIntensities.end()) {
1037 return std::nullopt;
1038 }
1039 return lightIt->second;
1040 };
1041
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001042 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043 return false;
1044 }
1045
Chris Yea52ade12020-08-27 16:49:20 -07001046 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047
Chris Yea52ade12020-08-27 16:49:20 -07001048 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001049
Chris Yea52ade12020-08-27 16:49:20 -07001050 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001051
Chris Yea52ade12020-08-27 16:49:20 -07001052 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001053};
1054
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055// --- FakeInputMapper ---
1056
1057class FakeInputMapper : public InputMapper {
1058 uint32_t mSources;
1059 int32_t mKeyboardType;
1060 int32_t mMetaState;
1061 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1062 KeyedVector<int32_t, int32_t> mScanCodeStates;
1063 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001064 // fake mapping which would normally come from keyCharacterMap
1065 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001066 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001067
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::mutex mLock;
1069 std::condition_variable mStateChangedCondition;
1070 bool mConfigureWasCalled GUARDED_BY(mLock);
1071 bool mResetWasCalled GUARDED_BY(mLock);
1072 bool mProcessWasCalled GUARDED_BY(mLock);
1073 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Arthur Hungc23540e2018-11-29 20:42:11 +08001075 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001077 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1078 : InputMapper(deviceContext),
1079 mSources(sources),
1080 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001081 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001082 mConfigureWasCalled(false),
1083 mResetWasCalled(false),
1084 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001085
Chris Yea52ade12020-08-27 16:49:20 -07001086 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001087
1088 void setKeyboardType(int32_t keyboardType) {
1089 mKeyboardType = keyboardType;
1090 }
1091
1092 void setMetaState(int32_t metaState) {
1093 mMetaState = metaState;
1094 }
1095
1096 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001097 std::unique_lock<std::mutex> lock(mLock);
1098 base::ScopedLockAssertion assumeLocked(mLock);
1099 const bool configureCalled =
1100 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1101 return mConfigureWasCalled;
1102 });
1103 if (!configureCalled) {
1104 FAIL() << "Expected configure() to have been called.";
1105 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106 mConfigureWasCalled = false;
1107 }
1108
1109 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001110 std::unique_lock<std::mutex> lock(mLock);
1111 base::ScopedLockAssertion assumeLocked(mLock);
1112 const bool resetCalled =
1113 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1114 return mResetWasCalled;
1115 });
1116 if (!resetCalled) {
1117 FAIL() << "Expected reset() to have been called.";
1118 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119 mResetWasCalled = false;
1120 }
1121
Yi Kong9b14ac62018-07-17 13:48:38 -07001122 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001123 std::unique_lock<std::mutex> lock(mLock);
1124 base::ScopedLockAssertion assumeLocked(mLock);
1125 const bool processCalled =
1126 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1127 return mProcessWasCalled;
1128 });
1129 if (!processCalled) {
1130 FAIL() << "Expected process() to have been called.";
1131 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001132 if (outLastEvent) {
1133 *outLastEvent = mLastEvent;
1134 }
1135 mProcessWasCalled = false;
1136 }
1137
1138 void setKeyCodeState(int32_t keyCode, int32_t state) {
1139 mKeyCodeStates.replaceValueFor(keyCode, state);
1140 }
1141
1142 void setScanCodeState(int32_t scanCode, int32_t state) {
1143 mScanCodeStates.replaceValueFor(scanCode, state);
1144 }
1145
1146 void setSwitchState(int32_t switchCode, int32_t state) {
1147 mSwitchStates.replaceValueFor(switchCode, state);
1148 }
1149
1150 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001151 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 }
1153
Philip Junker4af3b3d2021-12-14 10:36:55 +01001154 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1155 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1156 }
1157
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001159 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160
Chris Yea52ade12020-08-27 16:49:20 -07001161 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001162 InputMapper::populateDeviceInfo(deviceInfo);
1163
1164 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1165 deviceInfo->setKeyboardType(mKeyboardType);
1166 }
1167 }
1168
Chris Yea52ade12020-08-27 16:49:20 -07001169 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001170 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001171 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001172
1173 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001174 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001175 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1176 mViewport = config->getDisplayViewportByPort(*displayPort);
1177 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001178
1179 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 }
1181
Chris Yea52ade12020-08-27 16:49:20 -07001182 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001183 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001185 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 }
1187
Chris Yea52ade12020-08-27 16:49:20 -07001188 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 mLastEvent = *rawEvent;
1191 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001192 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 }
1194
Chris Yea52ade12020-08-27 16:49:20 -07001195 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1197 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1198 }
1199
Philip Junker4af3b3d2021-12-14 10:36:55 +01001200 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1201 auto it = mKeyCodeMapping.find(locationKeyCode);
1202 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001206 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1207 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1208 }
1209
Chris Yea52ade12020-08-27 16:49:20 -07001210 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1212 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1213 }
1214
Chris Yea52ade12020-08-27 16:49:20 -07001215 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001216 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001217 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001218 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1220 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1221 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001222 }
1223 }
1224 }
Chris Yea52ade12020-08-27 16:49:20 -07001225 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001226 return result;
1227 }
1228
1229 virtual int32_t getMetaState() {
1230 return mMetaState;
1231 }
1232
1233 virtual void fadePointer() {
1234 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001235
1236 virtual std::optional<int32_t> getAssociatedDisplay() {
1237 if (mViewport) {
1238 return std::make_optional(mViewport->displayId);
1239 }
1240 return std::nullopt;
1241 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242};
1243
1244
1245// --- InstrumentedInputReader ---
1246
1247class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001248 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
1250public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001251 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1252 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001253 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001254 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001256 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001258 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001260 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001261 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 InputDeviceIdentifier identifier;
1263 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001264 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001266 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001267 }
1268
Prabir Pradhan28efc192019-11-05 01:10:04 +00001269 // Make the protected loopOnce method accessible to tests.
1270 using InputReader::loopOnce;
1271
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001273 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1274 const InputDeviceIdentifier& identifier)
1275 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001276 if (!mNextDevices.empty()) {
1277 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1278 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 return device;
1280 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001281 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282 }
1283
arthurhungdcef2dc2020-08-11 14:47:50 +08001284 // --- FakeInputReaderContext ---
1285 class FakeInputReaderContext : public ContextImpl {
1286 int32_t mGlobalMetaState;
1287 bool mUpdateGlobalMetaStateWasCalled;
1288 int32_t mGeneration;
1289
1290 public:
1291 FakeInputReaderContext(InputReader* reader)
1292 : ContextImpl(reader),
1293 mGlobalMetaState(0),
1294 mUpdateGlobalMetaStateWasCalled(false),
1295 mGeneration(1) {}
1296
1297 virtual ~FakeInputReaderContext() {}
1298
1299 void assertUpdateGlobalMetaStateWasCalled() {
1300 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1301 << "Expected updateGlobalMetaState() to have been called.";
1302 mUpdateGlobalMetaStateWasCalled = false;
1303 }
1304
1305 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1306
1307 uint32_t getGeneration() { return mGeneration; }
1308
1309 void updateGlobalMetaState() override {
1310 mUpdateGlobalMetaStateWasCalled = true;
1311 ContextImpl::updateGlobalMetaState();
1312 }
1313
1314 int32_t getGlobalMetaState() override {
1315 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1316 }
1317
1318 int32_t bumpGeneration() override {
1319 mGeneration = ContextImpl::bumpGeneration();
1320 return mGeneration;
1321 }
1322 } mFakeContext;
1323
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001325
1326public:
1327 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328};
1329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330// --- InputReaderPolicyTest ---
1331class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001332protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001333 sp<FakeInputReaderPolicy> mFakePolicy;
1334
Chris Yea52ade12020-08-27 16:49:20 -07001335 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1336 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337};
1338
1339/**
1340 * Check that empty set of viewports is an acceptable configuration.
1341 * Also try to get internal viewport two different ways - by type and by uniqueId.
1342 *
1343 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1344 * Such configuration is not currently allowed.
1345 */
1346TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001347 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348
1349 // We didn't add any viewports yet, so there shouldn't be any.
1350 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352 ASSERT_FALSE(internalViewport);
1353
1354 // Add an internal viewport, then clear it
1355 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001356 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001357 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358
1359 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001365 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001366 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001367 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001368
1369 mFakePolicy->clearViewports();
1370 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001371 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001373 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001374 ASSERT_FALSE(internalViewport);
1375}
1376
1377TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1378 const std::string internalUniqueId = "local:0";
1379 const std::string externalUniqueId = "local:1";
1380 const std::string virtualUniqueId1 = "virtual:2";
1381 const std::string virtualUniqueId2 = "virtual:3";
1382 constexpr int32_t virtualDisplayId1 = 2;
1383 constexpr int32_t virtualDisplayId2 = 3;
1384
1385 // Add an internal viewport
1386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001387 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1388 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 // Add an external viewport
1390 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001391 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1392 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393 // Add an virtual viewport
1394 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1396 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397 // Add another virtual viewport
1398 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001399 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1400 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401
1402 // Check matching by type for internal
1403 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001404 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 ASSERT_TRUE(internalViewport);
1406 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1407
1408 // Check matching by type for external
1409 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001410 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_TRUE(externalViewport);
1412 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1413
1414 // Check matching by uniqueId for virtual viewport #1
1415 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001416 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001418 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1420 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1421
1422 // Check matching by uniqueId for virtual viewport #2
1423 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001424 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001426 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1428 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1429}
1430
1431
1432/**
1433 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1434 * that lookup works by checking display id.
1435 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1436 */
1437TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1438 const std::string uniqueId1 = "uniqueId1";
1439 const std::string uniqueId2 = "uniqueId2";
1440 constexpr int32_t displayId1 = 2;
1441 constexpr int32_t displayId2 = 3;
1442
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1444 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445 for (const ViewportType& type : types) {
1446 mFakePolicy->clearViewports();
1447 // Add a viewport
1448 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001449 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1450 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451 // Add another viewport
1452 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001453 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1454 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455
1456 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001457 std::optional<DisplayViewport> viewport1 =
1458 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001459 ASSERT_TRUE(viewport1);
1460 ASSERT_EQ(displayId1, viewport1->displayId);
1461 ASSERT_EQ(type, viewport1->type);
1462
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 std::optional<DisplayViewport> viewport2 =
1464 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_TRUE(viewport2);
1466 ASSERT_EQ(displayId2, viewport2->displayId);
1467 ASSERT_EQ(type, viewport2->type);
1468
1469 // When there are multiple viewports of the same kind, and uniqueId is not specified
1470 // in the call to getDisplayViewport, then that situation is not supported.
1471 // The viewports can be stored in any order, so we cannot rely on the order, since that
1472 // is just implementation detail.
1473 // However, we can check that it still returns *a* viewport, we just cannot assert
1474 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001475 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 ASSERT_TRUE(someViewport);
1477 }
1478}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001480/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001481 * When we have multiple internal displays make sure we always return the default display when
1482 * querying by type.
1483 */
1484TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1485 const std::string uniqueId1 = "uniqueId1";
1486 const std::string uniqueId2 = "uniqueId2";
1487 constexpr int32_t nonDefaultDisplayId = 2;
1488 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1489 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1490
1491 // Add the default display first and ensure it gets returned.
1492 mFakePolicy->clearViewports();
1493 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001497 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001498 ViewportType::INTERNAL);
1499
1500 std::optional<DisplayViewport> viewport =
1501 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1502 ASSERT_TRUE(viewport);
1503 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1504 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1505
1506 // Add the default display second to make sure order doesn't matter.
1507 mFakePolicy->clearViewports();
1508 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001513 ViewportType::INTERNAL);
1514
1515 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1516 ASSERT_TRUE(viewport);
1517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1519}
1520
1521/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001522 * Check getDisplayViewportByPort
1523 */
1524TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526 const std::string uniqueId1 = "uniqueId1";
1527 const std::string uniqueId2 = "uniqueId2";
1528 constexpr int32_t displayId1 = 1;
1529 constexpr int32_t displayId2 = 2;
1530 const uint8_t hdmi1 = 0;
1531 const uint8_t hdmi2 = 1;
1532 const uint8_t hdmi3 = 2;
1533
1534 mFakePolicy->clearViewports();
1535 // Add a viewport that's associated with some display port that's not of interest.
1536 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1538 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 // Add another viewport, connected to HDMI1 port
1540 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001541 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1542 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001543
1544 // Check that correct display viewport was returned by comparing the display ports.
1545 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1546 ASSERT_TRUE(hdmi1Viewport);
1547 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1548 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1549
1550 // Check that we can still get the same viewport using the uniqueId
1551 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1552 ASSERT_TRUE(hdmi1Viewport);
1553 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1554 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1555 ASSERT_EQ(type, hdmi1Viewport->type);
1556
1557 // Check that we cannot find a port with "HDMI2", because we never added one
1558 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1559 ASSERT_FALSE(hdmi2Viewport);
1560}
1561
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562// --- InputReaderTest ---
1563
1564class InputReaderTest : public testing::Test {
1565protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001566 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001568 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001569 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001570
Chris Yea52ade12020-08-27 16:49:20 -07001571 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001572 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575
Prabir Pradhan28efc192019-11-05 01:10:04 +00001576 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001577 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578 }
1579
Chris Yea52ade12020-08-27 16:49:20 -07001580 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001581 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001582 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001583 }
1584
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001585 void addDevice(int32_t eventHubId, const std::string& name,
1586 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588
1589 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001590 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001591 }
1592 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001593 mReader->loopOnce();
1594 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001595 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1596 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001597 }
1598
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001599 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001600 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 }
1603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001604 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001605 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001606 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001607 }
1608
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001609 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001610 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001611 ftl::Flags<InputDeviceClass> classes,
1612 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001613 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1615 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001616 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001617 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 return mapper;
1619 }
1620};
1621
Chris Ye98d3f532020-10-01 21:48:59 -07001622TEST_F(InputReaderTest, PolicyGetInputDevices) {
1623 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001624 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001625 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626
1627 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001628 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001630 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001631 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001634 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635}
1636
Chris Yee7310032020-09-22 15:36:28 -07001637TEST_F(InputReaderTest, GetMergedInputDevices) {
1638 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1639 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1640 // Add two subdevices to device
1641 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1642 // Must add at least one mapper or the device will be ignored!
1643 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1644 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1645
1646 // Push same device instance for next device to be added, so they'll have same identifier.
1647 mReader->pushNextDevice(device);
1648 mReader->pushNextDevice(device);
1649 ASSERT_NO_FATAL_FAILURE(
1650 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1651 ASSERT_NO_FATAL_FAILURE(
1652 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1653
1654 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001655 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001656}
1657
Chris Yee14523a2020-12-19 13:46:00 -08001658TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1659 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1660 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1661 // Add two subdevices to device
1662 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1663 // Must add at least one mapper or the device will be ignored!
1664 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1665 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1666
1667 // Push same device instance for next device to be added, so they'll have same identifier.
1668 mReader->pushNextDevice(device);
1669 mReader->pushNextDevice(device);
1670 // Sensor device is initially disabled
1671 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1672 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1673 nullptr));
1674 // Device is disabled because the only sub device is a sensor device and disabled initially.
1675 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1676 ASSERT_FALSE(device->isEnabled());
1677 ASSERT_NO_FATAL_FAILURE(
1678 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1679 // The merged device is enabled if any sub device is enabled
1680 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1681 ASSERT_TRUE(device->isEnabled());
1682}
1683
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001686 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 constexpr int32_t eventHubId = 1;
1688 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001690 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001691 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001692 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693
Yi Kong9b14ac62018-07-17 13:48:38 -07001694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695
1696 NotifyDeviceResetArgs resetArgs;
1697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001698 ASSERT_EQ(deviceId, resetArgs.deviceId);
1699
1700 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001702 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001703
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 ASSERT_EQ(deviceId, resetArgs.deviceId);
1706 ASSERT_EQ(device->isEnabled(), false);
1707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001708 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001709 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001712 ASSERT_EQ(device->isEnabled(), false);
1713
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001715 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001717 ASSERT_EQ(deviceId, resetArgs.deviceId);
1718 ASSERT_EQ(device->isEnabled(), true);
1719}
1720
Michael Wrightd02c5b62014-02-10 15:10:22 -08001721TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001723 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001725 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001727 AINPUT_SOURCE_KEYBOARD, nullptr);
1728 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001729
1730 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1731 AINPUT_SOURCE_ANY, AKEYCODE_A))
1732 << "Should return unknown when the device id is >= 0 but unknown.";
1733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001734 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1735 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown when the device id is valid but the sources are not "
1737 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001739 ASSERT_EQ(AKEY_STATE_DOWN,
1740 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1741 AKEYCODE_A))
1742 << "Should return value provided by mapper when device id is valid and the device "
1743 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001744
1745 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1746 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1747 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1748
1749 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1750 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1751 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1752}
1753
Philip Junker4af3b3d2021-12-14 10:36:55 +01001754TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1755 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1756 constexpr int32_t eventHubId = 1;
1757 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1758 InputDeviceClass::KEYBOARD,
1759 AINPUT_SOURCE_KEYBOARD, nullptr);
1760 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1761
1762 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1763 << "Should return unknown when the device with the specified id is not found.";
1764
1765 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1766 << "Should return correct mapping when device id is valid and mapping exists.";
1767
1768 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1769 << "Should return the location key code when device id is valid and there's no "
1770 "mapping.";
1771}
1772
1773TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1775 constexpr int32_t eventHubId = 1;
1776 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1777 InputDeviceClass::JOYSTICK,
1778 AINPUT_SOURCE_GAMEPAD, nullptr);
1779 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1780
1781 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1782 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1783}
1784
Michael Wrightd02c5b62014-02-10 15:10:22 -08001785TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001787 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001788 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001790 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001791 AINPUT_SOURCE_KEYBOARD, nullptr);
1792 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001793
1794 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1795 AINPUT_SOURCE_ANY, KEY_A))
1796 << "Should return unknown when the device id is >= 0 but unknown.";
1797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001798 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1799 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1800 << "Should return unknown when the device id is valid but the sources are not "
1801 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001803 ASSERT_EQ(AKEY_STATE_DOWN,
1804 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1805 KEY_A))
1806 << "Should return value provided by mapper when device id is valid and the device "
1807 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001808
1809 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1810 AINPUT_SOURCE_TRACKBALL, KEY_A))
1811 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1812
1813 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1814 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1815 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1816}
1817
1818TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001820 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001822 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001823 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001824 AINPUT_SOURCE_KEYBOARD, nullptr);
1825 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1828 AINPUT_SOURCE_ANY, SW_LID))
1829 << "Should return unknown when the device id is >= 0 but unknown.";
1830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001831 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1832 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1833 << "Should return unknown when the device id is valid but the sources are not "
1834 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001836 ASSERT_EQ(AKEY_STATE_DOWN,
1837 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1838 SW_LID))
1839 << "Should return value provided by mapper when device id is valid and the device "
1840 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841
1842 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1843 AINPUT_SOURCE_TRACKBALL, SW_LID))
1844 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1845
1846 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1847 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1848 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1849}
1850
1851TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001857 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001859 mapper.addSupportedKeyCode(AKEYCODE_A);
1860 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 uint8_t flags[4] = { 0, 0, 0, 1 };
1864
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001865 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001866 << "Should return false when device id is >= 0 but unknown.";
1867 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1868
1869 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001870 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001871 << "Should return false when device id is valid but the sources are not supported by "
1872 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1874
1875 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001876 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001877 keyCodes, flags))
1878 << "Should return value provided by mapper when device id is valid and the device "
1879 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1881
1882 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001883 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1884 << "Should return false when the device id is < 0 but the sources are not supported by "
1885 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001886 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1887
1888 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001889 ASSERT_TRUE(
1890 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1891 << "Should return value provided by mapper when device id is < 0 and one of the "
1892 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001893 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1894}
1895
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001896TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001897 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001898 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001899
1900 NotifyConfigurationChangedArgs args;
1901
1902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1903 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1904}
1905
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001906TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001909 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001911 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001916 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001917 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1919
1920 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001921 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001922 ASSERT_EQ(when, event.when);
1923 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001925 ASSERT_EQ(EV_KEY, event.type);
1926 ASSERT_EQ(KEY_A, event.code);
1927 ASSERT_EQ(1, event.value);
1928}
1929
Garfield Tan1c7bc862020-01-28 13:24:04 -08001930TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001931 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001932 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 constexpr int32_t eventHubId = 1;
1934 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001937 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001939
1940 NotifyDeviceResetArgs resetArgs;
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001942 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001944 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001947 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001948 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001950 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001951 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001953 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001954 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001961}
1962
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1964 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001965 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001966 constexpr int32_t eventHubId = 1;
1967 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1968 // Must add at least one mapper or the device will be ignored!
1969 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001970 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001971 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1972
1973 NotifyDeviceResetArgs resetArgs;
1974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1975 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1976}
1977
Arthur Hungc23540e2018-11-29 20:42:11 +08001978TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001981 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001982 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001983 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1984 FakeInputMapper& mapper =
1985 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001986 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001987
1988 const uint8_t hdmi1 = 1;
1989
1990 // Associated touch screen with second display.
1991 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1992
1993 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001994 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001999 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002000 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002001 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002002 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002003
2004 // Add the device, and make sure all of the callbacks are triggered.
2005 // The device is added after the input port associations are processed since
2006 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002007 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002010 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002011
Arthur Hung2c9a3342019-07-23 14:18:59 +08002012 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002013 ASSERT_EQ(deviceId, device->getId());
2014 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2015 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002016
2017 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002018 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002019 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002020 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002021}
2022
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2024 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002025 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002026 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2027 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2028 // Must add at least one mapper or the device will be ignored!
2029 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2030 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2031 mReader->pushNextDevice(device);
2032 mReader->pushNextDevice(device);
2033 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2034 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2035
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2037
2038 NotifyDeviceResetArgs resetArgs;
2039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2040 ASSERT_EQ(deviceId, resetArgs.deviceId);
2041 ASSERT_TRUE(device->isEnabled());
2042 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2043 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2044
2045 disableDevice(deviceId);
2046 mReader->loopOnce();
2047
2048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2049 ASSERT_EQ(deviceId, resetArgs.deviceId);
2050 ASSERT_FALSE(device->isEnabled());
2051 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2052 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2053
2054 enableDevice(deviceId);
2055 mReader->loopOnce();
2056
2057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2058 ASSERT_EQ(deviceId, resetArgs.deviceId);
2059 ASSERT_TRUE(device->isEnabled());
2060 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2061 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2062}
2063
2064TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2065 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002066 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002067 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2068 // Add two subdevices to device
2069 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2070 FakeInputMapper& mapperDevice1 =
2071 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2072 FakeInputMapper& mapperDevice2 =
2073 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2074 mReader->pushNextDevice(device);
2075 mReader->pushNextDevice(device);
2076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2077 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2078
2079 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2080 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2081
2082 ASSERT_EQ(AKEY_STATE_DOWN,
2083 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2084 ASSERT_EQ(AKEY_STATE_DOWN,
2085 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2086 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2087 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2088}
2089
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2091 NotifyPointerCaptureChangedArgs args;
2092
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002093 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
2096 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002097 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2098 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002099
2100 mFakePolicy->setPointerCapture(false);
2101 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2102 mReader->loopOnce();
2103 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002104 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002105
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002106 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002107 // does not change.
2108 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2109 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002110 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002111}
2112
Chris Ye87143712020-11-10 05:05:58 +00002113class FakeVibratorInputMapper : public FakeInputMapper {
2114public:
2115 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2116 : FakeInputMapper(deviceContext, sources) {}
2117
2118 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2119};
2120
2121TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2122 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002123 ftl::Flags<InputDeviceClass> deviceClass =
2124 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002125 constexpr int32_t eventHubId = 1;
2126 const char* DEVICE_LOCATION = "BLUETOOTH";
2127 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2128 FakeVibratorInputMapper& mapper =
2129 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2130 mReader->pushNextDevice(device);
2131
2132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2133 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2134
2135 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2136 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2137}
2138
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002139// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002140
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002141class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002142public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002143 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002144
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002145 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002146
2147 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2148
2149 void dump(std::string& dump) override {}
2150
2151 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2152 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002153 }
2154
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2156 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002157 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
2159 bool setLightColor(int32_t lightId, int32_t color) override {
2160 getDeviceContext().setLightBrightness(lightId, color >> 24);
2161 return true;
2162 }
2163
2164 std::optional<int32_t> getLightColor(int32_t lightId) override {
2165 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2166 if (!result.has_value()) {
2167 return std::nullopt;
2168 }
2169 return result.value() << 24;
2170 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002171
2172 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2173
2174 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2175
2176private:
2177 InputDeviceContext& mDeviceContext;
2178 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2179 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002180};
2181
Chris Yee2b1e5c2021-03-10 22:45:12 -08002182TEST_F(InputReaderTest, BatteryGetCapacity) {
2183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002184 ftl::Flags<InputDeviceClass> deviceClass =
2185 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002186 constexpr int32_t eventHubId = 1;
2187 const char* DEVICE_LOCATION = "BLUETOOTH";
2188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController& controller =
2190 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 mReader->pushNextDevice(device);
2192
2193 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2194
2195 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2196 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2197}
2198
2199TEST_F(InputReaderTest, BatteryGetStatus) {
2200 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002201 ftl::Flags<InputDeviceClass> deviceClass =
2202 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203 constexpr int32_t eventHubId = 1;
2204 const char* DEVICE_LOCATION = "BLUETOOTH";
2205 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002206 FakePeripheralController& controller =
2207 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002208 mReader->pushNextDevice(device);
2209
2210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2211
2212 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2213 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2214}
2215
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216TEST_F(InputReaderTest, LightGetColor) {
2217 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002218 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002219 constexpr int32_t eventHubId = 1;
2220 const char* DEVICE_LOCATION = "BLUETOOTH";
2221 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002222 FakePeripheralController& controller =
2223 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002224 mReader->pushNextDevice(device);
2225 RawLightInfo info = {.id = 1,
2226 .name = "Mono",
2227 .maxBrightness = 255,
2228 .flags = InputLightClass::BRIGHTNESS,
2229 .path = ""};
2230 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2231 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2232
2233 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234
Chris Yee2b1e5c2021-03-10 22:45:12 -08002235 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2236 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002237 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2238 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2239}
2240
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002241// --- InputReaderIntegrationTest ---
2242
2243// These tests create and interact with the InputReader only through its interface.
2244// The InputReader is started during SetUp(), which starts its processing in its own
2245// thread. The tests use linux uinput to emulate input devices.
2246// NOTE: Interacting with the physical device while these tests are running may cause
2247// the tests to fail.
2248class InputReaderIntegrationTest : public testing::Test {
2249protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002250 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002252 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002253
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002254 std::shared_ptr<FakePointerController> mFakePointerController;
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002258 mFakePointerController = std::make_shared<FakePointerController>();
2259 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2261 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002263 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2264 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002265 ASSERT_EQ(mReader->start(), OK);
2266
2267 // Since this test is run on a real device, all the input devices connected
2268 // to the test device will show up in mReader. We wait for those input devices to
2269 // show up before beginning the tests.
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2272 }
2273
Chris Yea52ade12020-08-27 16:49:20 -07002274 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002276 mReader.reset();
2277 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002278 mFakePolicy.clear();
2279 }
2280};
2281
2282TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2283 // An invalid input device that is only used for this test.
2284 class InvalidUinputDevice : public UinputDevice {
2285 public:
2286 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2287
2288 private:
2289 void configureDevice(int fd, uinput_user_dev* device) override {}
2290 };
2291
2292 const size_t numDevices = mFakePolicy->getInputDevices().size();
2293
2294 // UinputDevice does not set any event or key bits, so InputReader should not
2295 // consider it as a valid device.
2296 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2297 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2299 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2300
2301 invalidDevice.reset();
2302 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2304 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2305}
2306
2307TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2308 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2309
2310 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2314
2315 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002317 const auto& it =
2318 std::find_if(inputDevices.begin(), inputDevices.end(),
2319 [&keyboard](const InputDeviceInfo& info) {
2320 return info.getIdentifier().name == keyboard->getName();
2321 });
2322
2323 ASSERT_NE(it, inputDevices.end());
2324 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2325 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2326 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002327
2328 keyboard.reset();
2329 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2330 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2331 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2332}
2333
2334TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2335 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2336 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2337
2338 NotifyConfigurationChangedArgs configChangedArgs;
2339 ASSERT_NO_FATAL_FAILURE(
2340 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002341 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002342 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2343
2344 NotifyKeyArgs keyArgs;
2345 keyboard->pressAndReleaseHomeKey();
2346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2347 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002348 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002349 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002350 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002351 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002352 prevTimestamp = keyArgs.eventTime;
2353
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2355 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002356 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002358 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002361/**
2362 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2363 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2364 * are passed to the listener.
2365 */
2366static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2367TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2368 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2369 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2370 NotifyKeyArgs keyArgs;
2371
2372 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2375 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2376
2377 controller->pressAndReleaseKey(BTN_GEAR_UP);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2380 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2381}
2382
Arthur Hungaab25622020-01-16 11:22:11 +08002383// --- TouchProcessTest ---
2384class TouchIntegrationTest : public InputReaderIntegrationTest {
2385protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002386 const std::string UNIQUE_ID = "local:0";
2387
Chris Yea52ade12020-08-27 16:49:20 -07002388 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002389 InputReaderIntegrationTest::SetUp();
2390 // At least add an internal display.
2391 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2392 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002393 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002394
2395 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2396 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2398 }
2399
2400 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2401 int32_t orientation, const std::string& uniqueId,
2402 std::optional<uint8_t> physicalPort,
2403 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002404 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2405 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002406 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2407 }
2408
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002409 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2410 NotifyMotionArgs args;
2411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2412 EXPECT_EQ(action, args.action);
2413 ASSERT_EQ(points.size(), args.pointerCount);
2414 for (size_t i = 0; i < args.pointerCount; i++) {
2415 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2416 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2417 }
2418 }
2419
Arthur Hungaab25622020-01-16 11:22:11 +08002420 std::unique_ptr<UinputTouchScreen> mDevice;
2421};
2422
2423TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2424 NotifyMotionArgs args;
2425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002428 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002429 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002430 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2433
2434 // ACTION_MOVE
2435 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002436 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2439
2440 // ACTION_UP
2441 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002442 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2445}
2446
2447TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2448 NotifyMotionArgs args;
2449 const Point centerPoint = mDevice->getCenterPoint();
2450
2451 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002452 mDevice->sendSlot(FIRST_SLOT);
2453 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002454 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002455 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002456 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2458
2459 // ACTION_POINTER_DOWN (Second slot)
2460 const Point secondPoint = centerPoint + Point(100, 100);
2461 mDevice->sendSlot(SECOND_SLOT);
2462 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002463 mDevice->sendDown(secondPoint);
2464 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002466 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002467
2468 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002469 mDevice->sendMove(secondPoint + Point(1, 1));
2470 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2473
2474 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002475 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002476 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002478 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002479
2480 // ACTION_UP
2481 mDevice->sendSlot(FIRST_SLOT);
2482 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002483 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2486}
2487
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002488/**
2489 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2490 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2491 * data?
2492 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2493 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2494 * for Pointer 0 only is generated after.
2495 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2496 * events, we will not miss any information.
2497 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2498 * event generated afterwards that contains the newest movement of pointer 0.
2499 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2500 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2501 * losing information about non-palm pointers.
2502 */
2503TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2504 NotifyMotionArgs args;
2505 const Point centerPoint = mDevice->getCenterPoint();
2506
2507 // ACTION_DOWN
2508 mDevice->sendSlot(FIRST_SLOT);
2509 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2510 mDevice->sendDown(centerPoint);
2511 mDevice->sendSync();
2512 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2513
2514 // ACTION_POINTER_DOWN (Second slot)
2515 const Point secondPoint = centerPoint + Point(100, 100);
2516 mDevice->sendSlot(SECOND_SLOT);
2517 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2518 mDevice->sendDown(secondPoint);
2519 mDevice->sendSync();
2520 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2521
2522 // ACTION_MOVE (First slot)
2523 mDevice->sendSlot(FIRST_SLOT);
2524 mDevice->sendMove(centerPoint + Point(5, 5));
2525 // ACTION_POINTER_UP (Second slot)
2526 mDevice->sendSlot(SECOND_SLOT);
2527 mDevice->sendPointerUp();
2528 // Send a single sync for the above 2 pointer updates
2529 mDevice->sendSync();
2530
2531 // First, we should get POINTER_UP for the second pointer
2532 assertReceivedMotion(ACTION_POINTER_1_UP,
2533 {/*first pointer */ centerPoint + Point(5, 5),
2534 /*second pointer*/ secondPoint});
2535
2536 // Next, the MOVE event for the first pointer
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2538}
2539
2540/**
2541 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2542 * move, and then it will go up, all in the same frame.
2543 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2544 * gets sent to the listener.
2545 */
2546TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2547 NotifyMotionArgs args;
2548 const Point centerPoint = mDevice->getCenterPoint();
2549
2550 // ACTION_DOWN
2551 mDevice->sendSlot(FIRST_SLOT);
2552 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2553 mDevice->sendDown(centerPoint);
2554 mDevice->sendSync();
2555 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2556
2557 // ACTION_POINTER_DOWN (Second slot)
2558 const Point secondPoint = centerPoint + Point(100, 100);
2559 mDevice->sendSlot(SECOND_SLOT);
2560 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2561 mDevice->sendDown(secondPoint);
2562 mDevice->sendSync();
2563 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2564
2565 // ACTION_MOVE (First slot)
2566 mDevice->sendSlot(FIRST_SLOT);
2567 mDevice->sendMove(centerPoint + Point(5, 5));
2568 // ACTION_POINTER_UP (Second slot)
2569 mDevice->sendSlot(SECOND_SLOT);
2570 mDevice->sendMove(secondPoint + Point(6, 6));
2571 mDevice->sendPointerUp();
2572 // Send a single sync for the above 2 pointer updates
2573 mDevice->sendSync();
2574
2575 // First, we should get POINTER_UP for the second pointer
2576 // The movement of the second pointer during the liftoff frame is ignored.
2577 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2578 assertReceivedMotion(ACTION_POINTER_1_UP,
2579 {/*first pointer */ centerPoint + Point(5, 5),
2580 /*second pointer*/ secondPoint});
2581
2582 // Next, the MOVE event for the first pointer
2583 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2584}
2585
Arthur Hungaab25622020-01-16 11:22:11 +08002586TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2587 NotifyMotionArgs args;
2588 const Point centerPoint = mDevice->getCenterPoint();
2589
2590 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002591 mDevice->sendSlot(FIRST_SLOT);
2592 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002593 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2597
arthurhungcc7f9802020-04-30 17:55:40 +08002598 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002599 const Point secondPoint = centerPoint + Point(100, 100);
2600 mDevice->sendSlot(SECOND_SLOT);
2601 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2602 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002603 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002604 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002605 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002606
arthurhungcc7f9802020-04-30 17:55:40 +08002607 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002608 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002609 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002610 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2612
arthurhungcc7f9802020-04-30 17:55:40 +08002613 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2614 // a palm event.
2615 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002616 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002617 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002618 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002619 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002620 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002621
arthurhungcc7f9802020-04-30 17:55:40 +08002622 // Send up to second slot, expect first slot send moving.
2623 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002624 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002625 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002627
arthurhungcc7f9802020-04-30 17:55:40 +08002628 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002629 mDevice->sendSlot(FIRST_SLOT);
2630 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002631 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002632
arthurhungcc7f9802020-04-30 17:55:40 +08002633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2634 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002635}
2636
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638class InputDeviceTest : public testing::Test {
2639protected:
2640 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002641 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 static const int32_t DEVICE_ID;
2643 static const int32_t DEVICE_GENERATION;
2644 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002645 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002646 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002648 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002650 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002651 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002652 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Chris Yea52ade12020-08-27 16:49:20 -07002654 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002655 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002657 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002660 InputDeviceIdentifier identifier;
2661 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002662 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002663 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002665 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002666 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002667 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002668 }
2669
Chris Yea52ade12020-08-27 16:49:20 -07002670 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002671 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673 }
2674};
2675
2676const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002677const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2680const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002681const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002682 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002683const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684
2685TEST_F(InputDeviceTest, ImmutableProperties) {
2686 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002687 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002688 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689}
2690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002691TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2692 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002693}
2694
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2696 // Configuration.
2697 InputReaderConfiguration config;
2698 mDevice->configure(ARBITRARY_TIME, &config, 0);
2699
2700 // Reset.
2701 mDevice->reset(ARBITRARY_TIME);
2702
2703 NotifyDeviceResetArgs resetArgs;
2704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2705 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2706 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2707
2708 // Metadata.
2709 ASSERT_TRUE(mDevice->isIgnored());
2710 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2711
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002712 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002714 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2716 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2717
2718 // State queries.
2719 ASSERT_EQ(0, mDevice->getMetaState());
2720
2721 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2722 << "Ignored device should return unknown key code state.";
2723 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2724 << "Ignored device should return unknown scan code state.";
2725 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2726 << "Ignored device should return unknown switch state.";
2727
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002728 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002730 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002731 << "Ignored device should never mark any key codes.";
2732 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2733 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2734}
2735
2736TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2737 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002738 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002740 FakeInputMapper& mapper1 =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002742 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2743 mapper1.setMetaState(AMETA_ALT_ON);
2744 mapper1.addSupportedKeyCode(AKEYCODE_A);
2745 mapper1.addSupportedKeyCode(AKEYCODE_B);
2746 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2747 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2748 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2749 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2750 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002751
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002752 FakeInputMapper& mapper2 =
2753 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002754 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755
2756 InputReaderConfiguration config;
2757 mDevice->configure(ARBITRARY_TIME, &config, 0);
2758
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 std::string propertyValue;
2760 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002762 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002764 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2765 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766
2767 // Reset
2768 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2770 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002771
2772 NotifyDeviceResetArgs resetArgs;
2773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2774 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2775 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2776
2777 // Metadata.
2778 ASSERT_FALSE(mDevice->isIgnored());
2779 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2780
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002781 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002783 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2785 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2786
2787 // State queries.
2788 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2789 << "Should query mappers and combine meta states.";
2790
2791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2792 << "Should return unknown key code state when source not supported.";
2793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2794 << "Should return unknown scan code state when source not supported.";
2795 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2796 << "Should return unknown switch state when source not supported.";
2797
2798 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2799 << "Should query mapper when source is supported.";
2800 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2801 << "Should query mapper when source is supported.";
2802 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2803 << "Should query mapper when source is supported.";
2804
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002805 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 << "Should do nothing when source is unsupported.";
2809 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2810 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2811 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2812 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2813
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002814 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002815 << "Should query mapper when source is supported.";
2816 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2817 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2818 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2819 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2820
2821 // Event handling.
2822 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002823 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 mDevice->process(&event, 1);
2825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2827 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828}
2829
Arthur Hung2c9a3342019-07-23 14:18:59 +08002830// A single input device is associated with a specific display. Check that:
2831// 1. Device is disabled if the viewport corresponding to the associated display is not found
2832// 2. Device is disabled when setEnabled API is called
2833TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002834 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835
2836 // First Configuration.
2837 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2838
2839 // Device should be enabled by default.
2840 ASSERT_TRUE(mDevice->isEnabled());
2841
2842 // Prepare associated info.
2843 constexpr uint8_t hdmi = 1;
2844 const std::string UNIQUE_ID = "local:1";
2845
2846 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2847 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2848 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2849 // Device should be disabled because it is associated with a specific display via
2850 // input port <-> display port association, but the corresponding display is not found
2851 ASSERT_FALSE(mDevice->isEnabled());
2852
2853 // Prepare displays.
2854 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002855 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2856 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled after set disable.
2862 mFakePolicy->addDisabledDevice(mDevice->getId());
2863 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2864 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2865 ASSERT_FALSE(mDevice->isEnabled());
2866
2867 // Device should still be disabled even found the associated display.
2868 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2869 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2870 ASSERT_FALSE(mDevice->isEnabled());
2871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2874 // Device should be enabled by default.
2875 mFakePolicy->clearViewports();
2876 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2878 ASSERT_TRUE(mDevice->isEnabled());
2879
2880 // Device should be disabled because it is associated with a specific display, but the
2881 // corresponding display is not found.
2882 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002883 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002884 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2885 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2886 ASSERT_FALSE(mDevice->isEnabled());
2887
2888 // Device should be enabled when a display is found.
2889 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2890 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2891 NO_PORT, ViewportType::INTERNAL);
2892 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2893 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Device should be disabled after set disable.
2897 mFakePolicy->addDisabledDevice(mDevice->getId());
2898 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2899 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2900 ASSERT_FALSE(mDevice->isEnabled());
2901
2902 // Device should still be disabled even found the associated display.
2903 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2904 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2905 ASSERT_FALSE(mDevice->isEnabled());
2906}
2907
Christine Franks2a2293c2022-01-18 11:51:16 -08002908TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2909 mFakePolicy->clearViewports();
2910 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2911 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2912
2913 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2915 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2916 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2917 NO_PORT, ViewportType::INTERNAL);
2918 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2919 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2920 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2921}
2922
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923// --- InputMapperTest ---
2924
2925class InputMapperTest : public testing::Test {
2926protected:
2927 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 static const int32_t DEVICE_ID;
2930 static const int32_t DEVICE_GENERATION;
2931 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002932 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002934
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002935 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002936 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 std::unique_ptr<InstrumentedInputReader> mReader;
2939 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002941 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002942 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002944 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002945 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002946 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 }
2949
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002950 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002951 SetUp(DEVICE_CLASSES);
2952 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953
Chris Yea52ade12020-08-27 16:49:20 -07002954 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002955 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 }
2958
2959 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002960 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 }
2962
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002963 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002964 if (!changes ||
2965 (changes &
2966 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2967 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002968 mReader->requestRefreshConfiguration(changes);
2969 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002970 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002971 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2972 }
2973
arthurhungdcef2dc2020-08-11 14:47:50 +08002974 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2975 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002976 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002977 InputDeviceIdentifier identifier;
2978 identifier.name = name;
2979 identifier.location = location;
2980 std::shared_ptr<InputDevice> device =
2981 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2982 identifier);
2983 mReader->pushNextDevice(device);
2984 mFakeEventHub->addDevice(eventHubId, name, classes);
2985 mReader->loopOnce();
2986 return device;
2987 }
2988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002989 template <class T, typename... Args>
2990 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002992 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002994 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002995 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 }
2997
2998 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002999 int32_t orientation, const std::string& uniqueId,
3000 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003001 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3002 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003003 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3004 }
3005
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003006 void clearViewports() {
3007 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 }
3009
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003010 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3011 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012 RawEvent event;
3013 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003014 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003015 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 event.type = type;
3017 event.code = code;
3018 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003019 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003020 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 }
3022
3023 static void assertMotionRange(const InputDeviceInfo& info,
3024 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3025 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003026 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3028 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3029 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3030 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3031 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3032 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3033 }
3034
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003035 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3036 float size, float touchMajor, float touchMinor, float toolMajor,
3037 float toolMinor, float orientation, float distance,
3038 float scaledAxisEpsilon = 1.f) {
3039 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3040 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3042 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003043 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3044 scaledAxisEpsilon);
3045 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3046 scaledAxisEpsilon);
3047 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3048 scaledAxisEpsilon);
3049 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3050 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3052 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3053 }
3054
Michael Wright17db18e2020-06-26 20:51:44 +01003055 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003057 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_NEAR(x, actualX, 1);
3059 ASSERT_NEAR(y, actualY, 1);
3060 }
3061};
3062
3063const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003064const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003065const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3067const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003068const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3069 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003070const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
3072// --- SwitchInputMapperTest ---
3073
3074class SwitchInputMapperTest : public InputMapperTest {
3075protected:
3076};
3077
3078TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003082}
3083
3084TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003087 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003088 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092}
3093
3094TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101
3102 NotifySwitchArgs args;
3103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3104 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003105 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3106 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 args.switchMask);
3108 ASSERT_EQ(uint32_t(0), args.policyFlags);
3109}
3110
Chris Ye87143712020-11-10 05:05:58 +00003111// --- VibratorInputMapperTest ---
3112class VibratorInputMapperTest : public InputMapperTest {
3113protected:
3114 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3115};
3116
3117TEST_F(VibratorInputMapperTest, GetSources) {
3118 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3119
3120 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3121}
3122
3123TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3124 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3125
3126 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3127}
3128
3129TEST_F(VibratorInputMapperTest, Vibrate) {
3130 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003131 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003132 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3133
3134 VibrationElement pattern(2);
3135 VibrationSequence sequence(2);
3136 pattern.duration = std::chrono::milliseconds(200);
3137 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3138 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3139 sequence.addElement(pattern);
3140 pattern.duration = std::chrono::milliseconds(500);
3141 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3142 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3143 sequence.addElement(pattern);
3144
3145 std::vector<int64_t> timings = {0, 1};
3146 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3147
3148 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003149 // Start vibrating
3150 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003151 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003152 // Verify vibrator state listener was notified.
3153 mReader->loopOnce();
3154 NotifyVibratorStateArgs args;
3155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3156 ASSERT_EQ(DEVICE_ID, args.deviceId);
3157 ASSERT_TRUE(args.isOn);
3158 // Stop vibrating
3159 mapper.cancelVibrate(VIBRATION_TOKEN);
3160 ASSERT_FALSE(mapper.isVibrating());
3161 // Verify vibrator state listener was notified.
3162 mReader->loopOnce();
3163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003166}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167
Chris Yef59a2f42020-10-16 12:55:26 -07003168// --- SensorInputMapperTest ---
3169
3170class SensorInputMapperTest : public InputMapperTest {
3171protected:
3172 static const int32_t ACCEL_RAW_MIN;
3173 static const int32_t ACCEL_RAW_MAX;
3174 static const int32_t ACCEL_RAW_FUZZ;
3175 static const int32_t ACCEL_RAW_FLAT;
3176 static const int32_t ACCEL_RAW_RESOLUTION;
3177
3178 static const int32_t GYRO_RAW_MIN;
3179 static const int32_t GYRO_RAW_MAX;
3180 static const int32_t GYRO_RAW_FUZZ;
3181 static const int32_t GYRO_RAW_FLAT;
3182 static const int32_t GYRO_RAW_RESOLUTION;
3183
3184 static const float GRAVITY_MS2_UNIT;
3185 static const float DEGREE_RADIAN_UNIT;
3186
3187 void prepareAccelAxes();
3188 void prepareGyroAxes();
3189 void setAccelProperties();
3190 void setGyroProperties();
3191 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3192};
3193
3194const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3195const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3196const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3197const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3198const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3199
3200const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3201const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3202const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3203const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3204const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3205
3206const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3207const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3208
3209void SensorInputMapperTest::prepareAccelAxes() {
3210 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3211 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3212 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3213 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3215 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3216}
3217
3218void SensorInputMapperTest::prepareGyroAxes() {
3219 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3220 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3222 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3224 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3225}
3226
3227void SensorInputMapperTest::setAccelProperties() {
3228 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3229 /* sensorDataIndex */ 0);
3230 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3231 /* sensorDataIndex */ 1);
3232 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3233 /* sensorDataIndex */ 2);
3234 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3235 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3236 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3237 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3238 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3239}
3240
3241void SensorInputMapperTest::setGyroProperties() {
3242 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3243 /* sensorDataIndex */ 0);
3244 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3245 /* sensorDataIndex */ 1);
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3247 /* sensorDataIndex */ 2);
3248 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3249 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3250 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3251 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3252 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3253}
3254
3255TEST_F(SensorInputMapperTest, GetSources) {
3256 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3257
3258 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3259}
3260
3261TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3262 setAccelProperties();
3263 prepareAccelAxes();
3264 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3265
3266 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3267 std::chrono::microseconds(10000),
3268 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003269 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003275
3276 NotifySensorArgs args;
3277 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3278 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3279 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3280
3281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3282 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3283 ASSERT_EQ(args.deviceId, DEVICE_ID);
3284 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3285 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3286 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3287 ASSERT_EQ(args.values, values);
3288 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3289}
3290
3291TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3292 setGyroProperties();
3293 prepareGyroAxes();
3294 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3295
3296 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3297 std::chrono::microseconds(10000),
3298 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003299 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003305
3306 NotifySensorArgs args;
3307 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3308 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3309 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3310
3311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3312 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3313 ASSERT_EQ(args.deviceId, DEVICE_ID);
3314 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3315 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3316 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3317 ASSERT_EQ(args.values, values);
3318 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3319}
3320
Michael Wrightd02c5b62014-02-10 15:10:22 -08003321// --- KeyboardInputMapperTest ---
3322
3323class KeyboardInputMapperTest : public InputMapperTest {
3324protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003325 const std::string UNIQUE_ID = "local:0";
3326
3327 void prepareDisplay(int32_t orientation);
3328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003329 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330 int32_t originalKeyCode, int32_t rotatedKeyCode,
3331 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332};
3333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3335 * orientation.
3336 */
3337void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003338 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3339 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003340}
3341
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003343 int32_t originalScanCode, int32_t originalKeyCode,
3344 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 NotifyKeyArgs args;
3346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3357 ASSERT_EQ(originalScanCode, args.scanCode);
3358 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003359 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360}
3361
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368}
3369
3370TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3371 const int32_t USAGE_A = 0x070004;
3372 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003373 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3374 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003375 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3376 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3377 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 KeyboardInputMapper& mapper =
3380 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3381 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003382 // Initial metastate is AMETA_NONE.
3383 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
3385 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387 NotifyKeyArgs args;
3388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3389 ASSERT_EQ(DEVICE_ID, args.deviceId);
3390 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3391 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3392 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3393 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3394 ASSERT_EQ(KEY_HOME, args.scanCode);
3395 ASSERT_EQ(AMETA_NONE, args.metaState);
3396 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3397 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3398 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3399
3400 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003401 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3403 ASSERT_EQ(DEVICE_ID, args.deviceId);
3404 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3405 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3406 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3407 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3408 ASSERT_EQ(KEY_HOME, args.scanCode);
3409 ASSERT_EQ(AMETA_NONE, args.metaState);
3410 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3411 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3412 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3413
3414 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3418 ASSERT_EQ(DEVICE_ID, args.deviceId);
3419 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3420 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3421 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3422 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3423 ASSERT_EQ(0, args.scanCode);
3424 ASSERT_EQ(AMETA_NONE, args.metaState);
3425 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3426 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3427 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3428
3429 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3431 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(DEVICE_ID, args.deviceId);
3434 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3435 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3436 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3437 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3438 ASSERT_EQ(0, args.scanCode);
3439 ASSERT_EQ(AMETA_NONE, args.metaState);
3440 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3441 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3442 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3443
3444 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(DEVICE_ID, args.deviceId);
3449 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3450 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3451 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3452 ASSERT_EQ(0, args.keyCode);
3453 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3454 ASSERT_EQ(AMETA_NONE, args.metaState);
3455 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3456 ASSERT_EQ(0U, args.policyFlags);
3457 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3458
3459 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3461 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3463 ASSERT_EQ(DEVICE_ID, args.deviceId);
3464 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3465 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3466 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3467 ASSERT_EQ(0, args.keyCode);
3468 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3469 ASSERT_EQ(AMETA_NONE, args.metaState);
3470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3471 ASSERT_EQ(0U, args.policyFlags);
3472 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3473}
3474
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475/**
3476 * Ensure that the readTime is set to the time when the EV_KEY is received.
3477 */
3478TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3480
3481 KeyboardInputMapper& mapper =
3482 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3483 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3484 NotifyKeyArgs args;
3485
3486 // Key down
3487 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3489 ASSERT_EQ(12, args.readTime);
3490
3491 // Key up
3492 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3494 ASSERT_EQ(15, args.readTime);
3495}
3496
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3501 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3502 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 KeyboardInputMapper& mapper =
3505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Arthur Hung95f68612022-04-07 14:08:22 +08003508 // Initial metastate is AMETA_NONE.
3509 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
3511 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513 NotifyKeyArgs args;
3514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
3519 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003520 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3522 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
3531 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003532 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3534 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003535 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003536 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537}
3538
3539TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003545 KeyboardInputMapper& mapper =
3546 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3547 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003549 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3551 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3553 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3557 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3558}
3559
3560TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003561 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3563 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3564 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003567 KeyboardInputMapper& mapper =
3568 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3569 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003571 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003572 ASSERT_NO_FATAL_FAILURE(
3573 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3576 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3577 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3579 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003581 clearViewports();
3582 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583 ASSERT_NO_FATAL_FAILURE(
3584 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3588 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3590 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 clearViewports();
3593 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594 ASSERT_NO_FATAL_FAILURE(
3595 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3597 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3599 AKEYCODE_DPAD_UP, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3601 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 clearViewports();
3604 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605 ASSERT_NO_FATAL_FAILURE(
3606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3608 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3610 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3612 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
3614 // Special case: if orientation changes while key is down, we still emit the same keycode
3615 // in the key up as we did in the key down.
3616 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
3618 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3621 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3622 ASSERT_EQ(KEY_UP, args.scanCode);
3623 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3624
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003625 clearViewports();
3626 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3630 ASSERT_EQ(KEY_UP, args.scanCode);
3631 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3632}
3633
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3635 // If the keyboard is not orientation aware,
3636 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 NotifyKeyArgs args;
3643
3644 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3649 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3650
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3656 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3657}
3658
3659TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3660 // If the keyboard is orientation aware,
3661 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 KeyboardInputMapper& mapper =
3666 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3667 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 NotifyKeyArgs args;
3669
3670 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3671 // ^--- already checked by the previous test
3672
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003673 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003674 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3679 ASSERT_EQ(DISPLAY_ID, args.displayId);
3680
3681 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003682 clearViewports();
3683 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003684 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3689 ASSERT_EQ(newDisplayId, args.displayId);
3690}
3691
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003693 KeyboardInputMapper& mapper =
3694 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3695 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003697 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003701 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702}
3703
Philip Junker4af3b3d2021-12-14 10:36:55 +01003704TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3705 KeyboardInputMapper& mapper =
3706 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3707 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3708
3709 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3710 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3711 << "If a mapping is available, the result is equal to the mapping";
3712
3713 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3714 << "If no mapping is available, the result is the key location";
3715}
3716
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 KeyboardInputMapper& mapper =
3719 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3720 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727}
3728
3729TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 KeyboardInputMapper& mapper =
3731 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3732 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003737 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 ASSERT_TRUE(flags[0]);
3739 ASSERT_FALSE(flags[1]);
3740}
3741
3742TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003743 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3744 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3745 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 KeyboardInputMapper& mapper =
3751 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3752 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003753 // Initial metastate is AMETA_NONE.
3754 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
3761 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003767 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
3769 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
3777 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3781 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
3785 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3789 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3790 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792
3793 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3797 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3798 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800
3801 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003804 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3805 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3806 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808}
3809
Chris Yea52ade12020-08-27 16:49:20 -07003810TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3811 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3814 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3815
3816 KeyboardInputMapper& mapper =
3817 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3818 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3819
Chris Yea52ade12020-08-27 16:49:20 -07003820 // Meta state should be AMETA_NONE after reset
3821 mapper.reset(ARBITRARY_TIME);
3822 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3823 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3824 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3825 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3826
3827 NotifyKeyArgs args;
3828 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3831 ASSERT_EQ(AMETA_NONE, args.metaState);
3832 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3833 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3834 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3835
3836 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003837 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3839 ASSERT_EQ(AMETA_NONE, args.metaState);
3840 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3841 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3842 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3843}
3844
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3846 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003851
3852 // keyboard 2.
3853 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003854 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003856 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 std::shared_ptr<InputDevice> device2 =
3858 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003859 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003861 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3862 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3863 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3864 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 KeyboardInputMapper& mapper =
3867 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3868 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003871 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003873 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3874 device2->reset(ARBITRARY_TIME);
3875
3876 // Prepared displays and associated info.
3877 constexpr uint8_t hdmi1 = 0;
3878 constexpr uint8_t hdmi2 = 1;
3879 const std::string SECONDARY_UNIQUE_ID = "local:1";
3880
3881 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3882 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3883
3884 // No associated display viewport found, should disable the device.
3885 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3887 ASSERT_FALSE(device2->isEnabled());
3888
3889 // Prepare second display.
3890 constexpr int32_t newDisplayId = 2;
3891 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003892 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003893 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003894 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003895 // Default device will reconfigure above, need additional reconfiguration for another device.
3896 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3897 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3898
3899 // Device should be enabled after the associated display is found.
3900 ASSERT_TRUE(mDevice->isEnabled());
3901 ASSERT_TRUE(device2->isEnabled());
3902
3903 // Test pad key events
3904 ASSERT_NO_FATAL_FAILURE(
3905 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3907 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3908 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3909 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3911 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3912
3913 ASSERT_NO_FATAL_FAILURE(
3914 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3916 AKEYCODE_DPAD_RIGHT, newDisplayId));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3918 AKEYCODE_DPAD_DOWN, newDisplayId));
3919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3920 AKEYCODE_DPAD_LEFT, newDisplayId));
3921}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922
arthurhungc903df12020-08-11 15:08:42 +08003923TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3924 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3925 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3926 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3929 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3930
3931 KeyboardInputMapper& mapper =
3932 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3933 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003934 // Initial metastate is AMETA_NONE.
3935 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003936
3937 // Initialization should have turned all of the lights off.
3938 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3939 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3940 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3941
3942 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003945 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3946 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3947
3948 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3953
3954 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003957 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3959
3960 mFakeEventHub->removeDevice(EVENTHUB_ID);
3961 mReader->loopOnce();
3962
3963 // keyboard 2 should default toggle keys.
3964 const std::string USB2 = "USB2";
3965 const std::string DEVICE_NAME2 = "KEYBOARD2";
3966 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3967 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3968 std::shared_ptr<InputDevice> device2 =
3969 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003970 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003971 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3972 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3973 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3974 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3975 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3976 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3977
arthurhung6fe95782020-10-05 22:41:16 +08003978 KeyboardInputMapper& mapper2 =
3979 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003981 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3982 device2->reset(ARBITRARY_TIME);
3983
3984 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3985 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3986 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003987 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3988 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003989}
3990
Arthur Hungcb40a002021-08-03 14:31:01 +00003991TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3995
3996 // Suppose we have two mappers. (DPAD + KEYBOARD)
3997 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3998 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3999 KeyboardInputMapper& mapper =
4000 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4001 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004002 // Initial metastate is AMETA_NONE.
4003 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004004
4005 mReader->toggleCapsLockState(DEVICE_ID);
4006 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4007}
4008
Arthur Hungfb3cc112022-04-13 07:39:50 +00004009TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4010 // keyboard 1.
4011 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4012 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4013 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4016 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4017
4018 KeyboardInputMapper& mapper1 =
4019 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4020 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4021
4022 // keyboard 2.
4023 const std::string USB2 = "USB2";
4024 const std::string DEVICE_NAME2 = "KEYBOARD2";
4025 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4026 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4027 std::shared_ptr<InputDevice> device2 =
4028 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4029 ftl::Flags<InputDeviceClass>(0));
4030 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4031 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4032 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4033 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4034 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4035 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4036
4037 KeyboardInputMapper& mapper2 =
4038 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4039 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4040 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4041 device2->reset(ARBITRARY_TIME);
4042
Arthur Hung95f68612022-04-07 14:08:22 +08004043 // Initial metastate is AMETA_NONE.
4044 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4045 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4046
4047 // Toggle num lock on and off.
4048 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4049 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004050 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4051 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4052 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4053
4054 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4055 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4056 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4057 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4058 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4059
4060 // Toggle caps lock on and off.
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4063 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4064 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4066
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4069 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4070 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4072
4073 // Toggle scroll lock on and off.
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4076 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4077 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4079
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4082 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4083 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4085}
4086
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004087// --- KeyboardInputMapperTest_ExternalDevice ---
4088
4089class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4090protected:
Chris Yea52ade12020-08-27 16:49:20 -07004091 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092};
4093
4094TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004095 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4096 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004097
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4099 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4100 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4101 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004103 KeyboardInputMapper& mapper =
4104 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4105 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004106
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004108 NotifyKeyArgs args;
4109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4110 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4114 ASSERT_EQ(uint32_t(0), args.policyFlags);
4115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4118 ASSERT_EQ(uint32_t(0), args.policyFlags);
4119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4122 ASSERT_EQ(uint32_t(0), args.policyFlags);
4123
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4126 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4127
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004128 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4131}
4132
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004134 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004135
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4138 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141 KeyboardInputMapper& mapper =
4142 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4143 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004144
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004146 NotifyKeyArgs args;
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4148 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4149
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(uint32_t(0), args.policyFlags);
4161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4169}
4170
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171// --- CursorInputMapperTest ---
4172
4173class CursorInputMapperTest : public InputMapperTest {
4174protected:
4175 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4176
Michael Wright17db18e2020-06-26 20:51:44 +01004177 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178
Chris Yea52ade12020-08-27 16:49:20 -07004179 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 InputMapperTest::SetUp();
4181
Michael Wright17db18e2020-06-26 20:51:44 +01004182 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004183 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 }
4185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004186 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4187 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004188
4189 void prepareDisplay(int32_t orientation) {
4190 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004191 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004192 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4193 orientation, uniqueId, NO_PORT, viewportType);
4194 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004195
4196 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4197 float pressure) {
4198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4199 0.0f, 0.0f, 0.0f, EPSILON));
4200 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201};
4202
4203const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4206 int32_t originalY, int32_t rotatedX,
4207 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 NotifyMotionArgs args;
4209
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004215 ASSERT_NO_FATAL_FAILURE(
4216 assertCursorPointerCoords(args.pointerCoords[0],
4217 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4218 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219}
4220
4221TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004225 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
4228TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233}
4234
4235TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004237 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
4239 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
4242 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004243 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4244 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4246 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4247
4248 // When the bounds are set, then there should be a valid motion range.
4249 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4250
4251 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004252 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253
4254 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4255 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4256 1, 800 - 1, 0.0f, 0.0f));
4257 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4258 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4259 2, 480 - 1, 0.0f, 0.0f));
4260 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4261 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4262 0.0f, 1.0f, 0.0f, 0.0f));
4263}
4264
4265TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
4269 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4273 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4274 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4275 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4276 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4277 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4278 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4279 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4280 0.0f, 1.0f, 0.0f, 0.0f));
4281}
4282
4283TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004285 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286
arthurhungdcef2dc2020-08-11 14:47:50 +08004287 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
4289 NotifyMotionArgs args;
4290
4291 // Button press.
4292 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4296 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4297 ASSERT_EQ(DEVICE_ID, args.deviceId);
4298 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4299 ASSERT_EQ(uint32_t(0), args.policyFlags);
4300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4301 ASSERT_EQ(0, args.flags);
4302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4304 ASSERT_EQ(0, args.edgeFlags);
4305 ASSERT_EQ(uint32_t(1), args.pointerCount);
4306 ASSERT_EQ(0, args.pointerProperties[0].id);
4307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004308 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4310 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4311 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4312
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4314 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4315 ASSERT_EQ(DEVICE_ID, args.deviceId);
4316 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4317 ASSERT_EQ(uint32_t(0), args.policyFlags);
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4319 ASSERT_EQ(0, args.flags);
4320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4322 ASSERT_EQ(0, args.edgeFlags);
4323 ASSERT_EQ(uint32_t(1), args.pointerCount);
4324 ASSERT_EQ(0, args.pointerProperties[0].id);
4325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4328 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4329 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4330
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004332 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4333 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4335 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4336 ASSERT_EQ(DEVICE_ID, args.deviceId);
4337 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4338 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4340 ASSERT_EQ(0, args.flags);
4341 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4342 ASSERT_EQ(0, args.buttonState);
4343 ASSERT_EQ(0, args.edgeFlags);
4344 ASSERT_EQ(uint32_t(1), args.pointerCount);
4345 ASSERT_EQ(0, args.pointerProperties[0].id);
4346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4349 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4350 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4351
4352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4353 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4354 ASSERT_EQ(DEVICE_ID, args.deviceId);
4355 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4356 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4358 ASSERT_EQ(0, args.flags);
4359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4360 ASSERT_EQ(0, args.buttonState);
4361 ASSERT_EQ(0, args.edgeFlags);
4362 ASSERT_EQ(uint32_t(1), args.pointerCount);
4363 ASSERT_EQ(0, args.pointerProperties[0].id);
4364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004365 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4367 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4368 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4369}
4370
4371TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004373 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374
4375 NotifyMotionArgs args;
4376
4377 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4383 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4384 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
4386 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4392 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393}
4394
4395TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398
4399 NotifyMotionArgs args;
4400
4401 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004406 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004411
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422}
4423
4424TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004426 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
4428 NotifyMotionArgs args;
4429
4430 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004437 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4438 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4439 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4444 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4445 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4454 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4455 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456
4457 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004463
4464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467}
4468
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004469TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004470 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004471 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4472 // need to be rotated.
4473 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004474 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004476 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4485}
4486
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004487TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004489 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4490 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004491 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004493 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4495 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4496 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4497 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4498 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4499 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4500 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4501 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4502
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004503 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004513 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4516 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4517 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4518 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4519 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4520 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4521 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4522
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004523 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4531 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532}
4533
4534TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004536 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537
4538 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4539 mFakePointerController->setPosition(100, 200);
4540 mFakePointerController->setButtonState(0);
4541
4542 NotifyMotionArgs motionArgs;
4543 NotifyKeyArgs keyArgs;
4544
4545 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4549 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4550 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004552 ASSERT_NO_FATAL_FAILURE(
4553 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4557 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4558 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004559 ASSERT_NO_FATAL_FAILURE(
4560 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004561
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 ASSERT_EQ(0, motionArgs.buttonState);
4567 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_EQ(0, motionArgs.buttonState);
4574 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
4578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004580 ASSERT_EQ(0, motionArgs.buttonState);
4581 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
4585 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4591 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4592 motionArgs.buttonState);
4593 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4594 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004595 ASSERT_NO_FATAL_FAILURE(
4596 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4600 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4601 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4602 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(
4604 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004605
4606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4609 motionArgs.buttonState);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4611 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(
4613 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004621 ASSERT_NO_FATAL_FAILURE(
4622 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004623
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4627 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004628 ASSERT_NO_FATAL_FAILURE(
4629 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4635 ASSERT_EQ(0, motionArgs.buttonState);
4636 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004637 ASSERT_NO_FATAL_FAILURE(
4638 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_EQ(0, motionArgs.buttonState);
4644 ASSERT_EQ(0, mFakePointerController->getButtonState());
4645 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004648
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4650 ASSERT_EQ(0, motionArgs.buttonState);
4651 ASSERT_EQ(0, mFakePointerController->getButtonState());
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655
4656 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4660 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4661 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004662
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4666 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004667 ASSERT_NO_FATAL_FAILURE(
4668 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4672 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4673 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 ASSERT_EQ(0, motionArgs.buttonState);
4682 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004683 ASSERT_NO_FATAL_FAILURE(
4684 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004685
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_EQ(0, motionArgs.buttonState);
4689 ASSERT_EQ(0, mFakePointerController->getButtonState());
4690
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(
4692 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4694 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4695 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4696
4697 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004698 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4701 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4702 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004703
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4707 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004708 ASSERT_NO_FATAL_FAILURE(
4709 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710
4711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4713 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4714 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_EQ(0, motionArgs.buttonState);
4723 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004724 ASSERT_NO_FATAL_FAILURE(
4725 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004726
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4728 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4729 ASSERT_EQ(0, motionArgs.buttonState);
4730 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004731 ASSERT_NO_FATAL_FAILURE(
4732 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4735 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4736 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4737
4738 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4743 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4748 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004749 ASSERT_NO_FATAL_FAILURE(
4750 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
4752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4755 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004756 ASSERT_NO_FATAL_FAILURE(
4757 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004762 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_EQ(0, motionArgs.buttonState);
4764 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004765 ASSERT_NO_FATAL_FAILURE(
4766 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4770 ASSERT_EQ(0, motionArgs.buttonState);
4771 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(
4773 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004774
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4776 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4777 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4778
4779 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4783 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4784 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004785
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4789 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004790 ASSERT_NO_FATAL_FAILURE(
4791 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004792
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4795 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4796 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004797 ASSERT_NO_FATAL_FAILURE(
4798 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004803 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004806 ASSERT_NO_FATAL_FAILURE(
4807 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4811 ASSERT_EQ(0, motionArgs.buttonState);
4812 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004813 ASSERT_NO_FATAL_FAILURE(
4814 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4817 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4818 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4819}
4820
4821TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004823 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824
4825 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4826 mFakePointerController->setPosition(100, 200);
4827 mFakePointerController->setButtonState(0);
4828
4829 NotifyMotionArgs args;
4830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004835 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4838 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004839 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004840}
4841
4842TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004843 addConfigurationProperty("cursor.mode", "pointer");
4844 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004845 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004846
4847 NotifyDeviceResetArgs resetArgs;
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4849 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4850 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4851
4852 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4853 mFakePointerController->setPosition(100, 200);
4854 mFakePointerController->setButtonState(0);
4855
4856 NotifyMotionArgs args;
4857
4858 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4863 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4866 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004867 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004868
4869 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4873 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4876 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4878 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4881 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4882
4883 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4885 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4887 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4890 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4892 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4895 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4896
4897 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4902 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4905 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004906 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004907
4908 // Disable pointer capture and check that the device generation got bumped
4909 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004910 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911 mFakePolicy->setPointerCapture(false);
4912 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004913 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004914
4915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4916 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4917 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4918
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4923 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4926 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004927 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928}
4929
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004930/**
4931 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4932 * pointer acceleration or speed processing should not be applied.
4933 */
4934TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4935 addConfigurationProperty("cursor.mode", "pointer");
4936 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4937 100.f /*high threshold*/, 10.f /*acceleration*/);
4938 mFakePolicy->setVelocityControlParams(testParams);
4939 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4940
4941 NotifyDeviceResetArgs resetArgs;
4942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4943 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4944 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4945
4946 NotifyMotionArgs args;
4947
4948 // Move and verify scale is applied.
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4953 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4954 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4955 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4956 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4957 ASSERT_GT(relX, 10);
4958 ASSERT_GT(relY, 20);
4959
4960 // Enable Pointer Capture
4961 mFakePolicy->setPointerCapture(true);
4962 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4963 NotifyPointerCaptureChangedArgs captureArgs;
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4965 ASSERT_TRUE(captureArgs.request.enable);
4966
4967 // Move and verify scale is not applied.
4968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4972 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4974 ASSERT_EQ(10, args.pointerCoords[0].getX());
4975 ASSERT_EQ(20, args.pointerCoords[0].getY());
4976}
4977
Prabir Pradhan208360b2022-06-24 18:37:04 +00004978TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4979 addConfigurationProperty("cursor.mode", "pointer");
4980 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4981
4982 NotifyDeviceResetArgs resetArgs;
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4984 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4985 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4986
4987 // Ensure the display is rotated.
4988 prepareDisplay(DISPLAY_ORIENTATION_90);
4989
4990 NotifyMotionArgs args;
4991
4992 // Verify that the coordinates are rotated.
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4997 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4999 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5000 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5001
5002 // Enable Pointer Capture.
5003 mFakePolicy->setPointerCapture(true);
5004 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5005 NotifyPointerCaptureChangedArgs captureArgs;
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5007 ASSERT_TRUE(captureArgs.request.enable);
5008
5009 // Move and verify rotation is not applied.
5010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5014 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5016 ASSERT_EQ(10, args.pointerCoords[0].getX());
5017 ASSERT_EQ(20, args.pointerCoords[0].getY());
5018}
5019
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005020TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005021 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005022
Garfield Tan888a6a42020-01-09 11:39:16 -08005023 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005024 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005025 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5026 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005027 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5028 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005029 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5030 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5031
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005032 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5033 mFakePointerController->setPosition(100, 200);
5034 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005035
5036 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5044 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01005045 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005046 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5047}
5048
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049// --- TouchInputMapperTest ---
5050
5051class TouchInputMapperTest : public InputMapperTest {
5052protected:
5053 static const int32_t RAW_X_MIN;
5054 static const int32_t RAW_X_MAX;
5055 static const int32_t RAW_Y_MIN;
5056 static const int32_t RAW_Y_MAX;
5057 static const int32_t RAW_TOUCH_MIN;
5058 static const int32_t RAW_TOUCH_MAX;
5059 static const int32_t RAW_TOOL_MIN;
5060 static const int32_t RAW_TOOL_MAX;
5061 static const int32_t RAW_PRESSURE_MIN;
5062 static const int32_t RAW_PRESSURE_MAX;
5063 static const int32_t RAW_ORIENTATION_MIN;
5064 static const int32_t RAW_ORIENTATION_MAX;
5065 static const int32_t RAW_DISTANCE_MIN;
5066 static const int32_t RAW_DISTANCE_MAX;
5067 static const int32_t RAW_TILT_MIN;
5068 static const int32_t RAW_TILT_MAX;
5069 static const int32_t RAW_ID_MIN;
5070 static const int32_t RAW_ID_MAX;
5071 static const int32_t RAW_SLOT_MIN;
5072 static const int32_t RAW_SLOT_MAX;
5073 static const float X_PRECISION;
5074 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005075 static const float X_PRECISION_VIRTUAL;
5076 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077
5078 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005079 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080
5081 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5082
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005083 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005084 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005085
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086 enum Axes {
5087 POSITION = 1 << 0,
5088 TOUCH = 1 << 1,
5089 TOOL = 1 << 2,
5090 PRESSURE = 1 << 3,
5091 ORIENTATION = 1 << 4,
5092 MINOR = 1 << 5,
5093 ID = 1 << 6,
5094 DISTANCE = 1 << 7,
5095 TILT = 1 << 8,
5096 SLOT = 1 << 9,
5097 TOOL_TYPE = 1 << 10,
5098 };
5099
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005100 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5101 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005102 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005104 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 int32_t toRawX(float displayX);
5106 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005107 int32_t toRotatedRawX(float displayX);
5108 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005109 float toCookedX(float rawX, float rawY);
5110 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005112 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005114 float toDisplayY(int32_t rawY, int32_t displayHeight);
5115
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116};
5117
5118const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5119const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5120const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5121const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5122const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5123const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5124const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5125const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005126const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5127const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5129const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5130const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5131const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5132const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5133const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5134const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5135const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5136const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5137const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5138const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5139const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005140const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5141 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5142const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5143 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005144const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5145 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146
5147const float TouchInputMapperTest::GEOMETRIC_SCALE =
5148 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5149 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5150
5151const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5152 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5153 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5154};
5155
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005156void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005157 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5158 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005159}
5160
5161void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5162 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5163 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164}
5165
Santos Cordonfa5cf462017-04-05 10:37:00 -07005166void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005167 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5168 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5169 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005170}
5171
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005173 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5174 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177}
5178
Jason Gerecke489fda82012-09-07 17:19:40 -07005179void TouchInputMapperTest::prepareLocationCalibration() {
5180 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5181}
5182
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183int32_t TouchInputMapperTest::toRawX(float displayX) {
5184 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5185}
5186
5187int32_t TouchInputMapperTest::toRawY(float displayY) {
5188 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5189}
5190
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005191int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5192 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5193}
5194
5195int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5196 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5197}
5198
Jason Gerecke489fda82012-09-07 17:19:40 -07005199float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5200 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5201 return rawX;
5202}
5203
5204float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5205 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5206 return rawY;
5207}
5208
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005210 return toDisplayX(rawX, DISPLAY_WIDTH);
5211}
5212
5213float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5214 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215}
5216
5217float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005218 return toDisplayY(rawY, DISPLAY_HEIGHT);
5219}
5220
5221float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5222 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
5225
5226// --- SingleTouchInputMapperTest ---
5227
5228class SingleTouchInputMapperTest : public TouchInputMapperTest {
5229protected:
5230 void prepareButtons();
5231 void prepareAxes(int axes);
5232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005233 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5234 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5235 void processUp(SingleTouchInputMapper& mappery);
5236 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5237 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5238 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5239 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5240 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5241 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242};
5243
5244void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005245 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246}
5247
5248void SingleTouchInputMapperTest::prepareAxes(int axes) {
5249 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 }
5253 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5255 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 }
5257 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5259 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 }
5261 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005262 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5263 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 }
5265 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5267 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 }
5269}
5270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005271void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005275}
5276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005277void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005280}
5281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005282void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284}
5285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005286void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288}
5289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005290void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5291 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005295void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297}
5298
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5300 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005303}
5304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005305void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5306 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308}
5309
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005310void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005312}
5313
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 prepareButtons();
5316 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005319 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320}
5321
5322TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005323 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5324 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 prepareButtons();
5326 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005327 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
5332TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 prepareButtons();
5334 prepareAxes(POSITION);
5335 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005336 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005338 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339}
5340
5341TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005342 prepareButtons();
5343 prepareAxes(POSITION);
5344 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005345 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348}
5349
5350TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005351 addConfigurationProperty("touch.deviceType", "touchScreen");
5352 prepareDisplay(DISPLAY_ORIENTATION_0);
5353 prepareButtons();
5354 prepareAxes(POSITION);
5355 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005356 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357
5358 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360
5361 // Virtual key is down.
5362 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5363 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5364 processDown(mapper, x, y);
5365 processSync(mapper);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5367
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 // Virtual key is up.
5371 processUp(mapper);
5372 processSync(mapper);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005375 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376}
5377
5378TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 addConfigurationProperty("touch.deviceType", "touchScreen");
5380 prepareDisplay(DISPLAY_ORIENTATION_0);
5381 prepareButtons();
5382 prepareAxes(POSITION);
5383 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
5386 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388
5389 // Virtual key is down.
5390 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5391 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5392 processDown(mapper, x, y);
5393 processSync(mapper);
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5395
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
5398 // Virtual key is up.
5399 processUp(mapper);
5400 processSync(mapper);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5402
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005403 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404}
5405
5406TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 addConfigurationProperty("touch.deviceType", "touchScreen");
5408 prepareDisplay(DISPLAY_ORIENTATION_0);
5409 prepareButtons();
5410 prepareAxes(POSITION);
5411 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005415 ASSERT_TRUE(
5416 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 ASSERT_TRUE(flags[0]);
5418 ASSERT_FALSE(flags[1]);
5419}
5420
5421TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 addConfigurationProperty("touch.deviceType", "touchScreen");
5423 prepareDisplay(DISPLAY_ORIENTATION_0);
5424 prepareButtons();
5425 prepareAxes(POSITION);
5426 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005427 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428
arthurhungdcef2dc2020-08-11 14:47:50 +08005429 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430
5431 NotifyKeyArgs args;
5432
5433 // Press virtual key.
5434 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5435 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5436 processDown(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5440 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5441 ASSERT_EQ(DEVICE_ID, args.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5443 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5445 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5446 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5447 ASSERT_EQ(KEY_HOME, args.scanCode);
5448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5449 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5450
5451 // Release virtual key.
5452 processUp(mapper);
5453 processSync(mapper);
5454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5457 ASSERT_EQ(DEVICE_ID, args.deviceId);
5458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5459 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5460 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5461 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5462 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5463 ASSERT_EQ(KEY_HOME, args.scanCode);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5466
5467 // Should not have sent any motions.
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5469}
5470
5471TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 addConfigurationProperty("touch.deviceType", "touchScreen");
5473 prepareDisplay(DISPLAY_ORIENTATION_0);
5474 prepareButtons();
5475 prepareAxes(POSITION);
5476 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478
arthurhungdcef2dc2020-08-11 14:47:50 +08005479 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480
5481 NotifyKeyArgs keyArgs;
5482
5483 // Press virtual key.
5484 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5485 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5486 processDown(mapper, x, y);
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5490 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5491 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5493 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5494 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5496 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5497 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5499 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5500
5501 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5502 // into the display area.
5503 y -= 100;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5511 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5513 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5514 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5515 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5516 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5517 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5518 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5519
5520 NotifyMotionArgs motionArgs;
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5525 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5527 ASSERT_EQ(0, motionArgs.flags);
5528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5529 ASSERT_EQ(0, motionArgs.buttonState);
5530 ASSERT_EQ(0, motionArgs.edgeFlags);
5531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5532 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5535 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5536 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5537 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5538 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5539
5540 // Keep moving out of bounds. Should generate a pointer move.
5541 y -= 50;
5542 processMove(mapper, x, y);
5543 processSync(mapper);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5547 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5548 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5549 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5551 ASSERT_EQ(0, motionArgs.flags);
5552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5553 ASSERT_EQ(0, motionArgs.buttonState);
5554 ASSERT_EQ(0, motionArgs.edgeFlags);
5555 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5556 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5559 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5560 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5561 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5563
5564 // Release out of bounds. Should generate a pointer up.
5565 processUp(mapper);
5566 processSync(mapper);
5567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5570 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5571 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5572 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5574 ASSERT_EQ(0, motionArgs.flags);
5575 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5576 ASSERT_EQ(0, motionArgs.buttonState);
5577 ASSERT_EQ(0, motionArgs.edgeFlags);
5578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5582 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5583 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5584 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5586
5587 // Should not have sent any more keys or motions.
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5590}
5591
5592TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593 addConfigurationProperty("touch.deviceType", "touchScreen");
5594 prepareDisplay(DISPLAY_ORIENTATION_0);
5595 prepareButtons();
5596 prepareAxes(POSITION);
5597 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005598 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599
arthurhungdcef2dc2020-08-11 14:47:50 +08005600 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 NotifyMotionArgs motionArgs;
5603
5604 // Initially go down out of bounds.
5605 int32_t x = -10;
5606 int32_t y = -10;
5607 processDown(mapper, x, y);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5611
5612 // Move into the display area. Should generate a pointer down.
5613 x = 50;
5614 y = 75;
5615 processMove(mapper, x, y);
5616 processSync(mapper);
5617
5618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5621 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5622 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5623 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5624 ASSERT_EQ(0, motionArgs.flags);
5625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5626 ASSERT_EQ(0, motionArgs.buttonState);
5627 ASSERT_EQ(0, motionArgs.edgeFlags);
5628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5632 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5633 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5634 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5636
5637 // Release. Should generate a pointer up.
5638 processUp(mapper);
5639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5647 ASSERT_EQ(0, motionArgs.flags);
5648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5649 ASSERT_EQ(0, motionArgs.buttonState);
5650 ASSERT_EQ(0, motionArgs.edgeFlags);
5651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5655 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5659
5660 // Should not have sent any more keys or motions.
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5663}
5664
Santos Cordonfa5cf462017-04-05 10:37:00 -07005665TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005666 addConfigurationProperty("touch.deviceType", "touchScreen");
5667 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5668
5669 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5670 prepareButtons();
5671 prepareAxes(POSITION);
5672 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005674
arthurhungdcef2dc2020-08-11 14:47:50 +08005675 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005676
5677 NotifyMotionArgs motionArgs;
5678
5679 // Down.
5680 int32_t x = 100;
5681 int32_t y = 125;
5682 processDown(mapper, x, y);
5683 processSync(mapper);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5687 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5688 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5689 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5690 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5692 ASSERT_EQ(0, motionArgs.flags);
5693 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5694 ASSERT_EQ(0, motionArgs.buttonState);
5695 ASSERT_EQ(0, motionArgs.edgeFlags);
5696 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5700 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5701 1, 0, 0, 0, 0, 0, 0, 0));
5702 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5703 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5705
5706 // Move.
5707 x += 50;
5708 y += 75;
5709 processMove(mapper, x, y);
5710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5714 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5715 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5716 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5717 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5719 ASSERT_EQ(0, motionArgs.flags);
5720 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5721 ASSERT_EQ(0, motionArgs.buttonState);
5722 ASSERT_EQ(0, motionArgs.edgeFlags);
5723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5724 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5728 1, 0, 0, 0, 0, 0, 0, 0));
5729 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5730 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5731 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5732
5733 // Up.
5734 processUp(mapper);
5735 processSync(mapper);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5739 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5740 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5744 ASSERT_EQ(0, motionArgs.flags);
5745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5746 ASSERT_EQ(0, motionArgs.buttonState);
5747 ASSERT_EQ(0, motionArgs.edgeFlags);
5748 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5752 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5753 1, 0, 0, 0, 0, 0, 0, 0));
5754 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5755 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5757
5758 // Should not have sent any more keys or motions.
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5761}
5762
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764 addConfigurationProperty("touch.deviceType", "touchScreen");
5765 prepareDisplay(DISPLAY_ORIENTATION_0);
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005769 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
arthurhungdcef2dc2020-08-11 14:47:50 +08005771 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 NotifyMotionArgs motionArgs;
5774
5775 // Down.
5776 int32_t x = 100;
5777 int32_t y = 125;
5778 processDown(mapper, x, y);
5779 processSync(mapper);
5780
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5783 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5784 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5785 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5787 ASSERT_EQ(0, motionArgs.flags);
5788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5789 ASSERT_EQ(0, motionArgs.buttonState);
5790 ASSERT_EQ(0, motionArgs.edgeFlags);
5791 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5792 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5795 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5799
5800 // Move.
5801 x += 50;
5802 y += 75;
5803 processMove(mapper, x, y);
5804 processSync(mapper);
5805
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5809 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5810 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5812 ASSERT_EQ(0, motionArgs.flags);
5813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5814 ASSERT_EQ(0, motionArgs.buttonState);
5815 ASSERT_EQ(0, motionArgs.edgeFlags);
5816 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5820 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5821 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5822 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5824
5825 // Up.
5826 processUp(mapper);
5827 processSync(mapper);
5828
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5835 ASSERT_EQ(0, motionArgs.flags);
5836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5837 ASSERT_EQ(0, motionArgs.buttonState);
5838 ASSERT_EQ(0, motionArgs.edgeFlags);
5839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5840 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5847
5848 // Should not have sent any more keys or motions.
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5851}
5852
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005853TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 addConfigurationProperty("touch.deviceType", "touchScreen");
5855 prepareButtons();
5856 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005857 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5858 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860
5861 NotifyMotionArgs args;
5862
5863 // Rotation 90.
5864 prepareDisplay(DISPLAY_ORIENTATION_90);
5865 processDown(mapper, toRawX(50), toRawY(75));
5866 processSync(mapper);
5867
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5869 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5870 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5871
5872 processUp(mapper);
5873 processSync(mapper);
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5875}
5876
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005877TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878 addConfigurationProperty("touch.deviceType", "touchScreen");
5879 prepareButtons();
5880 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005881 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5882 // orientation-aware are affected by display rotation.
5883 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005884 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 NotifyMotionArgs args;
5887
5888 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005889 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 processDown(mapper, toRawX(50), toRawY(75));
5892 processSync(mapper);
5893
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5895 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5896 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5897
5898 processUp(mapper);
5899 processSync(mapper);
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5901
5902 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005903 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005905 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906 processSync(mapper);
5907
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5909 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5910 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5911
5912 processUp(mapper);
5913 processSync(mapper);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5915
5916 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005917 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 prepareDisplay(DISPLAY_ORIENTATION_180);
5919 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5920 processSync(mapper);
5921
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5923 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5924 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5925
5926 processUp(mapper);
5927 processSync(mapper);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5929
5930 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005931 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005933 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 processSync(mapper);
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5937 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5938 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5939
5940 processUp(mapper);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5943}
5944
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005945TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5946 addConfigurationProperty("touch.deviceType", "touchScreen");
5947 prepareButtons();
5948 prepareAxes(POSITION);
5949 addConfigurationProperty("touch.orientationAware", "1");
5950 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5951 clearViewports();
5952 prepareDisplay(DISPLAY_ORIENTATION_0);
5953 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5954 NotifyMotionArgs args;
5955
5956 // Orientation 0.
5957 processDown(mapper, toRawX(50), toRawY(75));
5958 processSync(mapper);
5959
5960 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5961 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5962 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5963
5964 processUp(mapper);
5965 processSync(mapper);
5966 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5967}
5968
5969TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5970 addConfigurationProperty("touch.deviceType", "touchScreen");
5971 prepareButtons();
5972 prepareAxes(POSITION);
5973 addConfigurationProperty("touch.orientationAware", "1");
5974 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5975 clearViewports();
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5978 NotifyMotionArgs args;
5979
5980 // Orientation 90.
5981 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5982 processSync(mapper);
5983
5984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5985 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5986 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5987
5988 processUp(mapper);
5989 processSync(mapper);
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5991}
5992
5993TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareButtons();
5996 prepareAxes(POSITION);
5997 addConfigurationProperty("touch.orientationAware", "1");
5998 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5999 clearViewports();
6000 prepareDisplay(DISPLAY_ORIENTATION_0);
6001 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6002 NotifyMotionArgs args;
6003
6004 // Orientation 180.
6005 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6006 processSync(mapper);
6007
6008 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6009 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6010 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6011
6012 processUp(mapper);
6013 processSync(mapper);
6014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6015}
6016
6017TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6018 addConfigurationProperty("touch.deviceType", "touchScreen");
6019 prepareButtons();
6020 prepareAxes(POSITION);
6021 addConfigurationProperty("touch.orientationAware", "1");
6022 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6023 clearViewports();
6024 prepareDisplay(DISPLAY_ORIENTATION_0);
6025 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6026 NotifyMotionArgs args;
6027
6028 // Orientation 270.
6029 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6030 processSync(mapper);
6031
6032 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6033 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6034 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6035
6036 processUp(mapper);
6037 processSync(mapper);
6038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6039}
6040
6041TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6042 addConfigurationProperty("touch.deviceType", "touchScreen");
6043 prepareButtons();
6044 prepareAxes(POSITION);
6045 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6046 // orientation-aware are affected by display rotation.
6047 addConfigurationProperty("touch.orientationAware", "0");
6048 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6049 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6050
6051 NotifyMotionArgs args;
6052
6053 // Orientation 90, Rotation 0.
6054 clearViewports();
6055 prepareDisplay(DISPLAY_ORIENTATION_0);
6056 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6057 processSync(mapper);
6058
6059 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6060 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6061 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6062
6063 processUp(mapper);
6064 processSync(mapper);
6065 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6066
6067 // Orientation 90, Rotation 90.
6068 clearViewports();
6069 prepareDisplay(DISPLAY_ORIENTATION_90);
6070 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6071 processSync(mapper);
6072
6073 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6074 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6075 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6076
6077 processUp(mapper);
6078 processSync(mapper);
6079 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6080
6081 // Orientation 90, Rotation 180.
6082 clearViewports();
6083 prepareDisplay(DISPLAY_ORIENTATION_180);
6084 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6085 processSync(mapper);
6086
6087 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6088 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6089 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6090
6091 processUp(mapper);
6092 processSync(mapper);
6093 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6094
6095 // Orientation 90, Rotation 270.
6096 clearViewports();
6097 prepareDisplay(DISPLAY_ORIENTATION_270);
6098 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6099 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6100 processSync(mapper);
6101
6102 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6103 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6104 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6105
6106 processUp(mapper);
6107 processSync(mapper);
6108 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6109}
6110
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareButtons();
6115 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006116 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117
6118 // These calculations are based on the input device calibration documentation.
6119 int32_t rawX = 100;
6120 int32_t rawY = 200;
6121 int32_t rawPressure = 10;
6122 int32_t rawToolMajor = 12;
6123 int32_t rawDistance = 2;
6124 int32_t rawTiltX = 30;
6125 int32_t rawTiltY = 110;
6126
6127 float x = toDisplayX(rawX);
6128 float y = toDisplayY(rawY);
6129 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6130 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6131 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6132 float distance = float(rawDistance);
6133
6134 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6135 float tiltScale = M_PI / 180;
6136 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6137 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6138 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6139 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6140
6141 processDown(mapper, rawX, rawY);
6142 processPressure(mapper, rawPressure);
6143 processToolMajor(mapper, rawToolMajor);
6144 processDistance(mapper, rawDistance);
6145 processTilt(mapper, rawTiltX, rawTiltY);
6146 processSync(mapper);
6147
6148 NotifyMotionArgs args;
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6151 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6152 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6153}
6154
Jason Gerecke489fda82012-09-07 17:19:40 -07006155TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006156 addConfigurationProperty("touch.deviceType", "touchScreen");
6157 prepareDisplay(DISPLAY_ORIENTATION_0);
6158 prepareLocationCalibration();
6159 prepareButtons();
6160 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006161 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006162
6163 int32_t rawX = 100;
6164 int32_t rawY = 200;
6165
6166 float x = toDisplayX(toCookedX(rawX, rawY));
6167 float y = toDisplayY(toCookedY(rawX, rawY));
6168
6169 processDown(mapper, rawX, rawY);
6170 processSync(mapper);
6171
6172 NotifyMotionArgs args;
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6175 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6176}
6177
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 addConfigurationProperty("touch.deviceType", "touchScreen");
6180 prepareDisplay(DISPLAY_ORIENTATION_0);
6181 prepareButtons();
6182 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006183 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 NotifyMotionArgs motionArgs;
6186 NotifyKeyArgs keyArgs;
6187
6188 processDown(mapper, 100, 200);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6192 ASSERT_EQ(0, motionArgs.buttonState);
6193
6194 // press BTN_LEFT, release BTN_LEFT
6195 processKey(mapper, BTN_LEFT, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6200
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6203 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6204
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 processKey(mapper, BTN_LEFT, 0);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006213 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6216 processKey(mapper, BTN_RIGHT, 1);
6217 processKey(mapper, BTN_MIDDLE, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6222 motionArgs.buttonState);
6223
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6231 motionArgs.buttonState);
6232
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 processKey(mapper, BTN_RIGHT, 0);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006241 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 processKey(mapper, BTN_MIDDLE, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252
6253 // press BTN_BACK, release BTN_BACK
6254 processKey(mapper, BTN_BACK, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6258 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006259
Michael Wrightd02c5b62014-02-10 15:10:22 -08006260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006262 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 processKey(mapper, BTN_BACK, 0);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006273
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
6277
Michael Wrightd02c5b62014-02-10 15:10:22 -08006278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6280 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6281
6282 // press BTN_SIDE, release BTN_SIDE
6283 processKey(mapper, BTN_SIDE, 1);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6286 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6287 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006288
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6292
6293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6295 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 processKey(mapper, BTN_SIDE, 0);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006301 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006305 ASSERT_EQ(0, motionArgs.buttonState);
6306
Michael Wrightd02c5b62014-02-10 15:10:22 -08006307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6308 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6310
6311 // press BTN_FORWARD, release BTN_FORWARD
6312 processKey(mapper, BTN_FORWARD, 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6316 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006317
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006320 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6321
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325
6326 processKey(mapper, BTN_FORWARD, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006330 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006334 ASSERT_EQ(0, motionArgs.buttonState);
6335
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6338 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6339
6340 // press BTN_EXTRA, release BTN_EXTRA
6341 processKey(mapper, BTN_EXTRA, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6345 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006346
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006349 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6353 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
6355 processKey(mapper, BTN_EXTRA, 0);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006363 ASSERT_EQ(0, motionArgs.buttonState);
6364
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6368
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6370
Michael Wrightd02c5b62014-02-10 15:10:22 -08006371 // press BTN_STYLUS, release BTN_STYLUS
6372 processKey(mapper, BTN_STYLUS, 1);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6380 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 processKey(mapper, BTN_STYLUS, 0);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006387
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391
6392 // press BTN_STYLUS2, release BTN_STYLUS2
6393 processKey(mapper, BTN_STYLUS2, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006397 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6398
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6400 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6401 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402
6403 processKey(mapper, BTN_STYLUS2, 0);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006408
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006411 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412
6413 // release touch
6414 processUp(mapper);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6418 ASSERT_EQ(0, motionArgs.buttonState);
6419}
6420
6421TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006422 addConfigurationProperty("touch.deviceType", "touchScreen");
6423 prepareDisplay(DISPLAY_ORIENTATION_0);
6424 prepareButtons();
6425 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006426 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427
6428 NotifyMotionArgs motionArgs;
6429
6430 // default tool type is finger
6431 processDown(mapper, 100, 200);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6436
6437 // eraser
6438 processKey(mapper, BTN_TOOL_RUBBER, 1);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6443
6444 // stylus
6445 processKey(mapper, BTN_TOOL_RUBBER, 0);
6446 processKey(mapper, BTN_TOOL_PEN, 1);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6451
6452 // brush
6453 processKey(mapper, BTN_TOOL_PEN, 0);
6454 processKey(mapper, BTN_TOOL_BRUSH, 1);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6459
6460 // pencil
6461 processKey(mapper, BTN_TOOL_BRUSH, 0);
6462 processKey(mapper, BTN_TOOL_PENCIL, 1);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6467
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006468 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 processKey(mapper, BTN_TOOL_PENCIL, 0);
6470 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6475
6476 // mouse
6477 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6478 processKey(mapper, BTN_TOOL_MOUSE, 1);
6479 processSync(mapper);
6480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6483
6484 // lens
6485 processKey(mapper, BTN_TOOL_MOUSE, 0);
6486 processKey(mapper, BTN_TOOL_LENS, 1);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6491
6492 // double-tap
6493 processKey(mapper, BTN_TOOL_LENS, 0);
6494 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6499
6500 // triple-tap
6501 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6502 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6503 processSync(mapper);
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507
6508 // quad-tap
6509 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6510 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6515
6516 // finger
6517 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6518 processKey(mapper, BTN_TOOL_FINGER, 1);
6519 processSync(mapper);
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6523
6524 // stylus trumps finger
6525 processKey(mapper, BTN_TOOL_PEN, 1);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6530
6531 // eraser trumps stylus
6532 processKey(mapper, BTN_TOOL_RUBBER, 1);
6533 processSync(mapper);
6534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6537
6538 // mouse trumps eraser
6539 processKey(mapper, BTN_TOOL_MOUSE, 1);
6540 processSync(mapper);
6541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6544
6545 // back to default tool type
6546 processKey(mapper, BTN_TOOL_MOUSE, 0);
6547 processKey(mapper, BTN_TOOL_RUBBER, 0);
6548 processKey(mapper, BTN_TOOL_PEN, 0);
6549 processKey(mapper, BTN_TOOL_FINGER, 0);
6550 processSync(mapper);
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6554}
6555
6556TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557 addConfigurationProperty("touch.deviceType", "touchScreen");
6558 prepareDisplay(DISPLAY_ORIENTATION_0);
6559 prepareButtons();
6560 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006561 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006562 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006563
6564 NotifyMotionArgs motionArgs;
6565
6566 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6567 processKey(mapper, BTN_TOOL_FINGER, 1);
6568 processMove(mapper, 100, 200);
6569 processSync(mapper);
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6573 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6579
6580 // move a little
6581 processMove(mapper, 150, 250);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6587
6588 // down when BTN_TOUCH is pressed, pressure defaults to 1
6589 processKey(mapper, BTN_TOUCH, 1);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6595
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6599 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6600
6601 // up when BTN_TOUCH is released, hover restored
6602 processKey(mapper, BTN_TOUCH, 0);
6603 processSync(mapper);
6604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6605 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6607 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6608
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6612 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6613
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6618
6619 // exit hover when pointer goes away
6620 processKey(mapper, BTN_TOOL_FINGER, 0);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6626}
6627
6628TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006629 addConfigurationProperty("touch.deviceType", "touchScreen");
6630 prepareDisplay(DISPLAY_ORIENTATION_0);
6631 prepareButtons();
6632 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006633 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634
6635 NotifyMotionArgs motionArgs;
6636
6637 // initially hovering because pressure is 0
6638 processDown(mapper, 100, 200);
6639 processPressure(mapper, 0);
6640 processSync(mapper);
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6644 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6645
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 // move a little
6652 processMove(mapper, 150, 250);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6657 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // down when pressure is non-zero
6660 processPressure(mapper, RAW_PRESSURE_MAX);
6661 processSync(mapper);
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6670 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6671
6672 // up when pressure becomes 0, hover restored
6673 processPressure(mapper, 0);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6679
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6688 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6689
6690 // exit hover when pointer goes away
6691 processUp(mapper);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6697}
6698
lilinnan687e58f2022-07-19 16:00:50 +08006699TEST_F(SingleTouchInputMapperTest,
6700 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6701 addConfigurationProperty("touch.deviceType", "touchScreen");
6702 prepareDisplay(DISPLAY_ORIENTATION_0);
6703 prepareButtons();
6704 prepareAxes(POSITION);
6705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6706 NotifyMotionArgs motionArgs;
6707
6708 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006709 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006710 processSync(mapper);
6711
6712 // We should receive a down event
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6714 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6715
6716 // Change display id
6717 clearViewports();
6718 prepareSecondaryDisplay(ViewportType::INTERNAL);
6719
6720 // We should receive a cancel event
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6722 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6723 // Then receive reset called
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6725}
6726
Prabir Pradhan5632d622021-09-06 07:57:20 -07006727// --- TouchDisplayProjectionTest ---
6728
6729class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6730public:
6731 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6732 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6733 // rotated equivalent of the given un-rotated physical display bounds.
6734 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6735 uint32_t inverseRotationFlags;
6736 auto width = DISPLAY_WIDTH;
6737 auto height = DISPLAY_HEIGHT;
6738 switch (orientation) {
6739 case DISPLAY_ORIENTATION_90:
6740 inverseRotationFlags = ui::Transform::ROT_270;
6741 std::swap(width, height);
6742 break;
6743 case DISPLAY_ORIENTATION_180:
6744 inverseRotationFlags = ui::Transform::ROT_180;
6745 break;
6746 case DISPLAY_ORIENTATION_270:
6747 inverseRotationFlags = ui::Transform::ROT_90;
6748 std::swap(width, height);
6749 break;
6750 case DISPLAY_ORIENTATION_0:
6751 inverseRotationFlags = ui::Transform::ROT_0;
6752 break;
6753 default:
6754 FAIL() << "Invalid orientation: " << orientation;
6755 }
6756
6757 const ui::Transform rotation(inverseRotationFlags, width, height);
6758 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6759
6760 std::optional<DisplayViewport> internalViewport =
6761 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6762 DisplayViewport& v = *internalViewport;
6763 v.displayId = DISPLAY_ID;
6764 v.orientation = orientation;
6765
6766 v.logicalLeft = 0;
6767 v.logicalTop = 0;
6768 v.logicalRight = 100;
6769 v.logicalBottom = 100;
6770
6771 v.physicalLeft = rotatedPhysicalDisplay.left;
6772 v.physicalTop = rotatedPhysicalDisplay.top;
6773 v.physicalRight = rotatedPhysicalDisplay.right;
6774 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6775
6776 v.deviceWidth = width;
6777 v.deviceHeight = height;
6778
6779 v.isActive = true;
6780 v.uniqueId = UNIQUE_ID;
6781 v.type = ViewportType::INTERNAL;
6782 mFakePolicy->updateViewport(v);
6783 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6784 }
6785
6786 void assertReceivedMove(const Point& point) {
6787 NotifyMotionArgs motionArgs;
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6792 1, 0, 0, 0, 0, 0, 0, 0));
6793 }
6794};
6795
6796TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6797 addConfigurationProperty("touch.deviceType", "touchScreen");
6798 prepareDisplay(DISPLAY_ORIENTATION_0);
6799
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803
6804 NotifyMotionArgs motionArgs;
6805
6806 // Configure the DisplayViewport such that the logical display maps to a subsection of
6807 // the display panel called the physical display. Here, the physical display is bounded by the
6808 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6809 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6810 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6811 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6812
6813 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6814 DISPLAY_ORIENTATION_270}) {
6815 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6816
6817 // Touches outside the physical display should be ignored, and should not generate any
6818 // events. Ensure touches at the following points that lie outside of the physical display
6819 // area do not generate any events.
6820 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6821 processDown(mapper, toRawX(point.x), toRawY(point.y));
6822 processSync(mapper);
6823 processUp(mapper);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6826 << "Unexpected event generated for touch outside physical display at point: "
6827 << point.x << ", " << point.y;
6828 }
6829 }
6830}
6831
6832TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6833 addConfigurationProperty("touch.deviceType", "touchScreen");
6834 prepareDisplay(DISPLAY_ORIENTATION_0);
6835
6836 prepareButtons();
6837 prepareAxes(POSITION);
6838 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6839
6840 NotifyMotionArgs motionArgs;
6841
6842 // Configure the DisplayViewport such that the logical display maps to a subsection of
6843 // the display panel called the physical display. Here, the physical display is bounded by the
6844 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6845 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6846
6847 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6848 DISPLAY_ORIENTATION_270}) {
6849 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6850
6851 // Touches that start outside the physical display should be ignored until it enters the
6852 // physical display bounds, at which point it should generate a down event. Start a touch at
6853 // the point (5, 100), which is outside the physical display bounds.
6854 static const Point kOutsidePoint{5, 100};
6855 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6856 processSync(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6858
6859 // Move the touch into the physical display area. This should generate a pointer down.
6860 processMove(mapper, toRawX(11), toRawY(21));
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6863 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6864 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6865 ASSERT_NO_FATAL_FAILURE(
6866 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6867
6868 // Move the touch inside the physical display area. This should generate a pointer move.
6869 processMove(mapper, toRawX(69), toRawY(159));
6870 processSync(mapper);
6871 assertReceivedMove({69, 159});
6872
6873 // Move outside the physical display area. Since the pointer is already down, this should
6874 // now continue generating events.
6875 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6876 processSync(mapper);
6877 assertReceivedMove(kOutsidePoint);
6878
6879 // Release. This should generate a pointer up.
6880 processUp(mapper);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6885 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6886
6887 // Ensure no more events were generated.
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6890 }
6891}
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893// --- MultiTouchInputMapperTest ---
6894
6895class MultiTouchInputMapperTest : public TouchInputMapperTest {
6896protected:
6897 void prepareAxes(int axes);
6898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006899 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6900 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6901 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6902 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6903 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6904 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6905 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6906 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6907 void processId(MultiTouchInputMapper& mapper, int32_t id);
6908 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6909 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6910 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6911 void processMTSync(MultiTouchInputMapper& mapper);
6912 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913};
6914
6915void MultiTouchInputMapperTest::prepareAxes(int axes) {
6916 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006917 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919 }
6920 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6922 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006923 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6925 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926 }
6927 }
6928 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6930 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006933 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934 }
6935 }
6936 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6938 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939 }
6940 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6942 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943 }
6944 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006945 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6946 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947 }
6948 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6950 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951 }
6952 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006953 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6954 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955 }
6956 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958 }
6959}
6960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006961void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6962 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965}
6966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006967void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6968 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970}
6971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006972void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6973 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975}
6976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006977void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979}
6980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006981void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983}
6984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006985void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6986 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988}
6989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006990void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992}
6993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006994void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996}
6997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006998void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000}
7001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007002void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004}
7005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008}
7009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007010void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7011 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013}
7014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007015void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017}
7018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021}
7022
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024 addConfigurationProperty("touch.deviceType", "touchScreen");
7025 prepareDisplay(DISPLAY_ORIENTATION_0);
7026 prepareAxes(POSITION);
7027 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029
arthurhungdcef2dc2020-08-11 14:47:50 +08007030 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031
7032 NotifyMotionArgs motionArgs;
7033
7034 // Two fingers down at once.
7035 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7036 processPosition(mapper, x1, y1);
7037 processMTSync(mapper);
7038 processPosition(mapper, x2, y2);
7039 processMTSync(mapper);
7040 processSync(mapper);
7041
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7044 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7045 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7046 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7048 ASSERT_EQ(0, motionArgs.flags);
7049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7050 ASSERT_EQ(0, motionArgs.buttonState);
7051 ASSERT_EQ(0, motionArgs.edgeFlags);
7052 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7054 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7056 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7057 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7058 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7060
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7062 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7063 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7064 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7065 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007066 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 ASSERT_EQ(0, motionArgs.flags);
7068 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7069 ASSERT_EQ(0, motionArgs.buttonState);
7070 ASSERT_EQ(0, motionArgs.edgeFlags);
7071 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7072 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7074 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7077 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7079 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7080 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7081 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7083
7084 // Move.
7085 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7086 processPosition(mapper, x1, y1);
7087 processMTSync(mapper);
7088 processPosition(mapper, x2, y2);
7089 processMTSync(mapper);
7090 processSync(mapper);
7091
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7094 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7095 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7096 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7098 ASSERT_EQ(0, motionArgs.flags);
7099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7100 ASSERT_EQ(0, motionArgs.buttonState);
7101 ASSERT_EQ(0, motionArgs.edgeFlags);
7102 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7103 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7105 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7108 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7110 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7111 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7112 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7114
7115 // First finger up.
7116 x2 += 15; y2 -= 20;
7117 processPosition(mapper, x2, y2);
7118 processMTSync(mapper);
7119 processSync(mapper);
7120
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7123 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7124 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7125 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007126 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 ASSERT_EQ(0, motionArgs.flags);
7128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7129 ASSERT_EQ(0, motionArgs.buttonState);
7130 ASSERT_EQ(0, motionArgs.edgeFlags);
7131 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7134 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7135 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7137 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7140 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7141 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7143
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7146 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7147 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7148 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7150 ASSERT_EQ(0, motionArgs.flags);
7151 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7152 ASSERT_EQ(0, motionArgs.buttonState);
7153 ASSERT_EQ(0, motionArgs.edgeFlags);
7154 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7155 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7156 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7158 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7159 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7160 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7162
7163 // Move.
7164 x2 += 20; y2 -= 25;
7165 processPosition(mapper, x2, y2);
7166 processMTSync(mapper);
7167 processSync(mapper);
7168
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7171 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7173 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7175 ASSERT_EQ(0, motionArgs.flags);
7176 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7177 ASSERT_EQ(0, motionArgs.buttonState);
7178 ASSERT_EQ(0, motionArgs.edgeFlags);
7179 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7180 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7184 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7185 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7187
7188 // New finger down.
7189 int32_t x3 = 700, y3 = 300;
7190 processPosition(mapper, x2, y2);
7191 processMTSync(mapper);
7192 processPosition(mapper, x3, y3);
7193 processMTSync(mapper);
7194 processSync(mapper);
7195
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007201 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_EQ(0, motionArgs.flags);
7203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7204 ASSERT_EQ(0, motionArgs.buttonState);
7205 ASSERT_EQ(0, motionArgs.edgeFlags);
7206 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7209 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7212 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7214 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7215 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7216 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7218
7219 // Second finger up.
7220 x3 += 30; y3 -= 20;
7221 processPosition(mapper, x3, y3);
7222 processMTSync(mapper);
7223 processSync(mapper);
7224
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7227 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7228 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7229 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007230 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.flags);
7232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7233 ASSERT_EQ(0, motionArgs.buttonState);
7234 ASSERT_EQ(0, motionArgs.edgeFlags);
7235 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7238 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7241 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7243 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7245 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7247
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7250 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7252 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7254 ASSERT_EQ(0, motionArgs.flags);
7255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7256 ASSERT_EQ(0, motionArgs.buttonState);
7257 ASSERT_EQ(0, motionArgs.edgeFlags);
7258 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7262 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7266
7267 // Last finger up.
7268 processMTSync(mapper);
7269 processSync(mapper);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7273 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7274 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7275 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7277 ASSERT_EQ(0, motionArgs.flags);
7278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7279 ASSERT_EQ(0, motionArgs.buttonState);
7280 ASSERT_EQ(0, motionArgs.edgeFlags);
7281 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7282 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7286 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7287 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7289
7290 // Should not have sent any more keys or motions.
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7293}
7294
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007295TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7296 addConfigurationProperty("touch.deviceType", "touchScreen");
7297 prepareDisplay(DISPLAY_ORIENTATION_0);
7298
7299 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7300 /*fuzz*/ 0, /*resolution*/ 10);
7301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7302 /*fuzz*/ 0, /*resolution*/ 11);
7303 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7304 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7305 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7306 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7307 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7308 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7310 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7311
7312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7313
7314 // X and Y axes
7315 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7316 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7317 // Touch major and minor
7318 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7319 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7320 // Tool major and minor
7321 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7322 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7323}
7324
7325TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7326 addConfigurationProperty("touch.deviceType", "touchScreen");
7327 prepareDisplay(DISPLAY_ORIENTATION_0);
7328
7329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7330 /*fuzz*/ 0, /*resolution*/ 10);
7331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7332 /*fuzz*/ 0, /*resolution*/ 11);
7333
7334 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7335
7336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7337
7338 // Touch major and minor
7339 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7340 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7341 // Tool major and minor
7342 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7343 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7344}
7345
Michael Wrightd02c5b62014-02-10 15:10:22 -08007346TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007347 addConfigurationProperty("touch.deviceType", "touchScreen");
7348 prepareDisplay(DISPLAY_ORIENTATION_0);
7349 prepareAxes(POSITION | ID);
7350 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007351 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352
arthurhungdcef2dc2020-08-11 14:47:50 +08007353 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354
7355 NotifyMotionArgs motionArgs;
7356
7357 // Two fingers down at once.
7358 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7359 processPosition(mapper, x1, y1);
7360 processId(mapper, 1);
7361 processMTSync(mapper);
7362 processPosition(mapper, x2, y2);
7363 processId(mapper, 2);
7364 processMTSync(mapper);
7365 processSync(mapper);
7366
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7369 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7370 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7371 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7373 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007376 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007377 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7380 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7383 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7385 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7386
7387 // Move.
7388 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7389 processPosition(mapper, x1, y1);
7390 processId(mapper, 1);
7391 processMTSync(mapper);
7392 processPosition(mapper, x2, y2);
7393 processId(mapper, 2);
7394 processMTSync(mapper);
7395 processSync(mapper);
7396
7397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7399 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7402 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7405 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7408
7409 // First finger up.
7410 x2 += 15; y2 -= 20;
7411 processPosition(mapper, x2, y2);
7412 processId(mapper, 2);
7413 processMTSync(mapper);
7414 processSync(mapper);
7415
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007417 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7419 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7421 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7424 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7426 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7430 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7431 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7434 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7435
7436 // Move.
7437 x2 += 20; y2 -= 25;
7438 processPosition(mapper, x2, y2);
7439 processId(mapper, 2);
7440 processMTSync(mapper);
7441 processSync(mapper);
7442
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7445 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7446 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7450
7451 // New finger down.
7452 int32_t x3 = 700, y3 = 300;
7453 processPosition(mapper, x2, y2);
7454 processId(mapper, 2);
7455 processMTSync(mapper);
7456 processPosition(mapper, x3, y3);
7457 processId(mapper, 3);
7458 processMTSync(mapper);
7459 processSync(mapper);
7460
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007462 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007463 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7466 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7469 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7472
7473 // Second finger up.
7474 x3 += 30; y3 -= 20;
7475 processPosition(mapper, x3, y3);
7476 processId(mapper, 3);
7477 processMTSync(mapper);
7478 processSync(mapper);
7479
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007481 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7485 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7490 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7491
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7494 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7495 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7498 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7499
7500 // Last finger up.
7501 processMTSync(mapper);
7502 processSync(mapper);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7505 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7506 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7510 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7511
7512 // Should not have sent any more keys or motions.
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7515}
7516
7517TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 addConfigurationProperty("touch.deviceType", "touchScreen");
7519 prepareDisplay(DISPLAY_ORIENTATION_0);
7520 prepareAxes(POSITION | ID | SLOT);
7521 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007522 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523
arthurhungdcef2dc2020-08-11 14:47:50 +08007524 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525
7526 NotifyMotionArgs motionArgs;
7527
7528 // Two fingers down at once.
7529 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7530 processPosition(mapper, x1, y1);
7531 processId(mapper, 1);
7532 processSlot(mapper, 1);
7533 processPosition(mapper, x2, y2);
7534 processId(mapper, 2);
7535 processSync(mapper);
7536
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7538 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7543 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7544
7545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007546 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7548 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7550 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7555 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7556
7557 // Move.
7558 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7559 processSlot(mapper, 0);
7560 processPosition(mapper, x1, y1);
7561 processSlot(mapper, 1);
7562 processPosition(mapper, x2, y2);
7563 processSync(mapper);
7564
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7567 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7570 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7573 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7575 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7576
7577 // First finger up.
7578 x2 += 15; y2 -= 20;
7579 processSlot(mapper, 0);
7580 processId(mapper, -1);
7581 processSlot(mapper, 1);
7582 processPosition(mapper, x2, y2);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7599 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7600 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // Move.
7606 x2 += 20; y2 -= 25;
7607 processPosition(mapper, x2, y2);
7608 processSync(mapper);
7609
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7612 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7613 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7616 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7617
7618 // New finger down.
7619 int32_t x3 = 700, y3 = 300;
7620 processPosition(mapper, x2, y2);
7621 processSlot(mapper, 0);
7622 processId(mapper, 3);
7623 processPosition(mapper, x3, y3);
7624 processSync(mapper);
7625
7626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007627 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7631 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7637
7638 // Second finger up.
7639 x3 += 30; y3 -= 20;
7640 processSlot(mapper, 1);
7641 processId(mapper, -1);
7642 processSlot(mapper, 0);
7643 processPosition(mapper, x3, y3);
7644 processSync(mapper);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007647 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7649 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7651 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7656 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7657
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7660 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7661 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7663 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7664 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7665
7666 // Last finger up.
7667 processId(mapper, -1);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7672 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7674 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Should not have sent any more keys or motions.
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681}
7682
7683TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 addConfigurationProperty("touch.deviceType", "touchScreen");
7685 prepareDisplay(DISPLAY_ORIENTATION_0);
7686 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007687 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688
7689 // These calculations are based on the input device calibration documentation.
7690 int32_t rawX = 100;
7691 int32_t rawY = 200;
7692 int32_t rawTouchMajor = 7;
7693 int32_t rawTouchMinor = 6;
7694 int32_t rawToolMajor = 9;
7695 int32_t rawToolMinor = 8;
7696 int32_t rawPressure = 11;
7697 int32_t rawDistance = 0;
7698 int32_t rawOrientation = 3;
7699 int32_t id = 5;
7700
7701 float x = toDisplayX(rawX);
7702 float y = toDisplayY(rawY);
7703 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7704 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7705 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7706 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7707 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7708 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7709 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7710 float distance = float(rawDistance);
7711
7712 processPosition(mapper, rawX, rawY);
7713 processTouchMajor(mapper, rawTouchMajor);
7714 processTouchMinor(mapper, rawTouchMinor);
7715 processToolMajor(mapper, rawToolMajor);
7716 processToolMinor(mapper, rawToolMinor);
7717 processPressure(mapper, rawPressure);
7718 processOrientation(mapper, rawOrientation);
7719 processDistance(mapper, rawDistance);
7720 processId(mapper, id);
7721 processMTSync(mapper);
7722 processSync(mapper);
7723
7724 NotifyMotionArgs args;
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7726 ASSERT_EQ(0, args.pointerProperties[0].id);
7727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7728 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7729 orientation, distance));
7730}
7731
7732TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 addConfigurationProperty("touch.deviceType", "touchScreen");
7734 prepareDisplay(DISPLAY_ORIENTATION_0);
7735 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7736 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007737 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738
7739 // These calculations are based on the input device calibration documentation.
7740 int32_t rawX = 100;
7741 int32_t rawY = 200;
7742 int32_t rawTouchMajor = 140;
7743 int32_t rawTouchMinor = 120;
7744 int32_t rawToolMajor = 180;
7745 int32_t rawToolMinor = 160;
7746
7747 float x = toDisplayX(rawX);
7748 float y = toDisplayY(rawY);
7749 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7750 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7751 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7752 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7753 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7754
7755 processPosition(mapper, rawX, rawY);
7756 processTouchMajor(mapper, rawTouchMajor);
7757 processTouchMinor(mapper, rawTouchMinor);
7758 processToolMajor(mapper, rawToolMajor);
7759 processToolMinor(mapper, rawToolMinor);
7760 processMTSync(mapper);
7761 processSync(mapper);
7762
7763 NotifyMotionArgs args;
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7766 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7767}
7768
7769TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 addConfigurationProperty("touch.deviceType", "touchScreen");
7771 prepareDisplay(DISPLAY_ORIENTATION_0);
7772 prepareAxes(POSITION | TOUCH | TOOL);
7773 addConfigurationProperty("touch.size.calibration", "diameter");
7774 addConfigurationProperty("touch.size.scale", "10");
7775 addConfigurationProperty("touch.size.bias", "160");
7776 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007777 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778
7779 // These calculations are based on the input device calibration documentation.
7780 // Note: We only provide a single common touch/tool value because the device is assumed
7781 // not to emit separate values for each pointer (isSummed = 1).
7782 int32_t rawX = 100;
7783 int32_t rawY = 200;
7784 int32_t rawX2 = 150;
7785 int32_t rawY2 = 250;
7786 int32_t rawTouchMajor = 5;
7787 int32_t rawToolMajor = 8;
7788
7789 float x = toDisplayX(rawX);
7790 float y = toDisplayY(rawY);
7791 float x2 = toDisplayX(rawX2);
7792 float y2 = toDisplayY(rawY2);
7793 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7794 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7795 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7796
7797 processPosition(mapper, rawX, rawY);
7798 processTouchMajor(mapper, rawTouchMajor);
7799 processToolMajor(mapper, rawToolMajor);
7800 processMTSync(mapper);
7801 processPosition(mapper, rawX2, rawY2);
7802 processTouchMajor(mapper, rawTouchMajor);
7803 processToolMajor(mapper, rawToolMajor);
7804 processMTSync(mapper);
7805 processSync(mapper);
7806
7807 NotifyMotionArgs args;
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007812 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_EQ(size_t(2), args.pointerCount);
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7815 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7817 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7818}
7819
7820TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821 addConfigurationProperty("touch.deviceType", "touchScreen");
7822 prepareDisplay(DISPLAY_ORIENTATION_0);
7823 prepareAxes(POSITION | TOUCH | TOOL);
7824 addConfigurationProperty("touch.size.calibration", "area");
7825 addConfigurationProperty("touch.size.scale", "43");
7826 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828
7829 // These calculations are based on the input device calibration documentation.
7830 int32_t rawX = 100;
7831 int32_t rawY = 200;
7832 int32_t rawTouchMajor = 5;
7833 int32_t rawToolMajor = 8;
7834
7835 float x = toDisplayX(rawX);
7836 float y = toDisplayY(rawY);
7837 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7838 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7839 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7840
7841 processPosition(mapper, rawX, rawY);
7842 processTouchMajor(mapper, rawTouchMajor);
7843 processToolMajor(mapper, rawToolMajor);
7844 processMTSync(mapper);
7845 processSync(mapper);
7846
7847 NotifyMotionArgs args;
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7850 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7851}
7852
7853TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 addConfigurationProperty("touch.deviceType", "touchScreen");
7855 prepareDisplay(DISPLAY_ORIENTATION_0);
7856 prepareAxes(POSITION | PRESSURE);
7857 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7858 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860
Michael Wrightaa449c92017-12-13 21:21:43 +00007861 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007862 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007863 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7864 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7865 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7866
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867 // These calculations are based on the input device calibration documentation.
7868 int32_t rawX = 100;
7869 int32_t rawY = 200;
7870 int32_t rawPressure = 60;
7871
7872 float x = toDisplayX(rawX);
7873 float y = toDisplayY(rawY);
7874 float pressure = float(rawPressure) * 0.01f;
7875
7876 processPosition(mapper, rawX, rawY);
7877 processPressure(mapper, rawPressure);
7878 processMTSync(mapper);
7879 processSync(mapper);
7880
7881 NotifyMotionArgs args;
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7884 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7885}
7886
7887TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007888 addConfigurationProperty("touch.deviceType", "touchScreen");
7889 prepareDisplay(DISPLAY_ORIENTATION_0);
7890 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892
7893 NotifyMotionArgs motionArgs;
7894 NotifyKeyArgs keyArgs;
7895
7896 processId(mapper, 1);
7897 processPosition(mapper, 100, 200);
7898 processSync(mapper);
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7901 ASSERT_EQ(0, motionArgs.buttonState);
7902
7903 // press BTN_LEFT, release BTN_LEFT
7904 processKey(mapper, BTN_LEFT, 1);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7912 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7913
Michael Wrightd02c5b62014-02-10 15:10:22 -08007914 processKey(mapper, BTN_LEFT, 0);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007919
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007922 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923
7924 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7925 processKey(mapper, BTN_RIGHT, 1);
7926 processKey(mapper, BTN_MIDDLE, 1);
7927 processSync(mapper);
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7930 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7931 motionArgs.buttonState);
7932
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7939 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7940 motionArgs.buttonState);
7941
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 processKey(mapper, BTN_RIGHT, 0);
7943 processSync(mapper);
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947
7948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007950 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951
7952 processKey(mapper, BTN_MIDDLE, 0);
7953 processSync(mapper);
7954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007957
7958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007960 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961
7962 // press BTN_BACK, release BTN_BACK
7963 processKey(mapper, BTN_BACK, 1);
7964 processSync(mapper);
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7966 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7967 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007968
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7975 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976
7977 processKey(mapper, BTN_BACK, 0);
7978 processSync(mapper);
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007985 ASSERT_EQ(0, motionArgs.buttonState);
7986
Michael Wrightd02c5b62014-02-10 15:10:22 -08007987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7989 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7990
7991 // press BTN_SIDE, release BTN_SIDE
7992 processKey(mapper, BTN_SIDE, 1);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7995 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7996 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007997
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008000 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8001
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8004 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005
8006 processKey(mapper, BTN_SIDE, 0);
8007 processSync(mapper);
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008011
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008014 ASSERT_EQ(0, motionArgs.buttonState);
8015
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8017 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8018 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8019
8020 // press BTN_FORWARD, release BTN_FORWARD
8021 processKey(mapper, BTN_FORWARD, 1);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8024 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8025 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8030
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8033 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034
8035 processKey(mapper, BTN_FORWARD, 0);
8036 processSync(mapper);
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043 ASSERT_EQ(0, motionArgs.buttonState);
8044
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8046 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8047 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8048
8049 // press BTN_EXTRA, release BTN_EXTRA
8050 processKey(mapper, BTN_EXTRA, 1);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8053 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8054 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008055
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8059
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8062 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063
8064 processKey(mapper, BTN_EXTRA, 0);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008072 ASSERT_EQ(0, motionArgs.buttonState);
8073
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8075 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8076 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8077
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8079
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 // press BTN_STYLUS, release BTN_STYLUS
8081 processKey(mapper, BTN_STYLUS, 1);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8089 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090
8091 processKey(mapper, BTN_STYLUS, 0);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 // press BTN_STYLUS2, release BTN_STYLUS2
8102 processKey(mapper, BTN_STYLUS2, 1);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111
8112 processKey(mapper, BTN_STYLUS2, 0);
8113 processSync(mapper);
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008120 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121
8122 // release touch
8123 processId(mapper, -1);
8124 processSync(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8127 ASSERT_EQ(0, motionArgs.buttonState);
8128}
8129
8130TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131 addConfigurationProperty("touch.deviceType", "touchScreen");
8132 prepareDisplay(DISPLAY_ORIENTATION_0);
8133 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
8136 NotifyMotionArgs motionArgs;
8137
8138 // default tool type is finger
8139 processId(mapper, 1);
8140 processPosition(mapper, 100, 200);
8141 processSync(mapper);
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8145
8146 // eraser
8147 processKey(mapper, BTN_TOOL_RUBBER, 1);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8152
8153 // stylus
8154 processKey(mapper, BTN_TOOL_RUBBER, 0);
8155 processKey(mapper, BTN_TOOL_PEN, 1);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8160
8161 // brush
8162 processKey(mapper, BTN_TOOL_PEN, 0);
8163 processKey(mapper, BTN_TOOL_BRUSH, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8168
8169 // pencil
8170 processKey(mapper, BTN_TOOL_BRUSH, 0);
8171 processKey(mapper, BTN_TOOL_PENCIL, 1);
8172 processSync(mapper);
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8176
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008177 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 processKey(mapper, BTN_TOOL_PENCIL, 0);
8179 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8184
8185 // mouse
8186 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8187 processKey(mapper, BTN_TOOL_MOUSE, 1);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8192
8193 // lens
8194 processKey(mapper, BTN_TOOL_MOUSE, 0);
8195 processKey(mapper, BTN_TOOL_LENS, 1);
8196 processSync(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8200
8201 // double-tap
8202 processKey(mapper, BTN_TOOL_LENS, 0);
8203 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8204 processSync(mapper);
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8208
8209 // triple-tap
8210 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8211 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8216
8217 // quad-tap
8218 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8219 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8224
8225 // finger
8226 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8227 processKey(mapper, BTN_TOOL_FINGER, 1);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8232
8233 // stylus trumps finger
8234 processKey(mapper, BTN_TOOL_PEN, 1);
8235 processSync(mapper);
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8239
8240 // eraser trumps stylus
8241 processKey(mapper, BTN_TOOL_RUBBER, 1);
8242 processSync(mapper);
8243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8246
8247 // mouse trumps eraser
8248 processKey(mapper, BTN_TOOL_MOUSE, 1);
8249 processSync(mapper);
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8253
8254 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8255 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8260
8261 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8262 processToolType(mapper, MT_TOOL_PEN);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8267
8268 // back to default tool type
8269 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8270 processKey(mapper, BTN_TOOL_MOUSE, 0);
8271 processKey(mapper, BTN_TOOL_RUBBER, 0);
8272 processKey(mapper, BTN_TOOL_PEN, 0);
8273 processKey(mapper, BTN_TOOL_FINGER, 0);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8278}
8279
8280TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 addConfigurationProperty("touch.deviceType", "touchScreen");
8282 prepareDisplay(DISPLAY_ORIENTATION_0);
8283 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008284 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286
8287 NotifyMotionArgs motionArgs;
8288
8289 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8290 processId(mapper, 1);
8291 processPosition(mapper, 100, 200);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8294 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8296 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8301 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8302
8303 // move a little
8304 processPosition(mapper, 150, 250);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8309 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8310
8311 // down when BTN_TOUCH is pressed, pressure defaults to 1
8312 processKey(mapper, BTN_TOUCH, 1);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8318
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8320 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8322 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8323
8324 // up when BTN_TOUCH is released, hover restored
8325 processKey(mapper, BTN_TOUCH, 0);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8330 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8331
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8341
8342 // exit hover when pointer goes away
8343 processId(mapper, -1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8349}
8350
8351TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 addConfigurationProperty("touch.deviceType", "touchScreen");
8353 prepareDisplay(DISPLAY_ORIENTATION_0);
8354 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008355 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356
8357 NotifyMotionArgs motionArgs;
8358
8359 // initially hovering because pressure is 0
8360 processId(mapper, 1);
8361 processPosition(mapper, 100, 200);
8362 processPressure(mapper, 0);
8363 processSync(mapper);
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8367 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8368
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8373
8374 // move a little
8375 processPosition(mapper, 150, 250);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8381
8382 // down when pressure becomes non-zero
8383 processPressure(mapper, RAW_PRESSURE_MAX);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8388 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8389
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8393 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8394
8395 // up when pressure becomes 0, hover restored
8396 processPressure(mapper, 0);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8399 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8401 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8402
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8406 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8407
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8411 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8412
8413 // exit hover when pointer goes away
8414 processId(mapper, -1);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8419 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8420}
8421
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008422/**
8423 * Set the input device port <--> display port associations, and check that the
8424 * events are routed to the display that matches the display port.
8425 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8426 */
8427TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008428 const std::string usb2 = "USB2";
8429 const uint8_t hdmi1 = 0;
8430 const uint8_t hdmi2 = 1;
8431 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008432 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008433
8434 addConfigurationProperty("touch.deviceType", "touchScreen");
8435 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008437
8438 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8439 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8440
8441 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8442 // for this input device is specified, and the matching viewport is not present,
8443 // the input device should be disabled (at the mapper level).
8444
8445 // Add viewport for display 2 on hdmi2
8446 prepareSecondaryDisplay(type, hdmi2);
8447 // Send a touch event
8448 processPosition(mapper, 100, 100);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8451
8452 // Add viewport for display 1 on hdmi1
8453 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8454 // Send a touch event again
8455 processPosition(mapper, 100, 100);
8456 processSync(mapper);
8457
8458 NotifyMotionArgs args;
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8460 ASSERT_EQ(DISPLAY_ID, args.displayId);
8461}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008463TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8464 addConfigurationProperty("touch.deviceType", "touchScreen");
8465 prepareAxes(POSITION);
8466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8467
8468 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8469
8470 prepareDisplay(DISPLAY_ORIENTATION_0);
8471 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8472
8473 // Send a touch event
8474 processPosition(mapper, 100, 100);
8475 processSync(mapper);
8476
8477 NotifyMotionArgs args;
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8479 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8480}
8481
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008482TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008483 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008484 std::shared_ptr<FakePointerController> fakePointerController =
8485 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008486 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008487 fakePointerController->setPosition(100, 200);
8488 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008489 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008490
Garfield Tan888a6a42020-01-09 11:39:16 -08008491 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008492 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008493
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008494 prepareDisplay(DISPLAY_ORIENTATION_0);
8495 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008497
8498 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008499 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008500
8501 NotifyMotionArgs motionArgs;
8502 processPosition(mapper, 100, 100);
8503 processSync(mapper);
8504
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8507 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8508}
8509
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008510/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008511 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8512 */
8513TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8514 addConfigurationProperty("touch.deviceType", "touchScreen");
8515 prepareAxes(POSITION);
8516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8517
8518 prepareDisplay(DISPLAY_ORIENTATION_0);
8519 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8520 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8521 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8522 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8523
8524 NotifyMotionArgs args;
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8526 ASSERT_EQ(26, args.readTime);
8527
8528 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8529 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8530 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8533 ASSERT_EQ(33, args.readTime);
8534}
8535
8536/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008537 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8538 * events should not be delivered to the listener.
8539 */
8540TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8541 addConfigurationProperty("touch.deviceType", "touchScreen");
8542 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8543 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8544 ViewportType::INTERNAL);
8545 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8546 prepareAxes(POSITION);
8547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8548
8549 NotifyMotionArgs motionArgs;
8550 processPosition(mapper, 100, 100);
8551 processSync(mapper);
8552
8553 mFakeListener->assertNotifyMotionWasNotCalled();
8554}
8555
Garfield Tanc734e4f2021-01-15 20:01:39 -08008556TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8557 addConfigurationProperty("touch.deviceType", "touchScreen");
8558 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8559 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8560 ViewportType::INTERNAL);
8561 std::optional<DisplayViewport> optionalDisplayViewport =
8562 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8563 ASSERT_TRUE(optionalDisplayViewport.has_value());
8564 DisplayViewport displayViewport = *optionalDisplayViewport;
8565
8566 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8567 prepareAxes(POSITION);
8568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8569
8570 // Finger down
8571 int32_t x = 100, y = 100;
8572 processPosition(mapper, x, y);
8573 processSync(mapper);
8574
8575 NotifyMotionArgs motionArgs;
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8578
8579 // Deactivate display viewport
8580 displayViewport.isActive = false;
8581 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8582 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8583
8584 // Finger move
8585 x += 10, y += 10;
8586 processPosition(mapper, x, y);
8587 processSync(mapper);
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8591
8592 // Reactivate display viewport
8593 displayViewport.isActive = true;
8594 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8595 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8596
8597 // Finger move again
8598 x += 10, y += 10;
8599 processPosition(mapper, x, y);
8600 processSync(mapper);
8601
8602 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8603 // no pointer on the touch device.
8604 mFakeListener->assertNotifyMotionWasNotCalled();
8605}
8606
Arthur Hung7c645402019-01-25 17:45:42 +08008607TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8608 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008609 prepareAxes(POSITION | ID | SLOT);
8610 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008612
8613 // Create the second touch screen device, and enable multi fingers.
8614 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008615 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008616 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008617 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008618 std::shared_ptr<InputDevice> device2 =
8619 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008620 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008621
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008622 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8623 0 /*flat*/, 0 /*fuzz*/);
8624 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8625 0 /*flat*/, 0 /*fuzz*/);
8626 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8627 0 /*flat*/, 0 /*fuzz*/);
8628 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8629 0 /*flat*/, 0 /*fuzz*/);
8630 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8631 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8632 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008633
8634 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008635 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008636 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8637 device2->reset(ARBITRARY_TIME);
8638
8639 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008640 std::shared_ptr<FakePointerController> fakePointerController =
8641 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008642 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008643
8644 // Setup policy for associated displays and show touches.
8645 const uint8_t hdmi1 = 0;
8646 const uint8_t hdmi2 = 1;
8647 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8648 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8649 mFakePolicy->setShowTouches(true);
8650
8651 // Create displays.
8652 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008653 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008654
8655 // Default device will reconfigure above, need additional reconfiguration for another device.
8656 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008657 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8658 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008659
8660 // Two fingers down at default display.
8661 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8662 processPosition(mapper, x1, y1);
8663 processId(mapper, 1);
8664 processSlot(mapper, 1);
8665 processPosition(mapper, x2, y2);
8666 processId(mapper, 2);
8667 processSync(mapper);
8668
8669 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8670 fakePointerController->getSpots().find(DISPLAY_ID);
8671 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8672 ASSERT_EQ(size_t(2), iter->second.size());
8673
8674 // Two fingers down at second display.
8675 processPosition(mapper2, x1, y1);
8676 processId(mapper2, 1);
8677 processSlot(mapper2, 1);
8678 processPosition(mapper2, x2, y2);
8679 processId(mapper2, 2);
8680 processSync(mapper2);
8681
8682 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8683 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8684 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008685
8686 // Disable the show touches configuration and ensure the spots are cleared.
8687 mFakePolicy->setShowTouches(false);
8688 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8689 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8690
8691 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008692}
8693
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008694TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008695 prepareAxes(POSITION);
8696 addConfigurationProperty("touch.deviceType", "touchScreen");
8697 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008698 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008699
8700 NotifyMotionArgs motionArgs;
8701 // Unrotated video frame
8702 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8703 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008704 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008705 processPosition(mapper, 100, 200);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(frames, motionArgs.videoFrames);
8709
8710 // Subsequent touch events should not have any videoframes
8711 // This is implemented separately in FakeEventHub,
8712 // but that should match the behaviour of TouchVideoDevice.
8713 processPosition(mapper, 200, 200);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8717}
8718
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008719TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008720 prepareAxes(POSITION);
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008723 // Unrotated video frame
8724 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8725 NotifyMotionArgs motionArgs;
8726
8727 // Test all 4 orientations
8728 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008729 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8730 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8731 clearViewports();
8732 prepareDisplay(orientation);
8733 std::vector<TouchVideoFrame> frames{frame};
8734 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8735 processPosition(mapper, 100, 200);
8736 processSync(mapper);
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8738 ASSERT_EQ(frames, motionArgs.videoFrames);
8739 }
8740}
8741
8742TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8743 prepareAxes(POSITION);
8744 addConfigurationProperty("touch.deviceType", "touchScreen");
8745 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8746 // orientation-aware are affected by display rotation.
8747 addConfigurationProperty("touch.orientationAware", "0");
8748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8749 // Unrotated video frame
8750 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8751 NotifyMotionArgs motionArgs;
8752
8753 // Test all 4 orientations
8754 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008755 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8756 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8757 clearViewports();
8758 prepareDisplay(orientation);
8759 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008760 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008761 processPosition(mapper, 100, 200);
8762 processSync(mapper);
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008764 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8765 // compared to the display. This is so that when the window transform (which contains the
8766 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8767 // window's coordinate space.
8768 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008769 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008770
8771 // Release finger.
8772 processSync(mapper);
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008774 }
8775}
8776
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008777TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008778 prepareAxes(POSITION);
8779 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008781 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8782 // so mix these.
8783 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8784 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8785 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8786 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8787 NotifyMotionArgs motionArgs;
8788
8789 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008790 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008791 processPosition(mapper, 100, 200);
8792 processSync(mapper);
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008794 ASSERT_EQ(frames, motionArgs.videoFrames);
8795}
8796
8797TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8798 prepareAxes(POSITION);
8799 addConfigurationProperty("touch.deviceType", "touchScreen");
8800 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8801 // orientation-aware are affected by display rotation.
8802 addConfigurationProperty("touch.orientationAware", "0");
8803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8804 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8805 // so mix these.
8806 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8807 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8808 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8809 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8810 NotifyMotionArgs motionArgs;
8811
8812 prepareDisplay(DISPLAY_ORIENTATION_90);
8813 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8814 processPosition(mapper, 100, 200);
8815 processSync(mapper);
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8817 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8818 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8819 // compared to the display. This is so that when the window transform (which contains the
8820 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8821 // window's coordinate space.
8822 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8823 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008824 ASSERT_EQ(frames, motionArgs.videoFrames);
8825}
8826
Arthur Hung9da14732019-09-02 16:16:58 +08008827/**
8828 * If we had defined port associations, but the viewport is not ready, the touch device would be
8829 * expected to be disabled, and it should be enabled after the viewport has found.
8830 */
8831TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008832 constexpr uint8_t hdmi2 = 1;
8833 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008834 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008835
8836 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8837
8838 addConfigurationProperty("touch.deviceType", "touchScreen");
8839 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008841
8842 ASSERT_EQ(mDevice->isEnabled(), false);
8843
8844 // Add display on hdmi2, the device should be enabled and can receive touch event.
8845 prepareSecondaryDisplay(type, hdmi2);
8846 ASSERT_EQ(mDevice->isEnabled(), true);
8847
8848 // Send a touch event.
8849 processPosition(mapper, 100, 100);
8850 processSync(mapper);
8851
8852 NotifyMotionArgs args;
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8854 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8855}
8856
Arthur Hung421eb1c2020-01-16 00:09:42 +08008857TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008858 addConfigurationProperty("touch.deviceType", "touchScreen");
8859 prepareDisplay(DISPLAY_ORIENTATION_0);
8860 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008861 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008862
8863 NotifyMotionArgs motionArgs;
8864
8865 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8866 // finger down
8867 processId(mapper, 1);
8868 processPosition(mapper, x1, y1);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8873
8874 // finger move
8875 processId(mapper, 1);
8876 processPosition(mapper, x2, y2);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8881
8882 // finger up.
8883 processId(mapper, -1);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8888
8889 // new finger down
8890 processId(mapper, 1);
8891 processPosition(mapper, x3, y3);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8896}
8897
8898/**
arthurhungcc7f9802020-04-30 17:55:40 +08008899 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8900 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008901 */
arthurhungcc7f9802020-04-30 17:55:40 +08008902TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008903 addConfigurationProperty("touch.deviceType", "touchScreen");
8904 prepareDisplay(DISPLAY_ORIENTATION_0);
8905 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008907
8908 NotifyMotionArgs motionArgs;
8909
8910 // default tool type is finger
8911 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008912 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008913 processPosition(mapper, x1, y1);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8918
8919 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8920 processToolType(mapper, MT_TOOL_PALM);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8923 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8924
8925 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008926 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008927 processPosition(mapper, x2, y2);
8928 processSync(mapper);
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8930
8931 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008932 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8935
8936 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008937 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008938 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008939 processPosition(mapper, x3, y3);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8944}
8945
arthurhungbf89a482020-04-17 17:37:55 +08008946/**
arthurhungcc7f9802020-04-30 17:55:40 +08008947 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8948 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008949 */
arthurhungcc7f9802020-04-30 17:55:40 +08008950TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008951 addConfigurationProperty("touch.deviceType", "touchScreen");
8952 prepareDisplay(DISPLAY_ORIENTATION_0);
8953 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8954 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8955
8956 NotifyMotionArgs motionArgs;
8957
8958 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008959 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8960 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008961 processPosition(mapper, x1, y1);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8966
8967 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008968 processSlot(mapper, SECOND_SLOT);
8969 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008970 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008971 processSync(mapper);
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008973 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8975
8976 // If the tool type of the first finger changes to MT_TOOL_PALM,
8977 // we expect to receive ACTION_POINTER_UP with cancel flag.
8978 processSlot(mapper, FIRST_SLOT);
8979 processId(mapper, FIRST_TRACKING_ID);
8980 processToolType(mapper, MT_TOOL_PALM);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008983 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008984 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8985
8986 // The following MOVE events of second finger should be processed.
8987 processSlot(mapper, SECOND_SLOT);
8988 processId(mapper, SECOND_TRACKING_ID);
8989 processPosition(mapper, x2 + 1, y2 + 1);
8990 processSync(mapper);
8991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8993 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8994
8995 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8996 // it. Second finger receive move.
8997 processSlot(mapper, FIRST_SLOT);
8998 processId(mapper, INVALID_TRACKING_ID);
8999 processSync(mapper);
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9002 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9003
9004 // Second finger keeps moving.
9005 processSlot(mapper, SECOND_SLOT);
9006 processId(mapper, SECOND_TRACKING_ID);
9007 processPosition(mapper, x2 + 2, y2 + 2);
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9011 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9012
9013 // Second finger up.
9014 processId(mapper, INVALID_TRACKING_ID);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9018 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9019}
9020
9021/**
9022 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9023 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9024 */
9025TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9026 addConfigurationProperty("touch.deviceType", "touchScreen");
9027 prepareDisplay(DISPLAY_ORIENTATION_0);
9028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9029 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9030
9031 NotifyMotionArgs motionArgs;
9032
9033 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9034 // First finger down.
9035 processId(mapper, FIRST_TRACKING_ID);
9036 processPosition(mapper, x1, y1);
9037 processSync(mapper);
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9041
9042 // Second finger down.
9043 processSlot(mapper, SECOND_SLOT);
9044 processId(mapper, SECOND_TRACKING_ID);
9045 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009048 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9050
arthurhungcc7f9802020-04-30 17:55:40 +08009051 // If the tool type of the first finger changes to MT_TOOL_PALM,
9052 // we expect to receive ACTION_POINTER_UP with cancel flag.
9053 processSlot(mapper, FIRST_SLOT);
9054 processId(mapper, FIRST_TRACKING_ID);
9055 processToolType(mapper, MT_TOOL_PALM);
9056 processSync(mapper);
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009058 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009059 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9060
9061 // Second finger keeps moving.
9062 processSlot(mapper, SECOND_SLOT);
9063 processId(mapper, SECOND_TRACKING_ID);
9064 processPosition(mapper, x2 + 1, y2 + 1);
9065 processSync(mapper);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9068
9069 // second finger becomes palm, receive cancel due to only 1 finger is active.
9070 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009071 processToolType(mapper, MT_TOOL_PALM);
9072 processSync(mapper);
9073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9074 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9075
arthurhungcc7f9802020-04-30 17:55:40 +08009076 // third finger down.
9077 processSlot(mapper, THIRD_SLOT);
9078 processId(mapper, THIRD_TRACKING_ID);
9079 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009080 processPosition(mapper, x3, y3);
9081 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009085 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9086
9087 // third finger move
9088 processId(mapper, THIRD_TRACKING_ID);
9089 processPosition(mapper, x3 + 1, y3 + 1);
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9093
9094 // first finger up, third finger receive move.
9095 processSlot(mapper, FIRST_SLOT);
9096 processId(mapper, INVALID_TRACKING_ID);
9097 processSync(mapper);
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9100 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9101
9102 // second finger up, third finger receive move.
9103 processSlot(mapper, SECOND_SLOT);
9104 processId(mapper, INVALID_TRACKING_ID);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9108 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9109
9110 // third finger up.
9111 processSlot(mapper, THIRD_SLOT);
9112 processId(mapper, INVALID_TRACKING_ID);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9116 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9117}
9118
9119/**
9120 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9121 * and the active finger could still be allowed to receive the events
9122 */
9123TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9124 addConfigurationProperty("touch.deviceType", "touchScreen");
9125 prepareDisplay(DISPLAY_ORIENTATION_0);
9126 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9127 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9128
9129 NotifyMotionArgs motionArgs;
9130
9131 // default tool type is finger
9132 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9133 processId(mapper, FIRST_TRACKING_ID);
9134 processPosition(mapper, x1, y1);
9135 processSync(mapper);
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9138 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9139
9140 // Second finger down.
9141 processSlot(mapper, SECOND_SLOT);
9142 processId(mapper, SECOND_TRACKING_ID);
9143 processPosition(mapper, x2, y2);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009146 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9148
9149 // If the tool type of the second finger changes to MT_TOOL_PALM,
9150 // we expect to receive ACTION_POINTER_UP with cancel flag.
9151 processId(mapper, SECOND_TRACKING_ID);
9152 processToolType(mapper, MT_TOOL_PALM);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009155 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009156 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9157
9158 // The following MOVE event should be processed.
9159 processSlot(mapper, FIRST_SLOT);
9160 processId(mapper, FIRST_TRACKING_ID);
9161 processPosition(mapper, x1 + 1, y1 + 1);
9162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9165 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9166
9167 // second finger up.
9168 processSlot(mapper, SECOND_SLOT);
9169 processId(mapper, INVALID_TRACKING_ID);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9173
9174 // first finger keep moving
9175 processSlot(mapper, FIRST_SLOT);
9176 processId(mapper, FIRST_TRACKING_ID);
9177 processPosition(mapper, x1 + 2, y1 + 2);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9181
9182 // first finger up.
9183 processId(mapper, INVALID_TRACKING_ID);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9187 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009188}
9189
Arthur Hung9ad18942021-06-19 02:04:46 +00009190/**
9191 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9192 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9193 * cause slot be valid again.
9194 */
9195TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9196 addConfigurationProperty("touch.deviceType", "touchScreen");
9197 prepareDisplay(DISPLAY_ORIENTATION_0);
9198 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9199 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9200
9201 NotifyMotionArgs motionArgs;
9202
9203 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9204 // First finger down.
9205 processId(mapper, FIRST_TRACKING_ID);
9206 processPosition(mapper, x1, y1);
9207 processPressure(mapper, RAW_PRESSURE_MAX);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9211 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9212
9213 // First finger move.
9214 processId(mapper, FIRST_TRACKING_ID);
9215 processPosition(mapper, x1 + 1, y1 + 1);
9216 processPressure(mapper, RAW_PRESSURE_MAX);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9220 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9221
9222 // Second finger down.
9223 processSlot(mapper, SECOND_SLOT);
9224 processId(mapper, SECOND_TRACKING_ID);
9225 processPosition(mapper, x2, y2);
9226 processPressure(mapper, RAW_PRESSURE_MAX);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009229 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009230 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9231
9232 // second finger up with some unexpected data.
9233 processSlot(mapper, SECOND_SLOT);
9234 processId(mapper, INVALID_TRACKING_ID);
9235 processPosition(mapper, x2, y2);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009238 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009239 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9240
9241 // first finger up with some unexpected data.
9242 processSlot(mapper, FIRST_SLOT);
9243 processId(mapper, INVALID_TRACKING_ID);
9244 processPosition(mapper, x2, y2);
9245 processPressure(mapper, RAW_PRESSURE_MAX);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9249 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9250}
9251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009252// --- MultiTouchInputMapperTest_ExternalDevice ---
9253
9254class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9255protected:
Chris Yea52ade12020-08-27 16:49:20 -07009256 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009257};
9258
9259/**
9260 * Expect fallback to internal viewport if device is external and external viewport is not present.
9261 */
9262TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9263 prepareAxes(POSITION);
9264 addConfigurationProperty("touch.deviceType", "touchScreen");
9265 prepareDisplay(DISPLAY_ORIENTATION_0);
9266 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9267
9268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9269
9270 NotifyMotionArgs motionArgs;
9271
9272 // Expect the event to be sent to the internal viewport,
9273 // because an external viewport is not present.
9274 processPosition(mapper, 100, 100);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9278
9279 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009280 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009281 processPosition(mapper, 100, 100);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9285}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009286
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009287TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9288 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9289 std::shared_ptr<FakePointerController> fakePointerController =
9290 std::make_shared<FakePointerController>();
9291 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9292 fakePointerController->setPosition(0, 0);
9293 fakePointerController->setButtonState(0);
9294
9295 // prepare device and capture
9296 prepareDisplay(DISPLAY_ORIENTATION_0);
9297 prepareAxes(POSITION | ID | SLOT);
9298 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9299 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9300 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009301 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009302 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9303
9304 // captured touchpad should be a touchpad source
9305 NotifyDeviceResetArgs resetArgs;
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9307 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9308
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009309 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009310
9311 const InputDeviceInfo::MotionRange* relRangeX =
9312 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9313 ASSERT_NE(relRangeX, nullptr);
9314 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9315 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9316 const InputDeviceInfo::MotionRange* relRangeY =
9317 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9318 ASSERT_NE(relRangeY, nullptr);
9319 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9320 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9321
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009322 // run captured pointer tests - note that this is unscaled, so input listener events should be
9323 // identical to what the hardware sends (accounting for any
9324 // calibration).
9325 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009326 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009327 processId(mapper, 1);
9328 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9329 processKey(mapper, BTN_TOUCH, 1);
9330 processSync(mapper);
9331
9332 // expect coord[0] to contain initial location of touch 0
9333 NotifyMotionArgs args;
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9336 ASSERT_EQ(1U, args.pointerCount);
9337 ASSERT_EQ(0, args.pointerProperties[0].id);
9338 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9339 ASSERT_NO_FATAL_FAILURE(
9340 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9341
9342 // FINGER 1 DOWN
9343 processSlot(mapper, 1);
9344 processId(mapper, 2);
9345 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9346 processSync(mapper);
9347
9348 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009350 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009351 ASSERT_EQ(2U, args.pointerCount);
9352 ASSERT_EQ(0, args.pointerProperties[0].id);
9353 ASSERT_EQ(1, args.pointerProperties[1].id);
9354 ASSERT_NO_FATAL_FAILURE(
9355 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9356 ASSERT_NO_FATAL_FAILURE(
9357 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9358
9359 // FINGER 1 MOVE
9360 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9361 processSync(mapper);
9362
9363 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9364 // from move
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9367 ASSERT_NO_FATAL_FAILURE(
9368 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9369 ASSERT_NO_FATAL_FAILURE(
9370 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9371
9372 // FINGER 0 MOVE
9373 processSlot(mapper, 0);
9374 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9375 processSync(mapper);
9376
9377 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9380 ASSERT_NO_FATAL_FAILURE(
9381 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9382 ASSERT_NO_FATAL_FAILURE(
9383 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9384
9385 // BUTTON DOWN
9386 processKey(mapper, BTN_LEFT, 1);
9387 processSync(mapper);
9388
9389 // touchinputmapper design sends a move before button press
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9394
9395 // BUTTON UP
9396 processKey(mapper, BTN_LEFT, 0);
9397 processSync(mapper);
9398
9399 // touchinputmapper design sends a move after button release
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9404
9405 // FINGER 0 UP
9406 processId(mapper, -1);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9410
9411 // FINGER 1 MOVE
9412 processSlot(mapper, 1);
9413 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9414 processSync(mapper);
9415
9416 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9419 ASSERT_EQ(1U, args.pointerCount);
9420 ASSERT_EQ(1, args.pointerProperties[0].id);
9421 ASSERT_NO_FATAL_FAILURE(
9422 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9423
9424 // FINGER 1 UP
9425 processId(mapper, -1);
9426 processKey(mapper, BTN_TOUCH, 0);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9430
9431 // non captured touchpad should be a mouse source
9432 mFakePolicy->setPointerCapture(false);
9433 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9435 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9436}
9437
9438TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9439 std::shared_ptr<FakePointerController> fakePointerController =
9440 std::make_shared<FakePointerController>();
9441 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9442 fakePointerController->setPosition(0, 0);
9443 fakePointerController->setButtonState(0);
9444
9445 // prepare device and capture
9446 prepareDisplay(DISPLAY_ORIENTATION_0);
9447 prepareAxes(POSITION | ID | SLOT);
9448 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9449 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009450 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009451 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9452 // run uncaptured pointer tests - pushes out generic events
9453 // FINGER 0 DOWN
9454 processId(mapper, 3);
9455 processPosition(mapper, 100, 100);
9456 processKey(mapper, BTN_TOUCH, 1);
9457 processSync(mapper);
9458
9459 // start at (100,100), cursor should be at (0,0) * scale
9460 NotifyMotionArgs args;
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9463 ASSERT_NO_FATAL_FAILURE(
9464 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9465
9466 // FINGER 0 MOVE
9467 processPosition(mapper, 200, 200);
9468 processSync(mapper);
9469
9470 // compute scaling to help with touch position checking
9471 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9472 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9473 float scale =
9474 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9475
9476 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9480 0, 0, 0, 0, 0, 0, 0));
9481}
9482
9483TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9484 std::shared_ptr<FakePointerController> fakePointerController =
9485 std::make_shared<FakePointerController>();
9486
9487 prepareDisplay(DISPLAY_ORIENTATION_0);
9488 prepareAxes(POSITION | ID | SLOT);
9489 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009490 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009491 mFakePolicy->setPointerCapture(false);
9492 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9493
9494 // uncaptured touchpad should be a pointer device
9495 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9496
9497 // captured touchpad should be a touchpad device
9498 mFakePolicy->setPointerCapture(true);
9499 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9500 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9501}
9502
HQ Liue6983c72022-04-19 22:14:56 +00009503class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9504protected:
9505 float mPointerMovementScale;
9506 float mPointerXZoomScale;
9507 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9508 addConfigurationProperty("touch.deviceType", "pointer");
9509 std::shared_ptr<FakePointerController> fakePointerController =
9510 std::make_shared<FakePointerController>();
9511 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9512 fakePointerController->setPosition(0, 0);
9513 fakePointerController->setButtonState(0);
9514 prepareDisplay(DISPLAY_ORIENTATION_0);
9515
9516 prepareAxes(POSITION);
9517 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9518 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9519 // needs to be disabled, and the pointer gesture needs to be enabled.
9520 mFakePolicy->setPointerCapture(false);
9521 mFakePolicy->setPointerGestureEnabled(true);
9522 mFakePolicy->setPointerController(fakePointerController);
9523
9524 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9525 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9526 mPointerMovementScale =
9527 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9528 mPointerXZoomScale =
9529 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9530 }
9531
9532 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9534 /*flat*/ 0,
9535 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9537 /*flat*/ 0,
9538 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9539 }
9540};
9541
9542/**
9543 * Two fingers down on a pointer mode touch pad. The width
9544 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9545 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9546 * be greater than the both value to be freeform gesture, so that after two
9547 * fingers start to move downwards, the gesture should be swipe.
9548 */
9549TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9550 // The min freeform gesture width is 25units/mm x 30mm = 750
9551 // which is greater than fraction of the diagnal length of the touchpad (349).
9552 // Thus, MaxSwipWidth is 750.
9553 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9555 NotifyMotionArgs motionArgs;
9556
9557 // Two fingers down at once.
9558 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9559 // Pointer's initial position is used the [0,0] coordinate.
9560 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9561
9562 processId(mapper, FIRST_TRACKING_ID);
9563 processPosition(mapper, x1, y1);
9564 processMTSync(mapper);
9565 processId(mapper, SECOND_TRACKING_ID);
9566 processPosition(mapper, x2, y2);
9567 processMTSync(mapper);
9568 processSync(mapper);
9569
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(1U, motionArgs.pointerCount);
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9574 ASSERT_NO_FATAL_FAILURE(
9575 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9576
9577 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9578 // that there should be 1 pointer.
9579 int32_t movingDistance = 200;
9580 y1 += movingDistance;
9581 y2 += movingDistance;
9582
9583 processId(mapper, FIRST_TRACKING_ID);
9584 processPosition(mapper, x1, y1);
9585 processMTSync(mapper);
9586 processId(mapper, SECOND_TRACKING_ID);
9587 processPosition(mapper, x2, y2);
9588 processMTSync(mapper);
9589 processSync(mapper);
9590
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9592 ASSERT_EQ(1U, motionArgs.pointerCount);
9593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9596 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9597 0, 0, 0, 0));
9598}
9599
9600/**
9601 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9602 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9603 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9604 * value to be freeform gesture, so that after two fingers start to move downwards,
9605 * the gesture should be swipe.
9606 */
9607TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9608 // The min freeform gesture width is 5units/mm x 30mm = 150
9609 // which is greater than fraction of the diagnal length of the touchpad (349).
9610 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9611 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9613 NotifyMotionArgs motionArgs;
9614
9615 // Two fingers down at once.
9616 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9617 // Pointer's initial position is used the [0,0] coordinate.
9618 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9619
9620 processId(mapper, FIRST_TRACKING_ID);
9621 processPosition(mapper, x1, y1);
9622 processMTSync(mapper);
9623 processId(mapper, SECOND_TRACKING_ID);
9624 processPosition(mapper, x2, y2);
9625 processMTSync(mapper);
9626 processSync(mapper);
9627
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9629 ASSERT_EQ(1U, motionArgs.pointerCount);
9630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9632 ASSERT_NO_FATAL_FAILURE(
9633 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9634
9635 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9636 // and there should be 1 pointer.
9637 int32_t movingDistance = 200;
9638 y1 += movingDistance;
9639 y2 += movingDistance;
9640
9641 processId(mapper, FIRST_TRACKING_ID);
9642 processPosition(mapper, x1, y1);
9643 processMTSync(mapper);
9644 processId(mapper, SECOND_TRACKING_ID);
9645 processPosition(mapper, x2, y2);
9646 processMTSync(mapper);
9647 processSync(mapper);
9648
9649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9650 ASSERT_EQ(1U, motionArgs.pointerCount);
9651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9653 // New coordinate is the scaled relative coordinate from the initial coordinate.
9654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9655 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9656 0, 0, 0, 0));
9657}
9658
9659/**
9660 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9661 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9662 * freeform gestures after two fingers start to move downwards.
9663 */
9664TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9665 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9667
9668 NotifyMotionArgs motionArgs;
9669
9670 // Two fingers down at once. Wider than the max swipe width.
9671 // The gesture is expected to be PRESS, then transformed to FREEFORM
9672 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9673
9674 processId(mapper, FIRST_TRACKING_ID);
9675 processPosition(mapper, x1, y1);
9676 processMTSync(mapper);
9677 processId(mapper, SECOND_TRACKING_ID);
9678 processPosition(mapper, x2, y2);
9679 processMTSync(mapper);
9680 processSync(mapper);
9681
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9683 ASSERT_EQ(1U, motionArgs.pointerCount);
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9686 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9687 ASSERT_NO_FATAL_FAILURE(
9688 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9689
9690 int32_t movingDistance = 200;
9691
9692 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9693 // then two down events for two pointers.
9694 y1 += movingDistance;
9695 y2 += movingDistance;
9696
9697 processId(mapper, FIRST_TRACKING_ID);
9698 processPosition(mapper, x1, y1);
9699 processMTSync(mapper);
9700 processId(mapper, SECOND_TRACKING_ID);
9701 processPosition(mapper, x2, y2);
9702 processMTSync(mapper);
9703 processSync(mapper);
9704
9705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9706 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9707 ASSERT_EQ(1U, motionArgs.pointerCount);
9708 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9711 ASSERT_EQ(1U, motionArgs.pointerCount);
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9715 ASSERT_EQ(2U, motionArgs.pointerCount);
9716 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9718 // Two pointers' scaled relative coordinates from their initial centroid.
9719 // Initial y coordinates are 0 as y1 and y2 have the same value.
9720 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9721 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9722 // When pointers move, the new coordinates equal to the initial coordinates plus
9723 // scaled moving distance.
9724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9725 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9726 0, 0, 0, 0));
9727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9728 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9729 0, 0, 0, 0));
9730
9731 // Move two fingers down again, expect one MOVE motion event.
9732 y1 += movingDistance;
9733 y2 += movingDistance;
9734
9735 processId(mapper, FIRST_TRACKING_ID);
9736 processPosition(mapper, x1, y1);
9737 processMTSync(mapper);
9738 processId(mapper, SECOND_TRACKING_ID);
9739 processPosition(mapper, x2, y2);
9740 processMTSync(mapper);
9741 processSync(mapper);
9742
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9744 ASSERT_EQ(2U, motionArgs.pointerCount);
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9748 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9749 0, 0, 0, 0, 0));
9750 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9751 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9752 0, 0, 0, 0, 0));
9753}
9754
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009755// --- JoystickInputMapperTest ---
9756
9757class JoystickInputMapperTest : public InputMapperTest {
9758protected:
9759 static const int32_t RAW_X_MIN;
9760 static const int32_t RAW_X_MAX;
9761 static const int32_t RAW_Y_MIN;
9762 static const int32_t RAW_Y_MAX;
9763
9764 void SetUp() override {
9765 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9766 }
9767 void prepareAxes() {
9768 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9769 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9770 }
9771
9772 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9774 }
9775
9776 void processSync(JoystickInputMapper& mapper) {
9777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9778 }
9779
9780 void prepareVirtualDisplay(int32_t orientation) {
9781 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9782 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9783 NO_PORT, ViewportType::VIRTUAL);
9784 }
9785};
9786
9787const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9788const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9789const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9790const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9791
9792TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9793 prepareAxes();
9794 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9795
9796 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9797
9798 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9799
9800 // Send an axis event
9801 processAxis(mapper, ABS_X, 100);
9802 processSync(mapper);
9803
9804 NotifyMotionArgs args;
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9806 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9807
9808 // Send another axis event
9809 processAxis(mapper, ABS_Y, 100);
9810 processSync(mapper);
9811
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9813 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9814}
9815
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009816// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009817
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009818class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009819protected:
9820 static const char* DEVICE_NAME;
9821 static const char* DEVICE_LOCATION;
9822 static const int32_t DEVICE_ID;
9823 static const int32_t DEVICE_GENERATION;
9824 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009825 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009826 static const int32_t EVENTHUB_ID;
9827
9828 std::shared_ptr<FakeEventHub> mFakeEventHub;
9829 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009830 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009831 std::unique_ptr<InstrumentedInputReader> mReader;
9832 std::shared_ptr<InputDevice> mDevice;
9833
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009834 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009835 mFakeEventHub = std::make_unique<FakeEventHub>();
9836 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009837 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009838 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009839 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009840 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9841 }
9842
9843 void SetUp() override { SetUp(DEVICE_CLASSES); }
9844
9845 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009846 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009847 mFakePolicy.clear();
9848 }
9849
9850 void configureDevice(uint32_t changes) {
9851 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9852 mReader->requestRefreshConfiguration(changes);
9853 mReader->loopOnce();
9854 }
9855 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9856 }
9857
9858 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9859 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009860 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009861 InputDeviceIdentifier identifier;
9862 identifier.name = name;
9863 identifier.location = location;
9864 std::shared_ptr<InputDevice> device =
9865 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9866 identifier);
9867 mReader->pushNextDevice(device);
9868 mFakeEventHub->addDevice(eventHubId, name, classes);
9869 mReader->loopOnce();
9870 return device;
9871 }
9872
9873 template <class T, typename... Args>
9874 T& addControllerAndConfigure(Args... args) {
9875 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9876
9877 return controller;
9878 }
9879};
9880
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009881const char* PeripheralControllerTest::DEVICE_NAME = "device";
9882const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9883const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9884const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9885const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009886const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9887 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009888const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009889
9890// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009891class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009892protected:
9893 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009894 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009895 }
9896};
9897
9898TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009899 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009900
9901 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9902 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9903}
9904
9905TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009906 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009907
9908 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9909 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9910}
9911
9912// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009913class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009914protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009915 void SetUp() override {
9916 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9917 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009918};
9919
Chris Ye85758332021-05-16 23:05:17 -07009920TEST_F(LightControllerTest, MonoLight) {
9921 RawLightInfo infoMono = {.id = 1,
9922 .name = "Mono",
9923 .maxBrightness = 255,
9924 .flags = InputLightClass::BRIGHTNESS,
9925 .path = ""};
9926 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009927
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009928 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009929 InputDeviceInfo info;
9930 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009931 std::vector<InputDeviceLightInfo> lights = info.getLights();
9932 ASSERT_EQ(1U, lights.size());
9933 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009934
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009935 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9936 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009937}
9938
9939TEST_F(LightControllerTest, RGBLight) {
9940 RawLightInfo infoRed = {.id = 1,
9941 .name = "red",
9942 .maxBrightness = 255,
9943 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9944 .path = ""};
9945 RawLightInfo infoGreen = {.id = 2,
9946 .name = "green",
9947 .maxBrightness = 255,
9948 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9949 .path = ""};
9950 RawLightInfo infoBlue = {.id = 3,
9951 .name = "blue",
9952 .maxBrightness = 255,
9953 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9954 .path = ""};
9955 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9956 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9957 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9958
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009959 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009960 InputDeviceInfo info;
9961 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009962 std::vector<InputDeviceLightInfo> lights = info.getLights();
9963 ASSERT_EQ(1U, lights.size());
9964 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009965
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009966 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9967 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009968}
9969
9970TEST_F(LightControllerTest, MultiColorRGBLight) {
9971 RawLightInfo infoColor = {.id = 1,
9972 .name = "red",
9973 .maxBrightness = 255,
9974 .flags = InputLightClass::BRIGHTNESS |
9975 InputLightClass::MULTI_INTENSITY |
9976 InputLightClass::MULTI_INDEX,
9977 .path = ""};
9978
9979 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9980
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009981 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009982 InputDeviceInfo info;
9983 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009984 std::vector<InputDeviceLightInfo> lights = info.getLights();
9985 ASSERT_EQ(1U, lights.size());
9986 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009987
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009988 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9989 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009990}
9991
9992TEST_F(LightControllerTest, PlayerIdLight) {
9993 RawLightInfo info1 = {.id = 1,
9994 .name = "player1",
9995 .maxBrightness = 255,
9996 .flags = InputLightClass::BRIGHTNESS,
9997 .path = ""};
9998 RawLightInfo info2 = {.id = 2,
9999 .name = "player2",
10000 .maxBrightness = 255,
10001 .flags = InputLightClass::BRIGHTNESS,
10002 .path = ""};
10003 RawLightInfo info3 = {.id = 3,
10004 .name = "player3",
10005 .maxBrightness = 255,
10006 .flags = InputLightClass::BRIGHTNESS,
10007 .path = ""};
10008 RawLightInfo info4 = {.id = 4,
10009 .name = "player4",
10010 .maxBrightness = 255,
10011 .flags = InputLightClass::BRIGHTNESS,
10012 .path = ""};
10013 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10014 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10015 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10016 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10017
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019 InputDeviceInfo info;
10020 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010021 std::vector<InputDeviceLightInfo> lights = info.getLights();
10022 ASSERT_EQ(1U, lights.size());
10023 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010024
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010025 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10026 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10027 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010028}
10029
Michael Wrightd02c5b62014-02-10 15:10:22 -080010030} // namespace android