blob: 9a0c565de331269d642b44d1c2dd4fd3c99b4063 [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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000512 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700513 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000521 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700522 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000536 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700537 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000798 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
799 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700800 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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000984 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +0000985
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
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000994 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override { return {}; }
Chris Yee2b1e5c2021-03-10 22:45:12 -0800995
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000996 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
997 int32_t batteryId) const override {
Chris Yee2b1e5c2021-03-10 22:45:12 -0800998 return std::nullopt;
999 }
Kim Low03ea0352020-11-06 12:45:07 -08001000
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001001 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001002 std::vector<int32_t> ids;
1003 for (const auto& [rawId, info] : mRawLightInfos) {
1004 ids.push_back(rawId);
1005 }
1006 return ids;
1007 }
1008
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001009 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001010 auto it = mRawLightInfos.find(lightId);
1011 if (it == mRawLightInfos.end()) {
1012 return std::nullopt;
1013 }
1014 return it->second;
1015 }
1016
1017 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1018 mLightBrightness.emplace(lightId, brightness);
1019 }
1020
1021 void setLightIntensities(int32_t deviceId, int32_t lightId,
1022 std::unordered_map<LightColor, int32_t> intensities) override {
1023 mLightIntensities.emplace(lightId, intensities);
1024 };
1025
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001026 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001027 auto lightIt = mLightBrightness.find(lightId);
1028 if (lightIt == mLightBrightness.end()) {
1029 return std::nullopt;
1030 }
1031 return lightIt->second;
1032 }
1033
1034 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001035 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001036 auto lightIt = mLightIntensities.find(lightId);
1037 if (lightIt == mLightIntensities.end()) {
1038 return std::nullopt;
1039 }
1040 return lightIt->second;
1041 };
1042
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001043 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001045 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046
Chris Yea52ade12020-08-27 16:49:20 -07001047 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001048
Chris Yea52ade12020-08-27 16:49:20 -07001049 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001050};
1051
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052// --- FakeInputMapper ---
1053
1054class FakeInputMapper : public InputMapper {
1055 uint32_t mSources;
1056 int32_t mKeyboardType;
1057 int32_t mMetaState;
1058 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1059 KeyedVector<int32_t, int32_t> mScanCodeStates;
1060 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001061 // fake mapping which would normally come from keyCharacterMap
1062 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001063 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001065 std::mutex mLock;
1066 std::condition_variable mStateChangedCondition;
1067 bool mConfigureWasCalled GUARDED_BY(mLock);
1068 bool mResetWasCalled GUARDED_BY(mLock);
1069 bool mProcessWasCalled GUARDED_BY(mLock);
1070 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071
Arthur Hungc23540e2018-11-29 20:42:11 +08001072 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001073public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001074 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1075 : InputMapper(deviceContext),
1076 mSources(sources),
1077 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001078 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001079 mConfigureWasCalled(false),
1080 mResetWasCalled(false),
1081 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082
Chris Yea52ade12020-08-27 16:49:20 -07001083 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
1085 void setKeyboardType(int32_t keyboardType) {
1086 mKeyboardType = keyboardType;
1087 }
1088
1089 void setMetaState(int32_t metaState) {
1090 mMetaState = metaState;
1091 }
1092
1093 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::unique_lock<std::mutex> lock(mLock);
1095 base::ScopedLockAssertion assumeLocked(mLock);
1096 const bool configureCalled =
1097 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1098 return mConfigureWasCalled;
1099 });
1100 if (!configureCalled) {
1101 FAIL() << "Expected configure() to have been called.";
1102 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 mConfigureWasCalled = false;
1104 }
1105
1106 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001107 std::unique_lock<std::mutex> lock(mLock);
1108 base::ScopedLockAssertion assumeLocked(mLock);
1109 const bool resetCalled =
1110 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1111 return mResetWasCalled;
1112 });
1113 if (!resetCalled) {
1114 FAIL() << "Expected reset() to have been called.";
1115 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116 mResetWasCalled = false;
1117 }
1118
Yi Kong9b14ac62018-07-17 13:48:38 -07001119 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001120 std::unique_lock<std::mutex> lock(mLock);
1121 base::ScopedLockAssertion assumeLocked(mLock);
1122 const bool processCalled =
1123 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1124 return mProcessWasCalled;
1125 });
1126 if (!processCalled) {
1127 FAIL() << "Expected process() to have been called.";
1128 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 if (outLastEvent) {
1130 *outLastEvent = mLastEvent;
1131 }
1132 mProcessWasCalled = false;
1133 }
1134
1135 void setKeyCodeState(int32_t keyCode, int32_t state) {
1136 mKeyCodeStates.replaceValueFor(keyCode, state);
1137 }
1138
1139 void setScanCodeState(int32_t scanCode, int32_t state) {
1140 mScanCodeStates.replaceValueFor(scanCode, state);
1141 }
1142
1143 void setSwitchState(int32_t switchCode, int32_t state) {
1144 mSwitchStates.replaceValueFor(switchCode, state);
1145 }
1146
1147 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001148 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149 }
1150
Philip Junker4af3b3d2021-12-14 10:36:55 +01001151 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1152 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1153 }
1154
Michael Wrightd02c5b62014-02-10 15:10:22 -08001155private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001156 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157
Chris Yea52ade12020-08-27 16:49:20 -07001158 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 InputMapper::populateDeviceInfo(deviceInfo);
1160
1161 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1162 deviceInfo->setKeyboardType(mKeyboardType);
1163 }
1164 }
1165
Chris Yea52ade12020-08-27 16:49:20 -07001166 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001167 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001169
1170 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001171 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001172 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1173 mViewport = config->getDisplayViewportByPort(*displayPort);
1174 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001175
1176 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001177 }
1178
Chris Yea52ade12020-08-27 16:49:20 -07001179 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001180 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001181 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001182 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 }
1184
Chris Yea52ade12020-08-27 16:49:20 -07001185 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001186 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 mLastEvent = *rawEvent;
1188 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001189 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 }
1191
Chris Yea52ade12020-08-27 16:49:20 -07001192 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1194 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1195 }
1196
Philip Junker4af3b3d2021-12-14 10:36:55 +01001197 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1198 auto it = mKeyCodeMapping.find(locationKeyCode);
1199 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1200 }
1201
Chris Yea52ade12020-08-27 16:49:20 -07001202 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1204 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1205 }
1206
Chris Yea52ade12020-08-27 16:49:20 -07001207 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1209 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1210 }
1211
Chris Yea52ade12020-08-27 16:49:20 -07001212 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001213 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001214 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001215 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1217 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1218 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219 }
1220 }
1221 }
Chris Yea52ade12020-08-27 16:49:20 -07001222 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001223 return result;
1224 }
1225
1226 virtual int32_t getMetaState() {
1227 return mMetaState;
1228 }
1229
1230 virtual void fadePointer() {
1231 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001232
1233 virtual std::optional<int32_t> getAssociatedDisplay() {
1234 if (mViewport) {
1235 return std::make_optional(mViewport->displayId);
1236 }
1237 return std::nullopt;
1238 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239};
1240
1241
1242// --- InstrumentedInputReader ---
1243
1244class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001245 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
1247public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001248 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1249 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001250 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001251 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001253 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001255 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001257 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001258 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 InputDeviceIdentifier identifier;
1260 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001261 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001263 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001264 }
1265
Prabir Pradhan28efc192019-11-05 01:10:04 +00001266 // Make the protected loopOnce method accessible to tests.
1267 using InputReader::loopOnce;
1268
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001270 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1271 const InputDeviceIdentifier& identifier)
1272 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001273 if (!mNextDevices.empty()) {
1274 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1275 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 return device;
1277 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001278 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280
arthurhungdcef2dc2020-08-11 14:47:50 +08001281 // --- FakeInputReaderContext ---
1282 class FakeInputReaderContext : public ContextImpl {
1283 int32_t mGlobalMetaState;
1284 bool mUpdateGlobalMetaStateWasCalled;
1285 int32_t mGeneration;
1286
1287 public:
1288 FakeInputReaderContext(InputReader* reader)
1289 : ContextImpl(reader),
1290 mGlobalMetaState(0),
1291 mUpdateGlobalMetaStateWasCalled(false),
1292 mGeneration(1) {}
1293
1294 virtual ~FakeInputReaderContext() {}
1295
1296 void assertUpdateGlobalMetaStateWasCalled() {
1297 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1298 << "Expected updateGlobalMetaState() to have been called.";
1299 mUpdateGlobalMetaStateWasCalled = false;
1300 }
1301
1302 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1303
1304 uint32_t getGeneration() { return mGeneration; }
1305
1306 void updateGlobalMetaState() override {
1307 mUpdateGlobalMetaStateWasCalled = true;
1308 ContextImpl::updateGlobalMetaState();
1309 }
1310
1311 int32_t getGlobalMetaState() override {
1312 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1313 }
1314
1315 int32_t bumpGeneration() override {
1316 mGeneration = ContextImpl::bumpGeneration();
1317 return mGeneration;
1318 }
1319 } mFakeContext;
1320
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001322
1323public:
1324 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325};
1326
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327// --- InputReaderPolicyTest ---
1328class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001329protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330 sp<FakeInputReaderPolicy> mFakePolicy;
1331
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001332 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001333 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334};
1335
1336/**
1337 * Check that empty set of viewports is an acceptable configuration.
1338 * Also try to get internal viewport two different ways - by type and by uniqueId.
1339 *
1340 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1341 * Such configuration is not currently allowed.
1342 */
1343TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001344 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001345
1346 // We didn't add any viewports yet, so there shouldn't be any.
1347 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001348 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001349 ASSERT_FALSE(internalViewport);
1350
1351 // Add an internal viewport, then clear it
1352 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001353 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001354 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355
1356 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001357 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001359 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001360
1361 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001364 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001365
1366 mFakePolicy->clearViewports();
1367 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001368 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001369 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001370 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001371 ASSERT_FALSE(internalViewport);
1372}
1373
1374TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1375 const std::string internalUniqueId = "local:0";
1376 const std::string externalUniqueId = "local:1";
1377 const std::string virtualUniqueId1 = "virtual:2";
1378 const std::string virtualUniqueId2 = "virtual:3";
1379 constexpr int32_t virtualDisplayId1 = 2;
1380 constexpr int32_t virtualDisplayId2 = 3;
1381
1382 // Add an internal viewport
1383 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1385 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 // Add an external viewport
1387 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001388 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1389 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 // Add an virtual viewport
1391 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001392 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1393 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 // Add another virtual viewport
1395 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001396 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1397 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001398
1399 // Check matching by type for internal
1400 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402 ASSERT_TRUE(internalViewport);
1403 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1404
1405 // Check matching by type for external
1406 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001407 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408 ASSERT_TRUE(externalViewport);
1409 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1410
1411 // Check matching by uniqueId for virtual viewport #1
1412 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001415 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1417 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1418
1419 // Check matching by uniqueId for virtual viewport #2
1420 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001421 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001422 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001423 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001424 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1425 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1426}
1427
1428
1429/**
1430 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1431 * that lookup works by checking display id.
1432 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1433 */
1434TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1435 const std::string uniqueId1 = "uniqueId1";
1436 const std::string uniqueId2 = "uniqueId2";
1437 constexpr int32_t displayId1 = 2;
1438 constexpr int32_t displayId2 = 3;
1439
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001440 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1441 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001442 for (const ViewportType& type : types) {
1443 mFakePolicy->clearViewports();
1444 // Add a viewport
1445 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001446 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1447 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 // Add another viewport
1449 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1451 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452
1453 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001454 std::optional<DisplayViewport> viewport1 =
1455 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_TRUE(viewport1);
1457 ASSERT_EQ(displayId1, viewport1->displayId);
1458 ASSERT_EQ(type, viewport1->type);
1459
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001460 std::optional<DisplayViewport> viewport2 =
1461 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001462 ASSERT_TRUE(viewport2);
1463 ASSERT_EQ(displayId2, viewport2->displayId);
1464 ASSERT_EQ(type, viewport2->type);
1465
1466 // When there are multiple viewports of the same kind, and uniqueId is not specified
1467 // in the call to getDisplayViewport, then that situation is not supported.
1468 // The viewports can be stored in any order, so we cannot rely on the order, since that
1469 // is just implementation detail.
1470 // However, we can check that it still returns *a* viewport, we just cannot assert
1471 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001472 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001473 ASSERT_TRUE(someViewport);
1474 }
1475}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001477/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001478 * When we have multiple internal displays make sure we always return the default display when
1479 * querying by type.
1480 */
1481TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1482 const std::string uniqueId1 = "uniqueId1";
1483 const std::string uniqueId2 = "uniqueId2";
1484 constexpr int32_t nonDefaultDisplayId = 2;
1485 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1486 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1487
1488 // Add the default display first and ensure it gets returned.
1489 mFakePolicy->clearViewports();
1490 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001491 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001492 ViewportType::INTERNAL);
1493 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001494 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001495 ViewportType::INTERNAL);
1496
1497 std::optional<DisplayViewport> viewport =
1498 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1499 ASSERT_TRUE(viewport);
1500 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1501 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1502
1503 // Add the default display second to make sure order doesn't matter.
1504 mFakePolicy->clearViewports();
1505 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001506 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001507 ViewportType::INTERNAL);
1508 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001509 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001510 ViewportType::INTERNAL);
1511
1512 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1513 ASSERT_TRUE(viewport);
1514 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1515 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1516}
1517
1518/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001519 * Check getDisplayViewportByPort
1520 */
1521TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001522 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001523 const std::string uniqueId1 = "uniqueId1";
1524 const std::string uniqueId2 = "uniqueId2";
1525 constexpr int32_t displayId1 = 1;
1526 constexpr int32_t displayId2 = 2;
1527 const uint8_t hdmi1 = 0;
1528 const uint8_t hdmi2 = 1;
1529 const uint8_t hdmi3 = 2;
1530
1531 mFakePolicy->clearViewports();
1532 // Add a viewport that's associated with some display port that's not of interest.
1533 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001534 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1535 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001536 // Add another viewport, connected to HDMI1 port
1537 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001538 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1539 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001540
1541 // Check that correct display viewport was returned by comparing the display ports.
1542 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1543 ASSERT_TRUE(hdmi1Viewport);
1544 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1545 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1546
1547 // Check that we can still get the same viewport using the uniqueId
1548 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1549 ASSERT_TRUE(hdmi1Viewport);
1550 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1551 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1552 ASSERT_EQ(type, hdmi1Viewport->type);
1553
1554 // Check that we cannot find a port with "HDMI2", because we never added one
1555 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1556 ASSERT_FALSE(hdmi2Viewport);
1557}
1558
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559// --- InputReaderTest ---
1560
1561class InputReaderTest : public testing::Test {
1562protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001563 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001565 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001566 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
Chris Yea52ade12020-08-27 16:49:20 -07001568 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001569 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001570 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001571 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572
Prabir Pradhan28efc192019-11-05 01:10:04 +00001573 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001574 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001575 }
1576
Chris Yea52ade12020-08-27 16:49:20 -07001577 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001578 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 }
1581
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001582 void addDevice(int32_t eventHubId, const std::string& name,
1583 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001584 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001585
1586 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001587 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001588 }
1589 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001590 mReader->loopOnce();
1591 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001592 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1593 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001594 }
1595
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001596 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001598 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001599 }
1600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001601 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001602 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001603 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001604 }
1605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001607 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001608 ftl::Flags<InputDeviceClass> classes,
1609 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001610 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001611 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1612 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001613 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001614 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615 return mapper;
1616 }
1617};
1618
Chris Ye98d3f532020-10-01 21:48:59 -07001619TEST_F(InputReaderTest, PolicyGetInputDevices) {
1620 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001621 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001622 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623
1624 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001625 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001626 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001627 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001628 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001629 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1630 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001631 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632}
1633
Chris Yee7310032020-09-22 15:36:28 -07001634TEST_F(InputReaderTest, GetMergedInputDevices) {
1635 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1636 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1637 // Add two subdevices to device
1638 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1639 // Must add at least one mapper or the device will be ignored!
1640 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1641 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1642
1643 // Push same device instance for next device to be added, so they'll have same identifier.
1644 mReader->pushNextDevice(device);
1645 mReader->pushNextDevice(device);
1646 ASSERT_NO_FATAL_FAILURE(
1647 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1648 ASSERT_NO_FATAL_FAILURE(
1649 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1650
1651 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001652 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001653}
1654
Chris Yee14523a2020-12-19 13:46:00 -08001655TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1656 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1657 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1658 // Add two subdevices to device
1659 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1660 // Must add at least one mapper or the device will be ignored!
1661 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1662 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1663
1664 // Push same device instance for next device to be added, so they'll have same identifier.
1665 mReader->pushNextDevice(device);
1666 mReader->pushNextDevice(device);
1667 // Sensor device is initially disabled
1668 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1669 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1670 nullptr));
1671 // Device is disabled because the only sub device is a sensor device and disabled initially.
1672 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1673 ASSERT_FALSE(device->isEnabled());
1674 ASSERT_NO_FATAL_FAILURE(
1675 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1676 // The merged device is enabled if any sub device is enabled
1677 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1678 ASSERT_TRUE(device->isEnabled());
1679}
1680
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001681TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001682 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001683 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001684 constexpr int32_t eventHubId = 1;
1685 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001688 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001689 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001690
Yi Kong9b14ac62018-07-17 13:48:38 -07001691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001692
1693 NotifyDeviceResetArgs resetArgs;
1694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695 ASSERT_EQ(deviceId, resetArgs.deviceId);
1696
1697 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001698 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001699 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001702 ASSERT_EQ(deviceId, resetArgs.deviceId);
1703 ASSERT_EQ(device->isEnabled(), false);
1704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001705 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001706 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001709 ASSERT_EQ(device->isEnabled(), false);
1710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001711 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001712 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001714 ASSERT_EQ(deviceId, resetArgs.deviceId);
1715 ASSERT_EQ(device->isEnabled(), true);
1716}
1717
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001719 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001720 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001721 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001722 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001724 AINPUT_SOURCE_KEYBOARD, nullptr);
1725 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001726
1727 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1728 AINPUT_SOURCE_ANY, AKEYCODE_A))
1729 << "Should return unknown when the device id is >= 0 but unknown.";
1730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1732 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1733 << "Should return unknown when the device id is valid but the sources are not "
1734 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001736 ASSERT_EQ(AKEY_STATE_DOWN,
1737 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1738 AKEYCODE_A))
1739 << "Should return value provided by mapper when device id is valid and the device "
1740 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001741
1742 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1743 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1744 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1745
1746 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1747 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1748 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1749}
1750
Philip Junker4af3b3d2021-12-14 10:36:55 +01001751TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1752 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1753 constexpr int32_t eventHubId = 1;
1754 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1755 InputDeviceClass::KEYBOARD,
1756 AINPUT_SOURCE_KEYBOARD, nullptr);
1757 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1758
1759 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1760 << "Should return unknown when the device with the specified id is not found.";
1761
1762 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1763 << "Should return correct mapping when device id is valid and mapping exists.";
1764
1765 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1766 << "Should return the location key code when device id is valid and there's no "
1767 "mapping.";
1768}
1769
1770TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1771 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1772 constexpr int32_t eventHubId = 1;
1773 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1774 InputDeviceClass::JOYSTICK,
1775 AINPUT_SOURCE_GAMEPAD, nullptr);
1776 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1777
1778 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1779 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1780}
1781
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001784 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001786 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001788 AINPUT_SOURCE_KEYBOARD, nullptr);
1789 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
1791 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1792 AINPUT_SOURCE_ANY, KEY_A))
1793 << "Should return unknown when the device id is >= 0 but unknown.";
1794
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001795 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1796 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1797 << "Should return unknown when the device id is valid but the sources are not "
1798 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 ASSERT_EQ(AKEY_STATE_DOWN,
1801 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1802 KEY_A))
1803 << "Should return value provided by mapper when device id is valid and the device "
1804 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001805
1806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1807 AINPUT_SOURCE_TRACKBALL, KEY_A))
1808 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1809
1810 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1811 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1812 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1813}
1814
1815TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001817 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001818 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001819 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001821 AINPUT_SOURCE_KEYBOARD, nullptr);
1822 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823
1824 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1825 AINPUT_SOURCE_ANY, SW_LID))
1826 << "Should return unknown when the device id is >= 0 but unknown.";
1827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001828 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1829 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1830 << "Should return unknown when the device id is valid but the sources are not "
1831 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001833 ASSERT_EQ(AKEY_STATE_DOWN,
1834 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1835 SW_LID))
1836 << "Should return value provided by mapper when device id is valid and the device "
1837 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001838
1839 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1840 AINPUT_SOURCE_TRACKBALL, SW_LID))
1841 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1842
1843 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1844 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1845 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1846}
1847
1848TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001850 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001854 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001856 mapper.addSupportedKeyCode(AKEYCODE_A);
1857 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001858
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001859 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001860 uint8_t flags[4] = { 0, 0, 0, 1 };
1861
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001862 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001863 << "Should return false when device id is >= 0 but unknown.";
1864 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1865
1866 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001867 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 << "Should return false when device id is valid but the sources are not supported by "
1869 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001870 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1871
1872 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001873 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 keyCodes, flags))
1875 << "Should return value provided by mapper when device id is valid and the device "
1876 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001877 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1878
1879 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001880 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1881 << "Should return false when the device id is < 0 but the sources are not supported by "
1882 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1884
1885 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001886 ASSERT_TRUE(
1887 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1888 << "Should return value provided by mapper when device id is < 0 and one of the "
1889 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1891}
1892
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001893TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001895 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896
1897 NotifyConfigurationChangedArgs args;
1898
1899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1900 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1901}
1902
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001903TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001904 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001905 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001906 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001908 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001909 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001911 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001913 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001914 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001915 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1916
1917 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001918 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001919 ASSERT_EQ(when, event.when);
1920 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001922 ASSERT_EQ(EV_KEY, event.type);
1923 ASSERT_EQ(KEY_A, event.code);
1924 ASSERT_EQ(1, event.value);
1925}
1926
Garfield Tan1c7bc862020-01-28 13:24:04 -08001927TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001929 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001930 constexpr int32_t eventHubId = 1;
1931 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001932 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001933 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001934 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001935 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001936
1937 NotifyDeviceResetArgs resetArgs;
1938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001939 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001944 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001945 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001947 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001948 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001950 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001951 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001953 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001954 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001957 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001958}
1959
Garfield Tan1c7bc862020-01-28 13:24:04 -08001960TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1961 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001962 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001963 constexpr int32_t eventHubId = 1;
1964 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1965 // Must add at least one mapper or the device will be ignored!
1966 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001967 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001968 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1969
1970 NotifyDeviceResetArgs resetArgs;
1971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1972 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1973}
1974
Arthur Hungc23540e2018-11-29 20:42:11 +08001975TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001978 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001979 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001980 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1981 FakeInputMapper& mapper =
1982 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001983 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001984
1985 const uint8_t hdmi1 = 1;
1986
1987 // Associated touch screen with second display.
1988 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1989
1990 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001991 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001992 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001993 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001994 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001995 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001996 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001997 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002000
2001 // Add the device, and make sure all of the callbacks are triggered.
2002 // The device is added after the input port associations are processed since
2003 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002004 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002007 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002008
Arthur Hung2c9a3342019-07-23 14:18:59 +08002009 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002010 ASSERT_EQ(deviceId, device->getId());
2011 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2012 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002013
2014 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002015 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002016 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002017 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002018}
2019
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002020TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2021 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002022 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002023 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2024 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2025 // Must add at least one mapper or the device will be ignored!
2026 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2027 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2028 mReader->pushNextDevice(device);
2029 mReader->pushNextDevice(device);
2030 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2031 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2032
2033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2034
2035 NotifyDeviceResetArgs resetArgs;
2036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2037 ASSERT_EQ(deviceId, resetArgs.deviceId);
2038 ASSERT_TRUE(device->isEnabled());
2039 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2040 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2041
2042 disableDevice(deviceId);
2043 mReader->loopOnce();
2044
2045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2046 ASSERT_EQ(deviceId, resetArgs.deviceId);
2047 ASSERT_FALSE(device->isEnabled());
2048 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2049 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2050
2051 enableDevice(deviceId);
2052 mReader->loopOnce();
2053
2054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2055 ASSERT_EQ(deviceId, resetArgs.deviceId);
2056 ASSERT_TRUE(device->isEnabled());
2057 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2058 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2059}
2060
2061TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2062 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002063 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002064 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2065 // Add two subdevices to device
2066 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2067 FakeInputMapper& mapperDevice1 =
2068 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2069 FakeInputMapper& mapperDevice2 =
2070 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2071 mReader->pushNextDevice(device);
2072 mReader->pushNextDevice(device);
2073 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2074 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2075
2076 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2077 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2078
2079 ASSERT_EQ(AKEY_STATE_DOWN,
2080 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2081 ASSERT_EQ(AKEY_STATE_DOWN,
2082 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2083 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2084 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2085}
2086
Prabir Pradhan7e186182020-11-10 13:56:45 -08002087TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2088 NotifyPointerCaptureChangedArgs args;
2089
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002091 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2092 mReader->loopOnce();
2093 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002094 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2095 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002096
2097 mFakePolicy->setPointerCapture(false);
2098 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2099 mReader->loopOnce();
2100 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002101 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002102
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002103 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002104 // does not change.
2105 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2106 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002107 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002108}
2109
Chris Ye87143712020-11-10 05:05:58 +00002110class FakeVibratorInputMapper : public FakeInputMapper {
2111public:
2112 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2113 : FakeInputMapper(deviceContext, sources) {}
2114
2115 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2116};
2117
2118TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2119 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002120 ftl::Flags<InputDeviceClass> deviceClass =
2121 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002122 constexpr int32_t eventHubId = 1;
2123 const char* DEVICE_LOCATION = "BLUETOOTH";
2124 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2125 FakeVibratorInputMapper& mapper =
2126 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2127 mReader->pushNextDevice(device);
2128
2129 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2130 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2131
2132 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2133 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2134}
2135
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002136// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002137
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002138class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002140 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002141
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002142 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002143
2144 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2145
2146 void dump(std::string& dump) override {}
2147
2148 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2149 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002150 }
2151
Chris Yee2b1e5c2021-03-10 22:45:12 -08002152 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2153 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002154 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002155
2156 bool setLightColor(int32_t lightId, int32_t color) override {
2157 getDeviceContext().setLightBrightness(lightId, color >> 24);
2158 return true;
2159 }
2160
2161 std::optional<int32_t> getLightColor(int32_t lightId) override {
2162 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2163 if (!result.has_value()) {
2164 return std::nullopt;
2165 }
2166 return result.value() << 24;
2167 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002168
2169 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2170
2171 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2172
2173private:
2174 InputDeviceContext& mDeviceContext;
2175 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2176 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002177};
2178
Chris Yee2b1e5c2021-03-10 22:45:12 -08002179TEST_F(InputReaderTest, BatteryGetCapacity) {
2180 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002181 ftl::Flags<InputDeviceClass> deviceClass =
2182 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002183 constexpr int32_t eventHubId = 1;
2184 const char* DEVICE_LOCATION = "BLUETOOTH";
2185 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002186 FakePeripheralController& controller =
2187 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188 mReader->pushNextDevice(device);
2189
2190 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2191
2192 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2193 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2194}
2195
2196TEST_F(InputReaderTest, BatteryGetStatus) {
2197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002198 ftl::Flags<InputDeviceClass> deviceClass =
2199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002200 constexpr int32_t eventHubId = 1;
2201 const char* DEVICE_LOCATION = "BLUETOOTH";
2202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002203 FakePeripheralController& controller =
2204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205 mReader->pushNextDevice(device);
2206
2207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2208
2209 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2210 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2211}
2212
Chris Ye3fdbfef2021-01-06 18:45:18 -08002213TEST_F(InputReaderTest, LightGetColor) {
2214 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002215 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216 constexpr int32_t eventHubId = 1;
2217 const char* DEVICE_LOCATION = "BLUETOOTH";
2218 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002219 FakePeripheralController& controller =
2220 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002221 mReader->pushNextDevice(device);
2222 RawLightInfo info = {.id = 1,
2223 .name = "Mono",
2224 .maxBrightness = 255,
2225 .flags = InputLightClass::BRIGHTNESS,
2226 .path = ""};
2227 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2228 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2229
2230 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002231
Chris Yee2b1e5c2021-03-10 22:45:12 -08002232 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2233 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002234 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2235 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2236}
2237
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002238// --- InputReaderIntegrationTest ---
2239
2240// These tests create and interact with the InputReader only through its interface.
2241// The InputReader is started during SetUp(), which starts its processing in its own
2242// thread. The tests use linux uinput to emulate input devices.
2243// NOTE: Interacting with the physical device while these tests are running may cause
2244// the tests to fail.
2245class InputReaderIntegrationTest : public testing::Test {
2246protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002247 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002249 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002250
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002251 std::shared_ptr<FakePointerController> mFakePointerController;
2252
Chris Yea52ade12020-08-27 16:49:20 -07002253 void SetUp() override {
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002254 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002255 mFakePointerController = std::make_shared<FakePointerController>();
2256 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002257 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2258 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002259
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002260 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2261 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002262 ASSERT_EQ(mReader->start(), OK);
2263
2264 // Since this test is run on a real device, all the input devices connected
2265 // to the test device will show up in mReader. We wait for those input devices to
2266 // show up before beginning the tests.
2267 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2268 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2269 }
2270
Chris Yea52ade12020-08-27 16:49:20 -07002271 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002272 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002273 mReader.reset();
2274 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002275 mFakePolicy.clear();
2276 }
2277};
2278
2279TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2280 // An invalid input device that is only used for this test.
2281 class InvalidUinputDevice : public UinputDevice {
2282 public:
2283 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2284
2285 private:
2286 void configureDevice(int fd, uinput_user_dev* device) override {}
2287 };
2288
2289 const size_t numDevices = mFakePolicy->getInputDevices().size();
2290
2291 // UinputDevice does not set any event or key bits, so InputReader should not
2292 // consider it as a valid device.
2293 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2296 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2297
2298 invalidDevice.reset();
2299 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2301 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2302}
2303
2304TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2305 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2306
2307 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2308 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2309 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2310 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2311
2312 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002313 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002314 const auto& it =
2315 std::find_if(inputDevices.begin(), inputDevices.end(),
2316 [&keyboard](const InputDeviceInfo& info) {
2317 return info.getIdentifier().name == keyboard->getName();
2318 });
2319
2320 ASSERT_NE(it, inputDevices.end());
2321 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2322 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2323 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002324
2325 keyboard.reset();
2326 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2327 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2328 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2329}
2330
2331TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2332 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2334
2335 NotifyConfigurationChangedArgs configChangedArgs;
2336 ASSERT_NO_FATAL_FAILURE(
2337 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002338 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002339 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2340
2341 NotifyKeyArgs keyArgs;
2342 keyboard->pressAndReleaseHomeKey();
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002345 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002346 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002347 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002348 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002349 prevTimestamp = keyArgs.eventTime;
2350
2351 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002353 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002354 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002355 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002356}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002357
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002358/**
2359 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2360 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2361 * are passed to the listener.
2362 */
2363static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2364TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2365 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2366 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2367 NotifyKeyArgs keyArgs;
2368
2369 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2370 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2371 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2372 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2373
2374 controller->pressAndReleaseKey(BTN_GEAR_UP);
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2377 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2378}
2379
Arthur Hungaab25622020-01-16 11:22:11 +08002380// --- TouchProcessTest ---
2381class TouchIntegrationTest : public InputReaderIntegrationTest {
2382protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002383 const std::string UNIQUE_ID = "local:0";
2384
Chris Yea52ade12020-08-27 16:49:20 -07002385 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002386 InputReaderIntegrationTest::SetUp();
2387 // At least add an internal display.
2388 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2389 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002390 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002391
2392 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2393 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2395 }
2396
2397 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2398 int32_t orientation, const std::string& uniqueId,
2399 std::optional<uint8_t> physicalPort,
2400 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002401 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2402 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002403 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2404 }
2405
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002406 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2407 NotifyMotionArgs args;
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2409 EXPECT_EQ(action, args.action);
2410 ASSERT_EQ(points.size(), args.pointerCount);
2411 for (size_t i = 0; i < args.pointerCount; i++) {
2412 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2413 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2414 }
2415 }
2416
Arthur Hungaab25622020-01-16 11:22:11 +08002417 std::unique_ptr<UinputTouchScreen> mDevice;
2418};
2419
2420TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2421 NotifyMotionArgs args;
2422 const Point centerPoint = mDevice->getCenterPoint();
2423
2424 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002425 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002426 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002427 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2430
2431 // ACTION_MOVE
2432 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002433 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2436
2437 // ACTION_UP
2438 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002439 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002440 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2441 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2442}
2443
2444TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2445 NotifyMotionArgs args;
2446 const Point centerPoint = mDevice->getCenterPoint();
2447
2448 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002449 mDevice->sendSlot(FIRST_SLOT);
2450 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002451 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2455
2456 // ACTION_POINTER_DOWN (Second slot)
2457 const Point secondPoint = centerPoint + Point(100, 100);
2458 mDevice->sendSlot(SECOND_SLOT);
2459 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002460 mDevice->sendDown(secondPoint);
2461 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002463 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002464
2465 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002466 mDevice->sendMove(secondPoint + Point(1, 1));
2467 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2470
2471 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002472 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002473 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002475 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002476
2477 // ACTION_UP
2478 mDevice->sendSlot(FIRST_SLOT);
2479 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002480 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2482 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2483}
2484
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002485/**
2486 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2487 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2488 * data?
2489 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2490 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2491 * for Pointer 0 only is generated after.
2492 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2493 * events, we will not miss any information.
2494 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2495 * event generated afterwards that contains the newest movement of pointer 0.
2496 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2497 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2498 * losing information about non-palm pointers.
2499 */
2500TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2501 NotifyMotionArgs args;
2502 const Point centerPoint = mDevice->getCenterPoint();
2503
2504 // ACTION_DOWN
2505 mDevice->sendSlot(FIRST_SLOT);
2506 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2507 mDevice->sendDown(centerPoint);
2508 mDevice->sendSync();
2509 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2510
2511 // ACTION_POINTER_DOWN (Second slot)
2512 const Point secondPoint = centerPoint + Point(100, 100);
2513 mDevice->sendSlot(SECOND_SLOT);
2514 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2515 mDevice->sendDown(secondPoint);
2516 mDevice->sendSync();
2517 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2518
2519 // ACTION_MOVE (First slot)
2520 mDevice->sendSlot(FIRST_SLOT);
2521 mDevice->sendMove(centerPoint + Point(5, 5));
2522 // ACTION_POINTER_UP (Second slot)
2523 mDevice->sendSlot(SECOND_SLOT);
2524 mDevice->sendPointerUp();
2525 // Send a single sync for the above 2 pointer updates
2526 mDevice->sendSync();
2527
2528 // First, we should get POINTER_UP for the second pointer
2529 assertReceivedMotion(ACTION_POINTER_1_UP,
2530 {/*first pointer */ centerPoint + Point(5, 5),
2531 /*second pointer*/ secondPoint});
2532
2533 // Next, the MOVE event for the first pointer
2534 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2535}
2536
2537/**
2538 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2539 * move, and then it will go up, all in the same frame.
2540 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2541 * gets sent to the listener.
2542 */
2543TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2544 NotifyMotionArgs args;
2545 const Point centerPoint = mDevice->getCenterPoint();
2546
2547 // ACTION_DOWN
2548 mDevice->sendSlot(FIRST_SLOT);
2549 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2550 mDevice->sendDown(centerPoint);
2551 mDevice->sendSync();
2552 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2553
2554 // ACTION_POINTER_DOWN (Second slot)
2555 const Point secondPoint = centerPoint + Point(100, 100);
2556 mDevice->sendSlot(SECOND_SLOT);
2557 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2558 mDevice->sendDown(secondPoint);
2559 mDevice->sendSync();
2560 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2561
2562 // ACTION_MOVE (First slot)
2563 mDevice->sendSlot(FIRST_SLOT);
2564 mDevice->sendMove(centerPoint + Point(5, 5));
2565 // ACTION_POINTER_UP (Second slot)
2566 mDevice->sendSlot(SECOND_SLOT);
2567 mDevice->sendMove(secondPoint + Point(6, 6));
2568 mDevice->sendPointerUp();
2569 // Send a single sync for the above 2 pointer updates
2570 mDevice->sendSync();
2571
2572 // First, we should get POINTER_UP for the second pointer
2573 // The movement of the second pointer during the liftoff frame is ignored.
2574 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2575 assertReceivedMotion(ACTION_POINTER_1_UP,
2576 {/*first pointer */ centerPoint + Point(5, 5),
2577 /*second pointer*/ secondPoint});
2578
2579 // Next, the MOVE event for the first pointer
2580 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2581}
2582
Arthur Hungaab25622020-01-16 11:22:11 +08002583TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2584 NotifyMotionArgs args;
2585 const Point centerPoint = mDevice->getCenterPoint();
2586
2587 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002588 mDevice->sendSlot(FIRST_SLOT);
2589 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002590 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002591 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2593 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2594
arthurhungcc7f9802020-04-30 17:55:40 +08002595 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002596 const Point secondPoint = centerPoint + Point(100, 100);
2597 mDevice->sendSlot(SECOND_SLOT);
2598 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2599 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002600 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002601 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002602 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002603
arthurhungcc7f9802020-04-30 17:55:40 +08002604 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2609
arthurhungcc7f9802020-04-30 17:55:40 +08002610 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2611 // a palm event.
2612 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002613 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002616 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002617 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
arthurhungcc7f9802020-04-30 17:55:40 +08002619 // Send up to second slot, expect first slot send moving.
2620 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002621 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002624
arthurhungcc7f9802020-04-30 17:55:40 +08002625 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002626 mDevice->sendSlot(FIRST_SLOT);
2627 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002629
arthurhungcc7f9802020-04-30 17:55:40 +08002630 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002632}
2633
Michael Wrightd02c5b62014-02-10 15:10:22 -08002634// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635class InputDeviceTest : public testing::Test {
2636protected:
2637 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002638 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 static const int32_t DEVICE_ID;
2640 static const int32_t DEVICE_GENERATION;
2641 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002642 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002643 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002645 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002647 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002648 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002649 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
Chris Yea52ade12020-08-27 16:49:20 -07002651 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002652 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002653 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002654 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002655 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002656 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002657 InputDeviceIdentifier identifier;
2658 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002659 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002660 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002661 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002662 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002663 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002664 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 }
2666
Chris Yea52ade12020-08-27 16:49:20 -07002667 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002668 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002669 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002670 }
2671};
2672
2673const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002674const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002675const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2677const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002678const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002679 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002680const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681
2682TEST_F(InputDeviceTest, ImmutableProperties) {
2683 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002684 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002685 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002686}
2687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002688TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2689 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002690}
2691
Michael Wrightd02c5b62014-02-10 15:10:22 -08002692TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2693 // Configuration.
2694 InputReaderConfiguration config;
2695 mDevice->configure(ARBITRARY_TIME, &config, 0);
2696
2697 // Reset.
2698 mDevice->reset(ARBITRARY_TIME);
2699
2700 NotifyDeviceResetArgs resetArgs;
2701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2702 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2703 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2704
2705 // Metadata.
2706 ASSERT_TRUE(mDevice->isIgnored());
2707 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2708
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002709 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002710 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002711 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2713 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2714
2715 // State queries.
2716 ASSERT_EQ(0, mDevice->getMetaState());
2717
2718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2719 << "Ignored device should return unknown key code state.";
2720 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2721 << "Ignored device should return unknown scan code state.";
2722 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2723 << "Ignored device should return unknown switch state.";
2724
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002725 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002727 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 << "Ignored device should never mark any key codes.";
2729 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2730 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2731}
2732
2733TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2734 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002735 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002736
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002737 FakeInputMapper& mapper1 =
2738 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002739 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2740 mapper1.setMetaState(AMETA_ALT_ON);
2741 mapper1.addSupportedKeyCode(AKEYCODE_A);
2742 mapper1.addSupportedKeyCode(AKEYCODE_B);
2743 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2744 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2745 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2746 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2747 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002749 FakeInputMapper& mapper2 =
2750 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752
2753 InputReaderConfiguration config;
2754 mDevice->configure(ARBITRARY_TIME, &config, 0);
2755
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002756 std::string propertyValue;
2757 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002759 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002761 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2762 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002763
2764 // Reset
2765 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002766 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2767 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768
2769 NotifyDeviceResetArgs resetArgs;
2770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2771 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2772 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2773
2774 // Metadata.
2775 ASSERT_FALSE(mDevice->isIgnored());
2776 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2777
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002778 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002780 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002781 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2782 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2783
2784 // State queries.
2785 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2786 << "Should query mappers and combine meta states.";
2787
2788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2789 << "Should return unknown key code state when source not supported.";
2790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2791 << "Should return unknown scan code state when source not supported.";
2792 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2793 << "Should return unknown switch state when source not supported.";
2794
2795 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2796 << "Should query mapper when source is supported.";
2797 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2798 << "Should query mapper when source is supported.";
2799 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2800 << "Should query mapper when source is supported.";
2801
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002802 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002803 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002804 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002805 << "Should do nothing when source is unsupported.";
2806 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2807 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2808 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2809 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2810
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002811 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812 << "Should query mapper when source is supported.";
2813 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2814 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2815 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2816 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2817
2818 // Event handling.
2819 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002820 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 mDevice->process(&event, 1);
2822
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002823 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2824 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002825}
2826
Arthur Hung2c9a3342019-07-23 14:18:59 +08002827// A single input device is associated with a specific display. Check that:
2828// 1. Device is disabled if the viewport corresponding to the associated display is not found
2829// 2. Device is disabled when setEnabled API is called
2830TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002832
2833 // First Configuration.
2834 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2835
2836 // Device should be enabled by default.
2837 ASSERT_TRUE(mDevice->isEnabled());
2838
2839 // Prepare associated info.
2840 constexpr uint8_t hdmi = 1;
2841 const std::string UNIQUE_ID = "local:1";
2842
2843 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2844 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2845 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2846 // Device should be disabled because it is associated with a specific display via
2847 // input port <-> display port association, but the corresponding display is not found
2848 ASSERT_FALSE(mDevice->isEnabled());
2849
2850 // Prepare displays.
2851 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002852 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2853 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002854 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2855 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2856 ASSERT_TRUE(mDevice->isEnabled());
2857
2858 // Device should be disabled after set disable.
2859 mFakePolicy->addDisabledDevice(mDevice->getId());
2860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2861 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2862 ASSERT_FALSE(mDevice->isEnabled());
2863
2864 // Device should still be disabled even found the associated display.
2865 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2866 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2867 ASSERT_FALSE(mDevice->isEnabled());
2868}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Christine Franks1ba71cc2021-04-07 14:37:42 -07002870TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2871 // Device should be enabled by default.
2872 mFakePolicy->clearViewports();
2873 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2874 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2875 ASSERT_TRUE(mDevice->isEnabled());
2876
2877 // Device should be disabled because it is associated with a specific display, but the
2878 // corresponding display is not found.
2879 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002880 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884
2885 // Device should be enabled when a display is found.
2886 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2887 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2888 NO_PORT, ViewportType::INTERNAL);
2889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2891 ASSERT_TRUE(mDevice->isEnabled());
2892
2893 // Device should be disabled after set disable.
2894 mFakePolicy->addDisabledDevice(mDevice->getId());
2895 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2896 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2897 ASSERT_FALSE(mDevice->isEnabled());
2898
2899 // Device should still be disabled even found the associated display.
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 ASSERT_FALSE(mDevice->isEnabled());
2903}
2904
Christine Franks2a2293c2022-01-18 11:51:16 -08002905TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2906 mFakePolicy->clearViewports();
2907 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2908 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2909
2910 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2911 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2912 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2913 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2914 NO_PORT, ViewportType::INTERNAL);
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2916 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2917 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2918}
2919
Michael Wrightd02c5b62014-02-10 15:10:22 -08002920// --- InputMapperTest ---
2921
2922class InputMapperTest : public testing::Test {
2923protected:
2924 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002925 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 static const int32_t DEVICE_ID;
2927 static const int32_t DEVICE_GENERATION;
2928 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002929 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002930 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002932 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002934 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002935 std::unique_ptr<InstrumentedInputReader> mReader;
2936 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002938 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002939 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002940 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002941 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002942 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002943 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002944 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002945 // Consume the device reset notification generated when adding a new device.
2946 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 }
2948
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002949 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002950 SetUp(DEVICE_CLASSES);
2951 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952
Chris Yea52ade12020-08-27 16:49:20 -07002953 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002954 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 }
2957
2958 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002959 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 }
2961
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002962 void configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002963 if (!changes ||
2964 (changes &
2965 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2966 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002967 mReader->requestRefreshConfiguration(changes);
2968 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002969 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002970 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002971 // Loop the reader to flush the input listener queue.
2972 mReader->loopOnce();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002973 }
2974
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2976 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002977 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 InputDeviceIdentifier identifier;
2979 identifier.name = name;
2980 identifier.location = location;
2981 std::shared_ptr<InputDevice> device =
2982 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2983 identifier);
2984 mReader->pushNextDevice(device);
2985 mFakeEventHub->addDevice(eventHubId, name, classes);
2986 mReader->loopOnce();
2987 return device;
2988 }
2989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 template <class T, typename... Args>
2991 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002992 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002993 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002995 mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002996 // Loop the reader to flush the input listener queue.
2997 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002998 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 }
3000
3001 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003002 int32_t orientation, const std::string& uniqueId,
3003 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003004 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3005 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003006 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3007 }
3008
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003009 void clearViewports() {
3010 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 }
3012
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003013 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
3014 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 RawEvent event;
3016 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003018 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 event.type = type;
3020 event.code = code;
3021 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003022 mapper.process(&event);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003023 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003024 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003025 }
3026
3027 static void assertMotionRange(const InputDeviceInfo& info,
3028 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3029 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003030 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3032 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3033 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3034 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3035 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3036 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3037 }
3038
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003039 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3040 float size, float touchMajor, float touchMinor, float toolMajor,
3041 float toolMinor, float orientation, float distance,
3042 float scaledAxisEpsilon = 1.f) {
3043 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3044 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3046 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003047 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3048 scaledAxisEpsilon);
3049 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3050 scaledAxisEpsilon);
3051 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3052 scaledAxisEpsilon);
3053 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3054 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3056 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3057 }
3058
Michael Wright17db18e2020-06-26 20:51:44 +01003059 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003061 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062 ASSERT_NEAR(x, actualX, 1);
3063 ASSERT_NEAR(y, actualY, 1);
3064 }
3065};
3066
3067const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003068const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003069const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3071const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003072const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3073 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003074const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075
3076// --- SwitchInputMapperTest ---
3077
3078class SwitchInputMapperTest : public InputMapperTest {
3079protected:
3080};
3081
3082TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003083 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003085 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086}
3087
3088TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003091 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003092 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003094 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003095 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003096}
3097
3098TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003099 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105
3106 NotifySwitchArgs args;
3107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3108 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003109 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3110 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111 args.switchMask);
3112 ASSERT_EQ(uint32_t(0), args.policyFlags);
3113}
3114
Chris Ye87143712020-11-10 05:05:58 +00003115// --- VibratorInputMapperTest ---
3116class VibratorInputMapperTest : public InputMapperTest {
3117protected:
3118 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3119};
3120
3121TEST_F(VibratorInputMapperTest, GetSources) {
3122 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3123
3124 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3125}
3126
3127TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3128 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3129
3130 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3131}
3132
3133TEST_F(VibratorInputMapperTest, Vibrate) {
3134 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003135 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003136 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3137
3138 VibrationElement pattern(2);
3139 VibrationSequence sequence(2);
3140 pattern.duration = std::chrono::milliseconds(200);
3141 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3142 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3143 sequence.addElement(pattern);
3144 pattern.duration = std::chrono::milliseconds(500);
3145 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3146 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3147 sequence.addElement(pattern);
3148
3149 std::vector<int64_t> timings = {0, 1};
3150 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3151
3152 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003153 // Start vibrating
3154 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003155 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003156 // Verify vibrator state listener was notified.
3157 mReader->loopOnce();
3158 NotifyVibratorStateArgs args;
3159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3160 ASSERT_EQ(DEVICE_ID, args.deviceId);
3161 ASSERT_TRUE(args.isOn);
3162 // Stop vibrating
3163 mapper.cancelVibrate(VIBRATION_TOKEN);
3164 ASSERT_FALSE(mapper.isVibrating());
3165 // Verify vibrator state listener was notified.
3166 mReader->loopOnce();
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3168 ASSERT_EQ(DEVICE_ID, args.deviceId);
3169 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003170}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171
Chris Yef59a2f42020-10-16 12:55:26 -07003172// --- SensorInputMapperTest ---
3173
3174class SensorInputMapperTest : public InputMapperTest {
3175protected:
3176 static const int32_t ACCEL_RAW_MIN;
3177 static const int32_t ACCEL_RAW_MAX;
3178 static const int32_t ACCEL_RAW_FUZZ;
3179 static const int32_t ACCEL_RAW_FLAT;
3180 static const int32_t ACCEL_RAW_RESOLUTION;
3181
3182 static const int32_t GYRO_RAW_MIN;
3183 static const int32_t GYRO_RAW_MAX;
3184 static const int32_t GYRO_RAW_FUZZ;
3185 static const int32_t GYRO_RAW_FLAT;
3186 static const int32_t GYRO_RAW_RESOLUTION;
3187
3188 static const float GRAVITY_MS2_UNIT;
3189 static const float DEGREE_RADIAN_UNIT;
3190
3191 void prepareAccelAxes();
3192 void prepareGyroAxes();
3193 void setAccelProperties();
3194 void setGyroProperties();
3195 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3196};
3197
3198const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3199const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3200const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3201const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3202const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3203
3204const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3205const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3206const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3207const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3208const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3209
3210const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3211const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3212
3213void SensorInputMapperTest::prepareAccelAxes() {
3214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3215 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3216 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3217 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3218 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3219 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3220}
3221
3222void SensorInputMapperTest::prepareGyroAxes() {
3223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3224 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3226 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3228 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3229}
3230
3231void SensorInputMapperTest::setAccelProperties() {
3232 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3233 /* sensorDataIndex */ 0);
3234 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3235 /* sensorDataIndex */ 1);
3236 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3237 /* sensorDataIndex */ 2);
3238 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3239 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3240 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3241 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3242 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3243}
3244
3245void SensorInputMapperTest::setGyroProperties() {
3246 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3247 /* sensorDataIndex */ 0);
3248 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3249 /* sensorDataIndex */ 1);
3250 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3251 /* sensorDataIndex */ 2);
3252 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3253 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3254 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3255 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3256 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3257}
3258
3259TEST_F(SensorInputMapperTest, GetSources) {
3260 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3261
3262 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3263}
3264
3265TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3266 setAccelProperties();
3267 prepareAccelAxes();
3268 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3269
3270 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3271 std::chrono::microseconds(10000),
3272 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003273 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003279
3280 NotifySensorArgs args;
3281 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3282 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3283 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3284
3285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3286 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3287 ASSERT_EQ(args.deviceId, DEVICE_ID);
3288 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3289 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3290 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3291 ASSERT_EQ(args.values, values);
3292 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3293}
3294
3295TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3296 setGyroProperties();
3297 prepareGyroAxes();
3298 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3299
3300 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3301 std::chrono::microseconds(10000),
3302 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003303 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003309
3310 NotifySensorArgs args;
3311 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3312 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3313 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3314
3315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3316 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3317 ASSERT_EQ(args.deviceId, DEVICE_ID);
3318 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3319 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3320 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3321 ASSERT_EQ(args.values, values);
3322 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3323}
3324
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325// --- KeyboardInputMapperTest ---
3326
3327class KeyboardInputMapperTest : public InputMapperTest {
3328protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003329 const std::string UNIQUE_ID = "local:0";
3330
3331 void prepareDisplay(int32_t orientation);
3332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003334 int32_t originalKeyCode, int32_t rotatedKeyCode,
3335 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336};
3337
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003338/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3339 * orientation.
3340 */
3341void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003342 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3343 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344}
3345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003347 int32_t originalScanCode, int32_t originalKeyCode,
3348 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 NotifyKeyArgs args;
3350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3354 ASSERT_EQ(originalScanCode, args.scanCode);
3355 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3360 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3361 ASSERT_EQ(originalScanCode, args.scanCode);
3362 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003363 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 KeyboardInputMapper& mapper =
3368 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3369 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372}
3373
3374TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3375 const int32_t USAGE_A = 0x070004;
3376 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003377 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3378 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003379 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3380 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3381 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003383 KeyboardInputMapper& mapper =
3384 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3385 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003386 // Initial metastate is AMETA_NONE.
3387 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 NotifyKeyArgs args;
3392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3393 ASSERT_EQ(DEVICE_ID, args.deviceId);
3394 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3395 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3397 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3398 ASSERT_EQ(KEY_HOME, args.scanCode);
3399 ASSERT_EQ(AMETA_NONE, args.metaState);
3400 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3401 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3402 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3403
3404 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3407 ASSERT_EQ(DEVICE_ID, args.deviceId);
3408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3409 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3410 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3411 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3412 ASSERT_EQ(KEY_HOME, args.scanCode);
3413 ASSERT_EQ(AMETA_NONE, args.metaState);
3414 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3415 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3416 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3417
3418 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(DEVICE_ID, args.deviceId);
3423 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3424 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3425 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3426 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3427 ASSERT_EQ(0, args.scanCode);
3428 ASSERT_EQ(AMETA_NONE, args.metaState);
3429 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3430 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3431 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3432
3433 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3435 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3437 ASSERT_EQ(DEVICE_ID, args.deviceId);
3438 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3439 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3440 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3441 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3442 ASSERT_EQ(0, args.scanCode);
3443 ASSERT_EQ(AMETA_NONE, args.metaState);
3444 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3445 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3446 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3447
3448 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(DEVICE_ID, args.deviceId);
3453 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3454 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3456 ASSERT_EQ(0, args.keyCode);
3457 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3458 ASSERT_EQ(AMETA_NONE, args.metaState);
3459 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3460 ASSERT_EQ(0U, args.policyFlags);
3461 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3462
3463 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3465 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3467 ASSERT_EQ(DEVICE_ID, args.deviceId);
3468 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3469 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3471 ASSERT_EQ(0, args.keyCode);
3472 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3473 ASSERT_EQ(AMETA_NONE, args.metaState);
3474 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3475 ASSERT_EQ(0U, args.policyFlags);
3476 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3477}
3478
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479/**
3480 * Ensure that the readTime is set to the time when the EV_KEY is received.
3481 */
3482TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3483 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3484
3485 KeyboardInputMapper& mapper =
3486 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3487 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3488 NotifyKeyArgs args;
3489
3490 // Key down
3491 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3493 ASSERT_EQ(12, args.readTime);
3494
3495 // Key up
3496 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3498 ASSERT_EQ(15, args.readTime);
3499}
3500
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3503 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003504 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3505 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 KeyboardInputMapper& mapper =
3509 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3510 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
Arthur Hung95f68612022-04-07 14:08:22 +08003512 // Initial metastate is AMETA_NONE.
3513 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
3515 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517 NotifyKeyArgs args;
3518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3519 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003521 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
3523 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003524 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3526 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
3529 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003530 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534
3535 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003536 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3538 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003539 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003540 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541}
3542
3543TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3546 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 KeyboardInputMapper& mapper =
3550 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3551 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003553 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3555 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3557 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3558 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3559 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3560 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3561 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3562}
3563
3564TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003565 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3566 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003571 KeyboardInputMapper& mapper =
3572 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3573 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003575 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576 ASSERT_NO_FATAL_FAILURE(
3577 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3579 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3581 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3583 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 clearViewports();
3586 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 ASSERT_NO_FATAL_FAILURE(
3588 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3590 AKEYCODE_DPAD_UP, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3592 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3594 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003596 clearViewports();
3597 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_UP, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 clearViewports();
3608 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 ASSERT_NO_FATAL_FAILURE(
3610 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3612 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3614 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3615 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3616 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003617
3618 // Special case: if orientation changes while key is down, we still emit the same keycode
3619 // in the key up as we did in the key down.
3620 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003621 clearViewports();
3622 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3626 ASSERT_EQ(KEY_UP, args.scanCode);
3627 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3628
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003629 clearViewports();
3630 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3633 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3634 ASSERT_EQ(KEY_UP, args.scanCode);
3635 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3636}
3637
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003638TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3639 // If the keyboard is not orientation aware,
3640 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003643 KeyboardInputMapper& mapper =
3644 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 NotifyKeyArgs args;
3647
3648 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3653 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3654
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003655 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3660 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3661}
3662
3663TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3664 // If the keyboard is orientation aware,
3665 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
3670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003672 NotifyKeyArgs args;
3673
3674 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3675 // ^--- already checked by the previous test
3676
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003677 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003678 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3683 ASSERT_EQ(DISPLAY_ID, args.displayId);
3684
3685 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003686 clearViewports();
3687 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003688 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(newDisplayId, args.displayId);
3694}
3695
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003697 KeyboardInputMapper& mapper =
3698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3699 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706}
3707
Philip Junker4af3b3d2021-12-14 10:36:55 +01003708TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3709 KeyboardInputMapper& mapper =
3710 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3711 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3712
3713 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3714 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3715 << "If a mapping is available, the result is equal to the mapping";
3716
3717 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3718 << "If no mapping is available, the result is the key location";
3719}
3720
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 KeyboardInputMapper& mapper =
3723 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3724 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003730 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731}
3732
3733TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 KeyboardInputMapper& mapper =
3735 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3736 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003738 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003741 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 ASSERT_TRUE(flags[0]);
3743 ASSERT_FALSE(flags[1]);
3744}
3745
3746TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003747 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3748 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3749 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3750 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 KeyboardInputMapper& mapper =
3755 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3756 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003757 // Initial metastate is AMETA_NONE.
3758 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
3789 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3794 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
3805 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3809 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812}
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3817 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3819
3820 KeyboardInputMapper& mapper =
3821 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3822 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3823
Chris Yea52ade12020-08-27 16:49:20 -07003824 // Meta state should be AMETA_NONE after reset
3825 mapper.reset(ARBITRARY_TIME);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3828 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3829 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3830
3831 NotifyKeyArgs args;
3832 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3835 ASSERT_EQ(AMETA_NONE, args.metaState);
3836 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3837 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3838 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3839
3840 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(AMETA_NONE, args.metaState);
3844 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3845 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3846 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3847}
3848
Arthur Hung2c9a3342019-07-23 14:18:59 +08003849TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3850 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855
3856 // keyboard 2.
3857 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003858 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003860 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003861 std::shared_ptr<InputDevice> device2 =
3862 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003863 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003865 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3866 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3867 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3868 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 KeyboardInputMapper& mapper =
3871 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3872 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003873
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003874 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003875 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003877 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3878 device2->reset(ARBITRARY_TIME);
3879
3880 // Prepared displays and associated info.
3881 constexpr uint8_t hdmi1 = 0;
3882 constexpr uint8_t hdmi2 = 1;
3883 const std::string SECONDARY_UNIQUE_ID = "local:1";
3884
3885 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3886 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3887
3888 // No associated display viewport found, should disable the device.
3889 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3890 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3891 ASSERT_FALSE(device2->isEnabled());
3892
3893 // Prepare second display.
3894 constexpr int32_t newDisplayId = 2;
3895 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003896 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003897 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003898 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003899 // Default device will reconfigure above, need additional reconfiguration for another device.
3900 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3902
3903 // Device should be enabled after the associated display is found.
3904 ASSERT_TRUE(mDevice->isEnabled());
3905 ASSERT_TRUE(device2->isEnabled());
3906
3907 // Test pad key events
3908 ASSERT_NO_FATAL_FAILURE(
3909 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3911 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3912 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3913 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3914 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3915 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3916
3917 ASSERT_NO_FATAL_FAILURE(
3918 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3919 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3920 AKEYCODE_DPAD_RIGHT, newDisplayId));
3921 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3922 AKEYCODE_DPAD_DOWN, newDisplayId));
3923 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3924 AKEYCODE_DPAD_LEFT, newDisplayId));
3925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
arthurhungc903df12020-08-11 15:08:42 +08003927TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3928 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3929 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3930 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3931 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3932 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3934
3935 KeyboardInputMapper& mapper =
3936 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3937 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003938 // Initial metastate is AMETA_NONE.
3939 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003940
3941 // Initialization should have turned all of the lights off.
3942 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3943 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3945
3946 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003949 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3950 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3951
3952 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003955 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3956 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3957
3958 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003961 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3962 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3963
3964 mFakeEventHub->removeDevice(EVENTHUB_ID);
3965 mReader->loopOnce();
3966
3967 // keyboard 2 should default toggle keys.
3968 const std::string USB2 = "USB2";
3969 const std::string DEVICE_NAME2 = "KEYBOARD2";
3970 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3971 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3972 std::shared_ptr<InputDevice> device2 =
3973 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003974 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003975 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3976 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3977 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3978 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3979 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3980 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3981
arthurhung6fe95782020-10-05 22:41:16 +08003982 KeyboardInputMapper& mapper2 =
3983 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3984 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003985 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3986 device2->reset(ARBITRARY_TIME);
3987
3988 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3989 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3990 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003991 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3992 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003993}
3994
Arthur Hungcb40a002021-08-03 14:31:01 +00003995TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3997 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3998 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3999
4000 // Suppose we have two mappers. (DPAD + KEYBOARD)
4001 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4002 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4003 KeyboardInputMapper& mapper =
4004 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4005 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004006 // Initial metastate is AMETA_NONE.
4007 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004008
4009 mReader->toggleCapsLockState(DEVICE_ID);
4010 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4011}
4012
Arthur Hungfb3cc112022-04-13 07:39:50 +00004013TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4014 // keyboard 1.
4015 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4016 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4017 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4021
4022 KeyboardInputMapper& mapper1 =
4023 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4024 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4025
4026 // keyboard 2.
4027 const std::string USB2 = "USB2";
4028 const std::string DEVICE_NAME2 = "KEYBOARD2";
4029 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4030 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4031 std::shared_ptr<InputDevice> device2 =
4032 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4033 ftl::Flags<InputDeviceClass>(0));
4034 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4035 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4036 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4037 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4038 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4039 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4040
4041 KeyboardInputMapper& mapper2 =
4042 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4043 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4044 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
4045 device2->reset(ARBITRARY_TIME);
4046
Arthur Hung95f68612022-04-07 14:08:22 +08004047 // Initial metastate is AMETA_NONE.
4048 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4049 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4050
4051 // Toggle num lock on and off.
4052 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4053 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004054 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4055 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4056 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4057
4058 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4059 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4060 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4061 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4062 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4063
4064 // Toggle caps lock on and off.
4065 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4066 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4067 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4068 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4069 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4070
4071 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4072 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4073 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4074 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4075 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4076
4077 // Toggle scroll lock on and off.
4078 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4079 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4080 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4081 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4082 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4083
4084 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4085 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4086 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4087 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4088 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4089}
4090
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004091// --- KeyboardInputMapperTest_ExternalDevice ---
4092
4093class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4094protected:
Chris Yea52ade12020-08-27 16:49:20 -07004095 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004096};
4097
4098TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004099 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4100 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004101
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4103 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4104 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4105 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 KeyboardInputMapper& mapper =
4108 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4109 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004110
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004112 NotifyKeyArgs args;
4113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4114 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4115
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4118 ASSERT_EQ(uint32_t(0), args.policyFlags);
4119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
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 + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4126 ASSERT_EQ(uint32_t(0), args.policyFlags);
4127
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4131
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004132 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4134 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4135}
4136
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004137TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004138 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4141 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4142 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Powei Fengd041c5d2019-05-03 17:11:33 -07004144 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004145 KeyboardInputMapper& mapper =
4146 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4147 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 NotifyKeyArgs args;
4151 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 + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
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 + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(uint32_t(0), args.policyFlags);
4165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4169
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004170 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4172 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4173}
4174
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175// --- CursorInputMapperTest ---
4176
4177class CursorInputMapperTest : public InputMapperTest {
4178protected:
4179 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4180
Michael Wright17db18e2020-06-26 20:51:44 +01004181 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
Chris Yea52ade12020-08-27 16:49:20 -07004183 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184 InputMapperTest::SetUp();
4185
Michael Wright17db18e2020-06-26 20:51:44 +01004186 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004187 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 }
4189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004190 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4191 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004192
4193 void prepareDisplay(int32_t orientation) {
4194 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004195 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004196 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4197 orientation, uniqueId, NO_PORT, viewportType);
4198 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199
4200 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4201 float pressure) {
4202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4203 0.0f, 0.0f, 0.0f, EPSILON));
4204 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205};
4206
4207const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4208
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004209void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4210 int32_t originalY, int32_t rotatedX,
4211 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 NotifyMotionArgs args;
4213
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004219 ASSERT_NO_FATAL_FAILURE(
4220 assertCursorPointerCoords(args.pointerCoords[0],
4221 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4222 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223}
4224
4225TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004227 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230}
4231
4232TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237}
4238
4239TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004241 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
4243 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004244 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
4246 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004247 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4248 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4250 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4251
4252 // When the bounds are set, then there should be a valid motion range.
4253 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4254
4255 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257
4258 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4259 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4260 1, 800 - 1, 0.0f, 0.0f));
4261 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4262 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4263 2, 480 - 1, 0.0f, 0.0f));
4264 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4265 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4266 0.0f, 1.0f, 0.0f, 0.0f));
4267}
4268
4269TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004272
4273 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004274 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275
4276 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4277 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4278 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4279 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4280 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4281 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4282 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4283 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4284 0.0f, 1.0f, 0.0f, 0.0f));
4285}
4286
4287TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290
arthurhungdcef2dc2020-08-11 14:47:50 +08004291 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
4293 NotifyMotionArgs args;
4294
4295 // Button press.
4296 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4300 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4301 ASSERT_EQ(DEVICE_ID, args.deviceId);
4302 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4303 ASSERT_EQ(uint32_t(0), args.policyFlags);
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4305 ASSERT_EQ(0, args.flags);
4306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4307 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4308 ASSERT_EQ(0, args.edgeFlags);
4309 ASSERT_EQ(uint32_t(1), args.pointerCount);
4310 ASSERT_EQ(0, args.pointerProperties[0].id);
4311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4314 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4315 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4316
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4318 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4319 ASSERT_EQ(DEVICE_ID, args.deviceId);
4320 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4321 ASSERT_EQ(uint32_t(0), args.policyFlags);
4322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4323 ASSERT_EQ(0, args.flags);
4324 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4325 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4326 ASSERT_EQ(0, args.edgeFlags);
4327 ASSERT_EQ(uint32_t(1), args.pointerCount);
4328 ASSERT_EQ(0, args.pointerProperties[0].id);
4329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004331 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4332 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4333 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4334
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4337 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4339 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4340 ASSERT_EQ(DEVICE_ID, args.deviceId);
4341 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4342 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4344 ASSERT_EQ(0, args.flags);
4345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4346 ASSERT_EQ(0, args.buttonState);
4347 ASSERT_EQ(0, args.edgeFlags);
4348 ASSERT_EQ(uint32_t(1), args.pointerCount);
4349 ASSERT_EQ(0, args.pointerProperties[0].id);
4350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004352 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4354 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4355
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4357 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4358 ASSERT_EQ(DEVICE_ID, args.deviceId);
4359 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4360 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4362 ASSERT_EQ(0, args.flags);
4363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4364 ASSERT_EQ(0, args.buttonState);
4365 ASSERT_EQ(0, args.edgeFlags);
4366 ASSERT_EQ(uint32_t(1), args.pointerCount);
4367 ASSERT_EQ(0, args.pointerProperties[0].id);
4368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004369 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4371 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4372 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4373}
4374
4375TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004377 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378
4379 NotifyMotionArgs args;
4380
4381 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004386 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4387 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4388 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389
4390 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4396 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397}
4398
4399TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004401 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
4403 NotifyMotionArgs args;
4404
4405 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4409 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004410 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004414 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004421 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004425 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426}
4427
4428TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
4432 NotifyMotionArgs args;
4433
4434 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004441 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4442 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4443 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4446 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4448 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4449 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004450
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004457 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4458 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4459 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460
4461 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004470 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471}
4472
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004473TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004475 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4476 // need to be rotated.
4477 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004478 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004480 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 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 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4486 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4487 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4488 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4489}
4490
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004491TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004493 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4494 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004495 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004497 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 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 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4503 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4506
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004507 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004508 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));
4512 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4513 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4514 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4515 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004517 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518 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 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4526
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004527 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004528 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));
4532 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4533 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536}
4537
4538TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
4542 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4543 mFakePointerController->setPosition(100, 200);
4544 mFakePointerController->setButtonState(0);
4545
4546 NotifyMotionArgs motionArgs;
4547 NotifyKeyArgs keyArgs;
4548
4549 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4554 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4555 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(
4557 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4560 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4561 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4562 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004563 ASSERT_NO_FATAL_FAILURE(
4564 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004565
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_EQ(0, motionArgs.buttonState);
4571 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(
4573 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004576 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 ASSERT_EQ(0, motionArgs.buttonState);
4578 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004579 ASSERT_NO_FATAL_FAILURE(
4580 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004581
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584 ASSERT_EQ(0, motionArgs.buttonState);
4585 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004586 ASSERT_NO_FATAL_FAILURE(
4587 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
4589 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4595 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4596 motionArgs.buttonState);
4597 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4598 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004599 ASSERT_NO_FATAL_FAILURE(
4600 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4604 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4605 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4606 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004607 ASSERT_NO_FATAL_FAILURE(
4608 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004609
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4612 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4613 motionArgs.buttonState);
4614 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4615 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004616 ASSERT_NO_FATAL_FAILURE(
4617 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4624 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4631 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(
4633 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004634
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4639 ASSERT_EQ(0, motionArgs.buttonState);
4640 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004641 ASSERT_NO_FATAL_FAILURE(
4642 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 ASSERT_EQ(0, motionArgs.buttonState);
4648 ASSERT_EQ(0, mFakePointerController->getButtonState());
4649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4654 ASSERT_EQ(0, motionArgs.buttonState);
4655 ASSERT_EQ(0, mFakePointerController->getButtonState());
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004657 ASSERT_NO_FATAL_FAILURE(
4658 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659
4660 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4664 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4665 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004666
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4670 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004671 ASSERT_NO_FATAL_FAILURE(
4672 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004673
4674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4676 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4677 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004678 ASSERT_NO_FATAL_FAILURE(
4679 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_EQ(0, motionArgs.buttonState);
4686 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004687 ASSERT_NO_FATAL_FAILURE(
4688 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004689
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692 ASSERT_EQ(0, motionArgs.buttonState);
4693 ASSERT_EQ(0, mFakePointerController->getButtonState());
4694
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4698 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4699 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4700
4701 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4705 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4706 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004707
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4711 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004712 ASSERT_NO_FATAL_FAILURE(
4713 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4717 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4718 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004719 ASSERT_NO_FATAL_FAILURE(
4720 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_EQ(0, motionArgs.buttonState);
4727 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(
4729 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730
4731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4733 ASSERT_EQ(0, motionArgs.buttonState);
4734 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004735 ASSERT_NO_FATAL_FAILURE(
4736 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4739 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4740 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4741
4742 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4746 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4747 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004750 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4752 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004753 ASSERT_NO_FATAL_FAILURE(
4754 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004755
4756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4758 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4759 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004760 ASSERT_NO_FATAL_FAILURE(
4761 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_EQ(0, motionArgs.buttonState);
4768 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004769 ASSERT_NO_FATAL_FAILURE(
4770 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4774 ASSERT_EQ(0, motionArgs.buttonState);
4775 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004776 ASSERT_NO_FATAL_FAILURE(
4777 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004778
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4780 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4781 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4782
4783 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4787 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4788 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004789
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4793 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004794 ASSERT_NO_FATAL_FAILURE(
4795 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004796
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4799 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4800 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004801 ASSERT_NO_FATAL_FAILURE(
4802 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004807 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004808 ASSERT_EQ(0, motionArgs.buttonState);
4809 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004810 ASSERT_NO_FATAL_FAILURE(
4811 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004812
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4815 ASSERT_EQ(0, motionArgs.buttonState);
4816 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004817 ASSERT_NO_FATAL_FAILURE(
4818 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004819
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4821 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4822 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4823}
4824
4825TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004827 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828
4829 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4830 mFakePointerController->setPosition(100, 200);
4831 mFakePointerController->setButtonState(0);
4832
4833 NotifyMotionArgs args;
4834
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004835 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004839 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4842 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 +01004843 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004844}
4845
4846TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004847 addConfigurationProperty("cursor.mode", "pointer");
4848 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004850
4851 NotifyDeviceResetArgs resetArgs;
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4853 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4854 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4855
4856 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4857 mFakePointerController->setPosition(100, 200);
4858 mFakePointerController->setButtonState(0);
4859
4860 NotifyMotionArgs args;
4861
4862 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4867 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4870 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 +01004871 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004872
4873 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4877 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4880 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4882 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4885 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4886
4887 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4889 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4891 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4894 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4896 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4899 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4900
4901 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4906 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4909 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 +01004910 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004911
4912 // Disable pointer capture and check that the device generation got bumped
4913 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004914 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004915 mFakePolicy->setPointerCapture(false);
4916 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004917 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004920 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4921
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4926 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4929 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 +01004930 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931}
4932
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004933/**
4934 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4935 * pointer acceleration or speed processing should not be applied.
4936 */
4937TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4938 addConfigurationProperty("cursor.mode", "pointer");
4939 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4940 100.f /*high threshold*/, 10.f /*acceleration*/);
4941 mFakePolicy->setVelocityControlParams(testParams);
4942 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4943
4944 NotifyDeviceResetArgs resetArgs;
4945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4946 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4947 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4948
4949 NotifyMotionArgs args;
4950
4951 // Move and verify scale is applied.
4952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4956 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4957 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4958 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4959 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4960 ASSERT_GT(relX, 10);
4961 ASSERT_GT(relY, 20);
4962
4963 // Enable Pointer Capture
4964 mFakePolicy->setPointerCapture(true);
4965 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4966 NotifyPointerCaptureChangedArgs captureArgs;
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4968 ASSERT_TRUE(captureArgs.request.enable);
4969
4970 // Move and verify scale is not applied.
4971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4975 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4977 ASSERT_EQ(10, args.pointerCoords[0].getX());
4978 ASSERT_EQ(20, args.pointerCoords[0].getY());
4979}
4980
Prabir Pradhan208360b2022-06-24 18:37:04 +00004981TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4982 addConfigurationProperty("cursor.mode", "pointer");
4983 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4984
4985 NotifyDeviceResetArgs resetArgs;
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4987 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4988 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4989
4990 // Ensure the display is rotated.
4991 prepareDisplay(DISPLAY_ORIENTATION_90);
4992
4993 NotifyMotionArgs args;
4994
4995 // Verify that the coordinates are rotated.
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5000 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5002 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5003 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5004
5005 // Enable Pointer Capture.
5006 mFakePolicy->setPointerCapture(true);
5007 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5008 NotifyPointerCaptureChangedArgs captureArgs;
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5010 ASSERT_TRUE(captureArgs.request.enable);
5011
5012 // Move and verify rotation is not applied.
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5017 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5019 ASSERT_EQ(10, args.pointerCoords[0].getX());
5020 ASSERT_EQ(20, args.pointerCoords[0].getY());
5021}
5022
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005023TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005024 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005025
Garfield Tan888a6a42020-01-09 11:39:16 -08005026 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005027 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08005028 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
5029 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00005030 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
5031 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08005032 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
5033 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5034
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005035 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5036 mFakePointerController->setPosition(100, 200);
5037 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005038
5039 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5044 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5045 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5047 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 +01005048 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005049 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
5050}
5051
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052// --- TouchInputMapperTest ---
5053
5054class TouchInputMapperTest : public InputMapperTest {
5055protected:
5056 static const int32_t RAW_X_MIN;
5057 static const int32_t RAW_X_MAX;
5058 static const int32_t RAW_Y_MIN;
5059 static const int32_t RAW_Y_MAX;
5060 static const int32_t RAW_TOUCH_MIN;
5061 static const int32_t RAW_TOUCH_MAX;
5062 static const int32_t RAW_TOOL_MIN;
5063 static const int32_t RAW_TOOL_MAX;
5064 static const int32_t RAW_PRESSURE_MIN;
5065 static const int32_t RAW_PRESSURE_MAX;
5066 static const int32_t RAW_ORIENTATION_MIN;
5067 static const int32_t RAW_ORIENTATION_MAX;
5068 static const int32_t RAW_DISTANCE_MIN;
5069 static const int32_t RAW_DISTANCE_MAX;
5070 static const int32_t RAW_TILT_MIN;
5071 static const int32_t RAW_TILT_MAX;
5072 static const int32_t RAW_ID_MIN;
5073 static const int32_t RAW_ID_MAX;
5074 static const int32_t RAW_SLOT_MIN;
5075 static const int32_t RAW_SLOT_MAX;
5076 static const float X_PRECISION;
5077 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005078 static const float X_PRECISION_VIRTUAL;
5079 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080
5081 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005082 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083
5084 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005086 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005087 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005088
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 enum Axes {
5090 POSITION = 1 << 0,
5091 TOUCH = 1 << 1,
5092 TOOL = 1 << 2,
5093 PRESSURE = 1 << 3,
5094 ORIENTATION = 1 << 4,
5095 MINOR = 1 << 5,
5096 ID = 1 << 6,
5097 DISTANCE = 1 << 7,
5098 TILT = 1 << 8,
5099 SLOT = 1 << 9,
5100 TOOL_TYPE = 1 << 10,
5101 };
5102
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005103 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5104 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005105 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005107 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 int32_t toRawX(float displayX);
5109 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005110 int32_t toRotatedRawX(float displayX);
5111 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005112 float toCookedX(float rawX, float rawY);
5113 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005115 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005117 float toDisplayY(int32_t rawY, int32_t displayHeight);
5118
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119};
5120
5121const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5122const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5123const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5124const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5125const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5126const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5127const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5128const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005129const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5130const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5132const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5133const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5134const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5135const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5136const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5137const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5138const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5139const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5140const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5141const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5142const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005143const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5144 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5145const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5146 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005147const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5148 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149
5150const float TouchInputMapperTest::GEOMETRIC_SCALE =
5151 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5152 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5153
5154const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5155 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5156 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5157};
5158
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005159void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005160 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5161 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005162}
5163
5164void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5165 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5166 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
Santos Cordonfa5cf462017-04-05 10:37:00 -07005169void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005170 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5171 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5172 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005173}
5174
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005176 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5177 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5178 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5179 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
Jason Gerecke489fda82012-09-07 17:19:40 -07005182void TouchInputMapperTest::prepareLocationCalibration() {
5183 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5184}
5185
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186int32_t TouchInputMapperTest::toRawX(float displayX) {
5187 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5188}
5189
5190int32_t TouchInputMapperTest::toRawY(float displayY) {
5191 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5192}
5193
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005194int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5195 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5196}
5197
5198int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5199 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5200}
5201
Jason Gerecke489fda82012-09-07 17:19:40 -07005202float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5203 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5204 return rawX;
5205}
5206
5207float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5208 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5209 return rawY;
5210}
5211
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005213 return toDisplayX(rawX, DISPLAY_WIDTH);
5214}
5215
5216float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5217 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218}
5219
5220float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005221 return toDisplayY(rawY, DISPLAY_HEIGHT);
5222}
5223
5224float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5225 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226}
5227
5228
5229// --- SingleTouchInputMapperTest ---
5230
5231class SingleTouchInputMapperTest : public TouchInputMapperTest {
5232protected:
5233 void prepareButtons();
5234 void prepareAxes(int axes);
5235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005236 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5237 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5238 void processUp(SingleTouchInputMapper& mappery);
5239 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5240 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5241 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5242 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5243 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5244 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005245};
5246
5247void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005248 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249}
5250
5251void SingleTouchInputMapperTest::prepareAxes(int axes) {
5252 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005253 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5254 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255 }
5256 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005257 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5258 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259 }
5260 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005261 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5262 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263 }
5264 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005265 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5266 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 }
5268 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005269 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 }
5272}
5273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005274void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278}
5279
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005280void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283}
5284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005285void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005287}
5288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005289void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291}
5292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005293void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5294 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296}
5297
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005298void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300}
5301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005302void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5303 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005304 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306}
5307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005308void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5309 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311}
5312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315}
5316
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 prepareButtons();
5319 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005322 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323}
5324
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 prepareButtons();
5327 prepareAxes(POSITION);
5328 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005331 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332}
5333
5334TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335 addConfigurationProperty("touch.deviceType", "touchScreen");
5336 prepareDisplay(DISPLAY_ORIENTATION_0);
5337 prepareButtons();
5338 prepareAxes(POSITION);
5339 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005340 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341
5342 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005343 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344
5345 // Virtual key is down.
5346 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5347 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5348 processDown(mapper, x, y);
5349 processSync(mapper);
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005352 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353
5354 // Virtual key is up.
5355 processUp(mapper);
5356 processSync(mapper);
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005359 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005360}
5361
5362TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 addConfigurationProperty("touch.deviceType", "touchScreen");
5364 prepareDisplay(DISPLAY_ORIENTATION_0);
5365 prepareButtons();
5366 prepareAxes(POSITION);
5367 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005368 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369
5370 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005371 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372
5373 // Virtual key is down.
5374 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5375 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5376 processDown(mapper, x, y);
5377 processSync(mapper);
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5379
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005380 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381
5382 // Virtual key is up.
5383 processUp(mapper);
5384 processSync(mapper);
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5386
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005387 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388}
5389
5390TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 addConfigurationProperty("touch.deviceType", "touchScreen");
5392 prepareDisplay(DISPLAY_ORIENTATION_0);
5393 prepareButtons();
5394 prepareAxes(POSITION);
5395 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005396 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005399 ASSERT_TRUE(
5400 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 ASSERT_TRUE(flags[0]);
5402 ASSERT_FALSE(flags[1]);
5403}
5404
5405TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 addConfigurationProperty("touch.deviceType", "touchScreen");
5407 prepareDisplay(DISPLAY_ORIENTATION_0);
5408 prepareButtons();
5409 prepareAxes(POSITION);
5410 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005411 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
arthurhungdcef2dc2020-08-11 14:47:50 +08005413 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414
5415 NotifyKeyArgs args;
5416
5417 // Press virtual key.
5418 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5419 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5420 processDown(mapper, x, y);
5421 processSync(mapper);
5422
5423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5424 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5425 ASSERT_EQ(DEVICE_ID, args.deviceId);
5426 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5427 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5428 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5429 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5430 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5431 ASSERT_EQ(KEY_HOME, args.scanCode);
5432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5433 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5434
5435 // Release virtual key.
5436 processUp(mapper);
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_UP, 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 // Should not have sent any motions.
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5453}
5454
5455TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 addConfigurationProperty("touch.deviceType", "touchScreen");
5457 prepareDisplay(DISPLAY_ORIENTATION_0);
5458 prepareButtons();
5459 prepareAxes(POSITION);
5460 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005461 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462
arthurhungdcef2dc2020-08-11 14:47:50 +08005463 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464
5465 NotifyKeyArgs keyArgs;
5466
5467 // Press virtual key.
5468 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5469 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5470 processDown(mapper, x, y);
5471 processSync(mapper);
5472
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5474 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5475 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5476 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5477 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5478 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5479 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5480 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5481 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5483 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5484
5485 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5486 // into the display area.
5487 y -= 100;
5488 processMove(mapper, x, y);
5489 processSync(mapper);
5490
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5492 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5493 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5494 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5495 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5496 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5497 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5498 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5499 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5500 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5502 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5503
5504 NotifyMotionArgs motionArgs;
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5506 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5507 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.flags);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513 ASSERT_EQ(0, motionArgs.buttonState);
5514 ASSERT_EQ(0, motionArgs.edgeFlags);
5515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5519 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5520 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5521 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5523
5524 // Keep moving out of bounds. Should generate a pointer move.
5525 y -= 50;
5526 processMove(mapper, x, y);
5527 processSync(mapper);
5528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5531 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5532 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5533 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5535 ASSERT_EQ(0, motionArgs.flags);
5536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5537 ASSERT_EQ(0, motionArgs.buttonState);
5538 ASSERT_EQ(0, motionArgs.edgeFlags);
5539 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5540 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5543 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5544 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5545 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5546 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5547
5548 // Release out of bounds. Should generate a pointer up.
5549 processUp(mapper);
5550 processSync(mapper);
5551
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5554 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5555 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5556 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5558 ASSERT_EQ(0, motionArgs.flags);
5559 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5560 ASSERT_EQ(0, motionArgs.buttonState);
5561 ASSERT_EQ(0, motionArgs.edgeFlags);
5562 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5563 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5564 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5566 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5567 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5568 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5569 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5570
5571 // Should not have sent any more keys or motions.
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5574}
5575
5576TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 addConfigurationProperty("touch.deviceType", "touchScreen");
5578 prepareDisplay(DISPLAY_ORIENTATION_0);
5579 prepareButtons();
5580 prepareAxes(POSITION);
5581 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005582 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
arthurhungdcef2dc2020-08-11 14:47:50 +08005584 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585
5586 NotifyMotionArgs motionArgs;
5587
5588 // Initially go down out of bounds.
5589 int32_t x = -10;
5590 int32_t y = -10;
5591 processDown(mapper, x, y);
5592 processSync(mapper);
5593
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5595
5596 // Move into the display area. Should generate a pointer down.
5597 x = 50;
5598 y = 75;
5599 processMove(mapper, x, y);
5600 processSync(mapper);
5601
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5603 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5604 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5605 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5606 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5608 ASSERT_EQ(0, motionArgs.flags);
5609 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5610 ASSERT_EQ(0, motionArgs.buttonState);
5611 ASSERT_EQ(0, motionArgs.edgeFlags);
5612 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5616 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5617 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5618 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5620
5621 // Release. Should generate a pointer up.
5622 processUp(mapper);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5627 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5628 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5629 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5631 ASSERT_EQ(0, motionArgs.flags);
5632 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5633 ASSERT_EQ(0, motionArgs.buttonState);
5634 ASSERT_EQ(0, motionArgs.edgeFlags);
5635 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5636 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5639 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5640 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5641 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5643
5644 // Should not have sent any more keys or motions.
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5647}
5648
Santos Cordonfa5cf462017-04-05 10:37:00 -07005649TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005650 addConfigurationProperty("touch.deviceType", "touchScreen");
5651 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5652
5653 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5654 prepareButtons();
5655 prepareAxes(POSITION);
5656 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005657 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005658
arthurhungdcef2dc2020-08-11 14:47:50 +08005659 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005660
5661 NotifyMotionArgs motionArgs;
5662
5663 // Down.
5664 int32_t x = 100;
5665 int32_t y = 125;
5666 processDown(mapper, x, y);
5667 processSync(mapper);
5668
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5671 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5672 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5674 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.flags);
5677 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5678 ASSERT_EQ(0, motionArgs.buttonState);
5679 ASSERT_EQ(0, motionArgs.edgeFlags);
5680 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5681 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5684 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5685 1, 0, 0, 0, 0, 0, 0, 0));
5686 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5687 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5689
5690 // Move.
5691 x += 50;
5692 y += 75;
5693 processMove(mapper, x, y);
5694 processSync(mapper);
5695
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5698 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5699 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5700 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5701 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5703 ASSERT_EQ(0, motionArgs.flags);
5704 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5705 ASSERT_EQ(0, motionArgs.buttonState);
5706 ASSERT_EQ(0, motionArgs.edgeFlags);
5707 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5708 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5711 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5712 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5714 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5716
5717 // Up.
5718 processUp(mapper);
5719 processSync(mapper);
5720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5723 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5724 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5725 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5726 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5727 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5728 ASSERT_EQ(0, motionArgs.flags);
5729 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5730 ASSERT_EQ(0, motionArgs.buttonState);
5731 ASSERT_EQ(0, motionArgs.edgeFlags);
5732 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5733 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5736 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5737 1, 0, 0, 0, 0, 0, 0, 0));
5738 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5739 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5741
5742 // Should not have sent any more keys or motions.
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5745}
5746
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 addConfigurationProperty("touch.deviceType", "touchScreen");
5749 prepareDisplay(DISPLAY_ORIENTATION_0);
5750 prepareButtons();
5751 prepareAxes(POSITION);
5752 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005753 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754
arthurhungdcef2dc2020-08-11 14:47:50 +08005755 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
5757 NotifyMotionArgs motionArgs;
5758
5759 // Down.
5760 int32_t x = 100;
5761 int32_t y = 125;
5762 processDown(mapper, x, y);
5763 processSync(mapper);
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5767 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5769 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5771 ASSERT_EQ(0, motionArgs.flags);
5772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5773 ASSERT_EQ(0, motionArgs.buttonState);
5774 ASSERT_EQ(0, motionArgs.edgeFlags);
5775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5780 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5781 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5783
5784 // Move.
5785 x += 50;
5786 y += 75;
5787 processMove(mapper, x, y);
5788 processSync(mapper);
5789
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5792 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5793 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5794 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5796 ASSERT_EQ(0, motionArgs.flags);
5797 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5798 ASSERT_EQ(0, motionArgs.buttonState);
5799 ASSERT_EQ(0, motionArgs.edgeFlags);
5800 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5804 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5805 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5806 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5808
5809 // Up.
5810 processUp(mapper);
5811 processSync(mapper);
5812
5813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5814 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5815 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5816 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5817 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5818 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5819 ASSERT_EQ(0, motionArgs.flags);
5820 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5821 ASSERT_EQ(0, motionArgs.buttonState);
5822 ASSERT_EQ(0, motionArgs.edgeFlags);
5823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5824 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5827 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5828 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5829 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5831
5832 // Should not have sent any more keys or motions.
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5835}
5836
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005837TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005838 addConfigurationProperty("touch.deviceType", "touchScreen");
5839 prepareButtons();
5840 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005841 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5842 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005843 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844
5845 NotifyMotionArgs args;
5846
5847 // Rotation 90.
5848 prepareDisplay(DISPLAY_ORIENTATION_90);
5849 processDown(mapper, toRawX(50), toRawY(75));
5850 processSync(mapper);
5851
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5853 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5854 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5855
5856 processUp(mapper);
5857 processSync(mapper);
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5859}
5860
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005861TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareButtons();
5864 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005865 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5866 // orientation-aware are affected by display rotation.
5867 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005868 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869
5870 NotifyMotionArgs args;
5871
5872 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005873 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 prepareDisplay(DISPLAY_ORIENTATION_0);
5875 processDown(mapper, toRawX(50), toRawY(75));
5876 processSync(mapper);
5877
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5879 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5880 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5881
5882 processUp(mapper);
5883 processSync(mapper);
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5885
5886 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005887 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005889 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 processSync(mapper);
5891
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5893 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5894 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5895
5896 processUp(mapper);
5897 processSync(mapper);
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5899
5900 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005901 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 prepareDisplay(DISPLAY_ORIENTATION_180);
5903 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5904 processSync(mapper);
5905
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5907 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5908 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5909
5910 processUp(mapper);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5913
5914 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005915 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005917 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 processSync(mapper);
5919
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5921 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5922 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5923
5924 processUp(mapper);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5927}
5928
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005929TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5930 addConfigurationProperty("touch.deviceType", "touchScreen");
5931 prepareButtons();
5932 prepareAxes(POSITION);
5933 addConfigurationProperty("touch.orientationAware", "1");
5934 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5935 clearViewports();
5936 prepareDisplay(DISPLAY_ORIENTATION_0);
5937 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5938 NotifyMotionArgs args;
5939
5940 // Orientation 0.
5941 processDown(mapper, toRawX(50), toRawY(75));
5942 processSync(mapper);
5943
5944 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5945 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5946 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5947
5948 processUp(mapper);
5949 processSync(mapper);
5950 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5951}
5952
5953TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5954 addConfigurationProperty("touch.deviceType", "touchScreen");
5955 prepareButtons();
5956 prepareAxes(POSITION);
5957 addConfigurationProperty("touch.orientationAware", "1");
5958 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5959 clearViewports();
5960 prepareDisplay(DISPLAY_ORIENTATION_0);
5961 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5962 NotifyMotionArgs args;
5963
5964 // Orientation 90.
5965 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5966 processSync(mapper);
5967
5968 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5969 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5970 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5971
5972 processUp(mapper);
5973 processSync(mapper);
5974 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5975}
5976
5977TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5978 addConfigurationProperty("touch.deviceType", "touchScreen");
5979 prepareButtons();
5980 prepareAxes(POSITION);
5981 addConfigurationProperty("touch.orientationAware", "1");
5982 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5983 clearViewports();
5984 prepareDisplay(DISPLAY_ORIENTATION_0);
5985 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5986 NotifyMotionArgs args;
5987
5988 // Orientation 180.
5989 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5990 processSync(mapper);
5991
5992 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5994 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5995
5996 processUp(mapper);
5997 processSync(mapper);
5998 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5999}
6000
6001TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6002 addConfigurationProperty("touch.deviceType", "touchScreen");
6003 prepareButtons();
6004 prepareAxes(POSITION);
6005 addConfigurationProperty("touch.orientationAware", "1");
6006 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6007 clearViewports();
6008 prepareDisplay(DISPLAY_ORIENTATION_0);
6009 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6010 NotifyMotionArgs args;
6011
6012 // Orientation 270.
6013 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6014 processSync(mapper);
6015
6016 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6017 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6018 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6019
6020 processUp(mapper);
6021 processSync(mapper);
6022 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6023}
6024
6025TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6026 addConfigurationProperty("touch.deviceType", "touchScreen");
6027 prepareButtons();
6028 prepareAxes(POSITION);
6029 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6030 // orientation-aware are affected by display rotation.
6031 addConfigurationProperty("touch.orientationAware", "0");
6032 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6033 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6034
6035 NotifyMotionArgs args;
6036
6037 // Orientation 90, Rotation 0.
6038 clearViewports();
6039 prepareDisplay(DISPLAY_ORIENTATION_0);
6040 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6041 processSync(mapper);
6042
6043 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6044 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6045 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6046
6047 processUp(mapper);
6048 processSync(mapper);
6049 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6050
6051 // Orientation 90, Rotation 90.
6052 clearViewports();
6053 prepareDisplay(DISPLAY_ORIENTATION_90);
6054 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6055 processSync(mapper);
6056
6057 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6058 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6059 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6060
6061 processUp(mapper);
6062 processSync(mapper);
6063 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6064
6065 // Orientation 90, Rotation 180.
6066 clearViewports();
6067 prepareDisplay(DISPLAY_ORIENTATION_180);
6068 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6069 processSync(mapper);
6070
6071 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6072 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6073 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6074
6075 processUp(mapper);
6076 processSync(mapper);
6077 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6078
6079 // Orientation 90, Rotation 270.
6080 clearViewports();
6081 prepareDisplay(DISPLAY_ORIENTATION_270);
6082 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6083 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6084 processSync(mapper);
6085
6086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6087 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6088 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6089
6090 processUp(mapper);
6091 processSync(mapper);
6092 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6093}
6094
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 addConfigurationProperty("touch.deviceType", "touchScreen");
6097 prepareDisplay(DISPLAY_ORIENTATION_0);
6098 prepareButtons();
6099 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006100 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101
6102 // These calculations are based on the input device calibration documentation.
6103 int32_t rawX = 100;
6104 int32_t rawY = 200;
6105 int32_t rawPressure = 10;
6106 int32_t rawToolMajor = 12;
6107 int32_t rawDistance = 2;
6108 int32_t rawTiltX = 30;
6109 int32_t rawTiltY = 110;
6110
6111 float x = toDisplayX(rawX);
6112 float y = toDisplayY(rawY);
6113 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6114 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6115 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6116 float distance = float(rawDistance);
6117
6118 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6119 float tiltScale = M_PI / 180;
6120 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6121 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6122 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6123 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6124
6125 processDown(mapper, rawX, rawY);
6126 processPressure(mapper, rawPressure);
6127 processToolMajor(mapper, rawToolMajor);
6128 processDistance(mapper, rawDistance);
6129 processTilt(mapper, rawTiltX, rawTiltY);
6130 processSync(mapper);
6131
6132 NotifyMotionArgs args;
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6135 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6136 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6137}
6138
Jason Gerecke489fda82012-09-07 17:19:40 -07006139TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006140 addConfigurationProperty("touch.deviceType", "touchScreen");
6141 prepareDisplay(DISPLAY_ORIENTATION_0);
6142 prepareLocationCalibration();
6143 prepareButtons();
6144 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006146
6147 int32_t rawX = 100;
6148 int32_t rawY = 200;
6149
6150 float x = toDisplayX(toCookedX(rawX, rawY));
6151 float y = toDisplayY(toCookedY(rawX, rawY));
6152
6153 processDown(mapper, rawX, rawY);
6154 processSync(mapper);
6155
6156 NotifyMotionArgs args;
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6159 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6160}
6161
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 addConfigurationProperty("touch.deviceType", "touchScreen");
6164 prepareDisplay(DISPLAY_ORIENTATION_0);
6165 prepareButtons();
6166 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006167 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168
6169 NotifyMotionArgs motionArgs;
6170 NotifyKeyArgs keyArgs;
6171
6172 processDown(mapper, 100, 200);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6176 ASSERT_EQ(0, motionArgs.buttonState);
6177
6178 // press BTN_LEFT, release BTN_LEFT
6179 processKey(mapper, BTN_LEFT, 1);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6184
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6187 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6188
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 processKey(mapper, BTN_LEFT, 0);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006197 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198
6199 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6200 processKey(mapper, BTN_RIGHT, 1);
6201 processKey(mapper, BTN_MIDDLE, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6206 motionArgs.buttonState);
6207
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6210 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6211
6212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6214 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6215 motionArgs.buttonState);
6216
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217 processKey(mapper, BTN_RIGHT, 0);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
6227 processKey(mapper, BTN_MIDDLE, 0);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006232
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006235 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 // press BTN_BACK, release BTN_BACK
6238 processKey(mapper, BTN_BACK, 1);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6241 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6242 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006243
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6247
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6250 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006251
6252 processKey(mapper, BTN_BACK, 0);
6253 processSync(mapper);
6254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006256 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006259 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006260 ASSERT_EQ(0, motionArgs.buttonState);
6261
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6263 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6264 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6265
6266 // press BTN_SIDE, release BTN_SIDE
6267 processKey(mapper, BTN_SIDE, 1);
6268 processSync(mapper);
6269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6270 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6271 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006272
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6276
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6279 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280
6281 processKey(mapper, BTN_SIDE, 0);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006289 ASSERT_EQ(0, motionArgs.buttonState);
6290
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6292 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6293 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6294
6295 // press BTN_FORWARD, release BTN_FORWARD
6296 processKey(mapper, BTN_FORWARD, 1);
6297 processSync(mapper);
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6299 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6300 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006301
Michael Wrightd02c5b62014-02-10 15:10:22 -08006302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309
6310 processKey(mapper, BTN_FORWARD, 0);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006315
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006318 ASSERT_EQ(0, motionArgs.buttonState);
6319
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6321 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6322 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6323
6324 // press BTN_EXTRA, release BTN_EXTRA
6325 processKey(mapper, BTN_EXTRA, 1);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6329 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006330
Michael Wrightd02c5b62014-02-10 15:10:22 -08006331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006333 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6334
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338
6339 processKey(mapper, BTN_EXTRA, 0);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006344
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006347 ASSERT_EQ(0, motionArgs.buttonState);
6348
Michael Wrightd02c5b62014-02-10 15:10:22 -08006349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6350 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6351 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6352
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6354
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 // press BTN_STYLUS, release BTN_STYLUS
6356 processKey(mapper, BTN_STYLUS, 1);
6357 processSync(mapper);
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006360 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6361
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6364 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006365
6366 processKey(mapper, BTN_STYLUS, 0);
6367 processSync(mapper);
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006374 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375
6376 // press BTN_STYLUS2, release BTN_STYLUS2
6377 processKey(mapper, BTN_STYLUS2, 1);
6378 processSync(mapper);
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006381 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6382
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6385 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386
6387 processKey(mapper, BTN_STYLUS2, 0);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006391 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006395 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006396
6397 // release touch
6398 processUp(mapper);
6399 processSync(mapper);
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6402 ASSERT_EQ(0, motionArgs.buttonState);
6403}
6404
6405TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 addConfigurationProperty("touch.deviceType", "touchScreen");
6407 prepareDisplay(DISPLAY_ORIENTATION_0);
6408 prepareButtons();
6409 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006410 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 NotifyMotionArgs motionArgs;
6413
6414 // default tool type is finger
6415 processDown(mapper, 100, 200);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6420
6421 // eraser
6422 processKey(mapper, BTN_TOOL_RUBBER, 1);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6427
6428 // stylus
6429 processKey(mapper, BTN_TOOL_RUBBER, 0);
6430 processKey(mapper, BTN_TOOL_PEN, 1);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6435
6436 // brush
6437 processKey(mapper, BTN_TOOL_PEN, 0);
6438 processKey(mapper, BTN_TOOL_BRUSH, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6443
6444 // pencil
6445 processKey(mapper, BTN_TOOL_BRUSH, 0);
6446 processKey(mapper, BTN_TOOL_PENCIL, 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006452 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 processKey(mapper, BTN_TOOL_PENCIL, 0);
6454 processKey(mapper, BTN_TOOL_AIRBRUSH, 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 // mouse
6461 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6462 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6467
6468 // lens
6469 processKey(mapper, BTN_TOOL_MOUSE, 0);
6470 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
6475
6476 // double-tap
6477 processKey(mapper, BTN_TOOL_LENS, 0);
6478 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6483
6484 // triple-tap
6485 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6486 processKey(mapper, BTN_TOOL_TRIPLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
6491
6492 // quad-tap
6493 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6494 processKey(mapper, BTN_TOOL_QUADTAP, 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 // finger
6501 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6502 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
6509 processKey(mapper, BTN_TOOL_PEN, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6513 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6514
6515 // eraser trumps stylus
6516 processKey(mapper, BTN_TOOL_RUBBER, 1);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6521
6522 // mouse trumps eraser
6523 processKey(mapper, BTN_TOOL_MOUSE, 1);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6527 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6528
6529 // back to default tool type
6530 processKey(mapper, BTN_TOOL_MOUSE, 0);
6531 processKey(mapper, BTN_TOOL_RUBBER, 0);
6532 processKey(mapper, BTN_TOOL_PEN, 0);
6533 processKey(mapper, BTN_TOOL_FINGER, 0);
6534 processSync(mapper);
6535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6538}
6539
6540TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541 addConfigurationProperty("touch.deviceType", "touchScreen");
6542 prepareDisplay(DISPLAY_ORIENTATION_0);
6543 prepareButtons();
6544 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006545 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006546 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547
6548 NotifyMotionArgs motionArgs;
6549
6550 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6551 processKey(mapper, BTN_TOOL_FINGER, 1);
6552 processMove(mapper, 100, 200);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6557 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6558
6559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6563
6564 // move a little
6565 processMove(mapper, 150, 250);
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6570 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6571
6572 // down when BTN_TOUCH is pressed, pressure defaults to 1
6573 processKey(mapper, BTN_TOUCH, 1);
6574 processSync(mapper);
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6578 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6579
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6583 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6584
6585 // up when BTN_TOUCH is released, hover restored
6586 processKey(mapper, BTN_TOUCH, 0);
6587 processSync(mapper);
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6591 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6592
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6601 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6602
6603 // exit hover when pointer goes away
6604 processKey(mapper, BTN_TOOL_FINGER, 0);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6610}
6611
6612TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613 addConfigurationProperty("touch.deviceType", "touchScreen");
6614 prepareDisplay(DISPLAY_ORIENTATION_0);
6615 prepareButtons();
6616 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006618
6619 NotifyMotionArgs motionArgs;
6620
6621 // initially hovering because pressure is 0
6622 processDown(mapper, 100, 200);
6623 processPressure(mapper, 0);
6624 processSync(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6628 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6629
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6633 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6634
6635 // move a little
6636 processMove(mapper, 150, 250);
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6641 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6642
6643 // down when pressure is non-zero
6644 processPressure(mapper, RAW_PRESSURE_MAX);
6645 processSync(mapper);
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6649 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6650
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6654 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // up when pressure becomes 0, hover restored
6657 processPressure(mapper, 0);
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6660 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6662 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6663
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6667 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6668
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6672 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6673
6674 // exit hover when pointer goes away
6675 processUp(mapper);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6680 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6681}
6682
lilinnan687e58f2022-07-19 16:00:50 +08006683TEST_F(SingleTouchInputMapperTest,
6684 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
6686 prepareDisplay(DISPLAY_ORIENTATION_0);
6687 prepareButtons();
6688 prepareAxes(POSITION);
6689 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6690 NotifyMotionArgs motionArgs;
6691
6692 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006693 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006694 processSync(mapper);
6695
6696 // We should receive a down event
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6698 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6699
6700 // Change display id
6701 clearViewports();
6702 prepareSecondaryDisplay(ViewportType::INTERNAL);
6703
6704 // We should receive a cancel event
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6707 // Then receive reset called
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6709}
6710
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006711TEST_F(SingleTouchInputMapperTest,
6712 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 prepareDisplay(DISPLAY_ORIENTATION_0);
6715 prepareButtons();
6716 prepareAxes(POSITION);
6717 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6719 NotifyMotionArgs motionArgs;
6720
6721 // Start a new gesture.
6722 processDown(mapper, 100, 200);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6726
6727 // Make the viewport inactive. This will put the device in disabled mode.
6728 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6729 viewport->isActive = false;
6730 mFakePolicy->updateViewport(*viewport);
6731 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6732
6733 // We should receive a cancel event for the ongoing gesture.
6734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6735 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6736 // Then we should be notified that the device was reset.
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6738
6739 // No events are generated while the viewport is inactive.
6740 processMove(mapper, 101, 201);
6741 processSync(mapper);
6742 processDown(mapper, 102, 202);
6743 processSync(mapper);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6745
6746 // Make the viewport active again. The device should resume processing events.
6747 viewport->isActive = true;
6748 mFakePolicy->updateViewport(*viewport);
6749 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6750
6751 // The device is reset because it changes back to direct mode, without generating any events.
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6754
6755 // Start a new gesture.
6756 processDown(mapper, 100, 200);
6757 processSync(mapper);
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6760
6761 // No more events.
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6764}
6765
Prabir Pradhan5632d622021-09-06 07:57:20 -07006766// --- TouchDisplayProjectionTest ---
6767
6768class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6769public:
6770 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6771 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6772 // rotated equivalent of the given un-rotated physical display bounds.
6773 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6774 uint32_t inverseRotationFlags;
6775 auto width = DISPLAY_WIDTH;
6776 auto height = DISPLAY_HEIGHT;
6777 switch (orientation) {
6778 case DISPLAY_ORIENTATION_90:
6779 inverseRotationFlags = ui::Transform::ROT_270;
6780 std::swap(width, height);
6781 break;
6782 case DISPLAY_ORIENTATION_180:
6783 inverseRotationFlags = ui::Transform::ROT_180;
6784 break;
6785 case DISPLAY_ORIENTATION_270:
6786 inverseRotationFlags = ui::Transform::ROT_90;
6787 std::swap(width, height);
6788 break;
6789 case DISPLAY_ORIENTATION_0:
6790 inverseRotationFlags = ui::Transform::ROT_0;
6791 break;
6792 default:
6793 FAIL() << "Invalid orientation: " << orientation;
6794 }
6795
6796 const ui::Transform rotation(inverseRotationFlags, width, height);
6797 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6798
6799 std::optional<DisplayViewport> internalViewport =
6800 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6801 DisplayViewport& v = *internalViewport;
6802 v.displayId = DISPLAY_ID;
6803 v.orientation = orientation;
6804
6805 v.logicalLeft = 0;
6806 v.logicalTop = 0;
6807 v.logicalRight = 100;
6808 v.logicalBottom = 100;
6809
6810 v.physicalLeft = rotatedPhysicalDisplay.left;
6811 v.physicalTop = rotatedPhysicalDisplay.top;
6812 v.physicalRight = rotatedPhysicalDisplay.right;
6813 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6814
6815 v.deviceWidth = width;
6816 v.deviceHeight = height;
6817
6818 v.isActive = true;
6819 v.uniqueId = UNIQUE_ID;
6820 v.type = ViewportType::INTERNAL;
6821 mFakePolicy->updateViewport(v);
6822 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6823 }
6824
6825 void assertReceivedMove(const Point& point) {
6826 NotifyMotionArgs motionArgs;
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6829 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6831 1, 0, 0, 0, 0, 0, 0, 0));
6832 }
6833};
6834
6835TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6836 addConfigurationProperty("touch.deviceType", "touchScreen");
6837 prepareDisplay(DISPLAY_ORIENTATION_0);
6838
6839 prepareButtons();
6840 prepareAxes(POSITION);
6841 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6842
6843 NotifyMotionArgs motionArgs;
6844
6845 // Configure the DisplayViewport such that the logical display maps to a subsection of
6846 // the display panel called the physical display. Here, the physical display is bounded by the
6847 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6848 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6849 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6850 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6851
6852 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6853 DISPLAY_ORIENTATION_270}) {
6854 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6855
6856 // Touches outside the physical display should be ignored, and should not generate any
6857 // events. Ensure touches at the following points that lie outside of the physical display
6858 // area do not generate any events.
6859 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6860 processDown(mapper, toRawX(point.x), toRawY(point.y));
6861 processSync(mapper);
6862 processUp(mapper);
6863 processSync(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6865 << "Unexpected event generated for touch outside physical display at point: "
6866 << point.x << ", " << point.y;
6867 }
6868 }
6869}
6870
6871TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6872 addConfigurationProperty("touch.deviceType", "touchScreen");
6873 prepareDisplay(DISPLAY_ORIENTATION_0);
6874
6875 prepareButtons();
6876 prepareAxes(POSITION);
6877 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6878
6879 NotifyMotionArgs motionArgs;
6880
6881 // Configure the DisplayViewport such that the logical display maps to a subsection of
6882 // the display panel called the physical display. Here, the physical display is bounded by the
6883 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6884 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6885
6886 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6887 DISPLAY_ORIENTATION_270}) {
6888 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6889
6890 // Touches that start outside the physical display should be ignored until it enters the
6891 // physical display bounds, at which point it should generate a down event. Start a touch at
6892 // the point (5, 100), which is outside the physical display bounds.
6893 static const Point kOutsidePoint{5, 100};
6894 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6897
6898 // Move the touch into the physical display area. This should generate a pointer down.
6899 processMove(mapper, toRawX(11), toRawY(21));
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6903 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6904 ASSERT_NO_FATAL_FAILURE(
6905 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6906
6907 // Move the touch inside the physical display area. This should generate a pointer move.
6908 processMove(mapper, toRawX(69), toRawY(159));
6909 processSync(mapper);
6910 assertReceivedMove({69, 159});
6911
6912 // Move outside the physical display area. Since the pointer is already down, this should
6913 // now continue generating events.
6914 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6915 processSync(mapper);
6916 assertReceivedMove(kOutsidePoint);
6917
6918 // Release. This should generate a pointer up.
6919 processUp(mapper);
6920 processSync(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6924 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6925
6926 // Ensure no more events were generated.
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6929 }
6930}
6931
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932// --- MultiTouchInputMapperTest ---
6933
6934class MultiTouchInputMapperTest : public TouchInputMapperTest {
6935protected:
6936 void prepareAxes(int axes);
6937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006938 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6939 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6940 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6941 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6942 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6943 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6944 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6945 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6946 void processId(MultiTouchInputMapper& mapper, int32_t id);
6947 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6948 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6949 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6950 void processMTSync(MultiTouchInputMapper& mapper);
6951 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952};
6953
6954void MultiTouchInputMapperTest::prepareAxes(int axes) {
6955 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006956 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006958 }
6959 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006960 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6961 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006963 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6964 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965 }
6966 }
6967 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6969 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006972 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973 }
6974 }
6975 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006976 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6977 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 }
6979 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006980 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6981 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982 }
6983 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6985 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006986 }
6987 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6989 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990 }
6991 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6993 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 }
6995 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997 }
6998}
6999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007000void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7001 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007004}
7005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007006void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7007 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009}
7010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7012 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014}
7015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007016void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018}
7019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007020void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022}
7023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007024void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7025 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007027}
7028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007029void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031}
7032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007033void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035}
7036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007037void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039}
7040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007041void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043}
7044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007045void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047}
7048
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007049void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7050 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052}
7053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056}
7057
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007058void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060}
7061
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063 addConfigurationProperty("touch.deviceType", "touchScreen");
7064 prepareDisplay(DISPLAY_ORIENTATION_0);
7065 prepareAxes(POSITION);
7066 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007067 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068
arthurhungdcef2dc2020-08-11 14:47:50 +08007069 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070
7071 NotifyMotionArgs motionArgs;
7072
7073 // Two fingers down at once.
7074 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7075 processPosition(mapper, x1, y1);
7076 processMTSync(mapper);
7077 processPosition(mapper, x2, y2);
7078 processMTSync(mapper);
7079 processSync(mapper);
7080
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7082 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7083 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7084 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7085 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7086 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7087 ASSERT_EQ(0, motionArgs.flags);
7088 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7089 ASSERT_EQ(0, motionArgs.buttonState);
7090 ASSERT_EQ(0, motionArgs.edgeFlags);
7091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7095 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7096 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7097 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7099
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7101 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7102 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7103 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7104 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007105 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 ASSERT_EQ(0, motionArgs.flags);
7107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7108 ASSERT_EQ(0, motionArgs.buttonState);
7109 ASSERT_EQ(0, motionArgs.edgeFlags);
7110 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7111 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7113 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7114 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7116 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7118 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7119 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7120 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7122
7123 // Move.
7124 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7125 processPosition(mapper, x1, y1);
7126 processMTSync(mapper);
7127 processPosition(mapper, x2, y2);
7128 processMTSync(mapper);
7129 processSync(mapper);
7130
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7137 ASSERT_EQ(0, motionArgs.flags);
7138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7139 ASSERT_EQ(0, motionArgs.buttonState);
7140 ASSERT_EQ(0, motionArgs.edgeFlags);
7141 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7144 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7147 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7149 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7150 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7151 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7153
7154 // First finger up.
7155 x2 += 15; y2 -= 20;
7156 processPosition(mapper, x2, y2);
7157 processMTSync(mapper);
7158 processSync(mapper);
7159
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7162 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7163 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7164 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007165 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166 ASSERT_EQ(0, motionArgs.flags);
7167 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7168 ASSERT_EQ(0, motionArgs.buttonState);
7169 ASSERT_EQ(0, motionArgs.edgeFlags);
7170 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7171 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7173 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7174 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7176 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7178 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7179 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7180 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7182
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7185 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7186 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7187 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7189 ASSERT_EQ(0, motionArgs.flags);
7190 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7191 ASSERT_EQ(0, motionArgs.buttonState);
7192 ASSERT_EQ(0, motionArgs.edgeFlags);
7193 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7194 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7197 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7198 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7199 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7200 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7201
7202 // Move.
7203 x2 += 20; y2 -= 25;
7204 processPosition(mapper, x2, y2);
7205 processMTSync(mapper);
7206 processSync(mapper);
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7210 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7211 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7212 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7214 ASSERT_EQ(0, motionArgs.flags);
7215 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7216 ASSERT_EQ(0, motionArgs.buttonState);
7217 ASSERT_EQ(0, motionArgs.edgeFlags);
7218 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7219 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7220 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7222 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7223 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7224 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7226
7227 // New finger down.
7228 int32_t x3 = 700, y3 = 300;
7229 processPosition(mapper, x2, y2);
7230 processMTSync(mapper);
7231 processPosition(mapper, x3, y3);
7232 processMTSync(mapper);
7233 processSync(mapper);
7234
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7237 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7238 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7239 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007240 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_EQ(0, motionArgs.flags);
7242 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7243 ASSERT_EQ(0, motionArgs.buttonState);
7244 ASSERT_EQ(0, motionArgs.edgeFlags);
7245 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7246 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7248 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7251 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7253 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7254 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7255 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7257
7258 // Second finger up.
7259 x3 += 30; y3 -= 20;
7260 processPosition(mapper, x3, y3);
7261 processMTSync(mapper);
7262 processSync(mapper);
7263
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7266 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7267 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7268 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007269 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007270 ASSERT_EQ(0, motionArgs.flags);
7271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7272 ASSERT_EQ(0, motionArgs.buttonState);
7273 ASSERT_EQ(0, motionArgs.edgeFlags);
7274 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7275 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7277 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7280 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7293 ASSERT_EQ(0, motionArgs.flags);
7294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7295 ASSERT_EQ(0, motionArgs.buttonState);
7296 ASSERT_EQ(0, motionArgs.edgeFlags);
7297 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7301 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7305
7306 // Last finger up.
7307 processMTSync(mapper);
7308 processSync(mapper);
7309
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7312 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7313 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7314 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7315 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7316 ASSERT_EQ(0, motionArgs.flags);
7317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7318 ASSERT_EQ(0, motionArgs.buttonState);
7319 ASSERT_EQ(0, motionArgs.edgeFlags);
7320 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7321 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7324 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7325 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7326 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7328
7329 // Should not have sent any more keys or motions.
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7332}
7333
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007334TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7335 addConfigurationProperty("touch.deviceType", "touchScreen");
7336 prepareDisplay(DISPLAY_ORIENTATION_0);
7337
7338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7339 /*fuzz*/ 0, /*resolution*/ 10);
7340 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7341 /*fuzz*/ 0, /*resolution*/ 11);
7342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7343 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7345 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7347 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7349 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7350
7351 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7352
7353 // X and Y axes
7354 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7355 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7356 // Touch major and minor
7357 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7358 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7359 // Tool major and minor
7360 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7361 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7362}
7363
7364TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7365 addConfigurationProperty("touch.deviceType", "touchScreen");
7366 prepareDisplay(DISPLAY_ORIENTATION_0);
7367
7368 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7369 /*fuzz*/ 0, /*resolution*/ 10);
7370 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7371 /*fuzz*/ 0, /*resolution*/ 11);
7372
7373 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7374
7375 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7376
7377 // Touch major and minor
7378 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7379 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7380 // Tool major and minor
7381 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7382 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7383}
7384
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007386 addConfigurationProperty("touch.deviceType", "touchScreen");
7387 prepareDisplay(DISPLAY_ORIENTATION_0);
7388 prepareAxes(POSITION | ID);
7389 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391
arthurhungdcef2dc2020-08-11 14:47:50 +08007392 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007393
7394 NotifyMotionArgs motionArgs;
7395
7396 // Two fingers down at once.
7397 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7398 processPosition(mapper, x1, y1);
7399 processId(mapper, 1);
7400 processMTSync(mapper);
7401 processPosition(mapper, x2, y2);
7402 processId(mapper, 2);
7403 processMTSync(mapper);
7404 processSync(mapper);
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7408 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7409 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7412 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7413
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007415 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7418 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7419 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7422 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7424 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7425
7426 // Move.
7427 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7428 processPosition(mapper, x1, y1);
7429 processId(mapper, 1);
7430 processMTSync(mapper);
7431 processPosition(mapper, x2, y2);
7432 processId(mapper, 2);
7433 processMTSync(mapper);
7434 processSync(mapper);
7435
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7438 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7447
7448 // First finger up.
7449 x2 += 15; y2 -= 20;
7450 processPosition(mapper, x2, y2);
7451 processId(mapper, 2);
7452 processMTSync(mapper);
7453 processSync(mapper);
7454
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007456 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7460 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7463 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7465 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7466
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7469 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7470 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7473 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7474
7475 // Move.
7476 x2 += 20; y2 -= 25;
7477 processPosition(mapper, x2, y2);
7478 processId(mapper, 2);
7479 processMTSync(mapper);
7480 processSync(mapper);
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7485 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7489
7490 // New finger down.
7491 int32_t x3 = 700, y3 = 300;
7492 processPosition(mapper, x2, y2);
7493 processId(mapper, 2);
7494 processMTSync(mapper);
7495 processPosition(mapper, x3, y3);
7496 processId(mapper, 3);
7497 processMTSync(mapper);
7498 processSync(mapper);
7499
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007501 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7503 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7505 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7510 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7511
7512 // Second finger up.
7513 x3 += 30; y3 -= 20;
7514 processPosition(mapper, x3, y3);
7515 processId(mapper, 3);
7516 processMTSync(mapper);
7517 processSync(mapper);
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007520 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7524 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7527 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7529 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7530
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7533 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7534 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7535 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7537 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7538
7539 // Last finger up.
7540 processMTSync(mapper);
7541 processSync(mapper);
7542
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7544 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7549 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7550
7551 // Should not have sent any more keys or motions.
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7554}
7555
7556TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 addConfigurationProperty("touch.deviceType", "touchScreen");
7558 prepareDisplay(DISPLAY_ORIENTATION_0);
7559 prepareAxes(POSITION | ID | SLOT);
7560 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007561 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562
arthurhungdcef2dc2020-08-11 14:47:50 +08007563 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564
7565 NotifyMotionArgs motionArgs;
7566
7567 // Two fingers down at once.
7568 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7569 processPosition(mapper, x1, y1);
7570 processId(mapper, 1);
7571 processSlot(mapper, 1);
7572 processPosition(mapper, x2, y2);
7573 processId(mapper, 2);
7574 processSync(mapper);
7575
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7578 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7579 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007585 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007586 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7589 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7592 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7595
7596 // Move.
7597 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7598 processSlot(mapper, 0);
7599 processPosition(mapper, x1, y1);
7600 processSlot(mapper, 1);
7601 processPosition(mapper, x2, y2);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7606 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7609 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7612 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7615
7616 // First finger up.
7617 x2 += 15; y2 -= 20;
7618 processSlot(mapper, 0);
7619 processId(mapper, -1);
7620 processSlot(mapper, 1);
7621 processPosition(mapper, x2, y2);
7622 processSync(mapper);
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007625 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7627 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7629 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7634 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7635
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7638 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7639 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7643
7644 // Move.
7645 x2 += 20; y2 -= 25;
7646 processPosition(mapper, x2, y2);
7647 processSync(mapper);
7648
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7651 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7652 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7655 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7656
7657 // New finger down.
7658 int32_t x3 = 700, y3 = 300;
7659 processPosition(mapper, x2, y2);
7660 processSlot(mapper, 0);
7661 processId(mapper, 3);
7662 processPosition(mapper, x3, y3);
7663 processSync(mapper);
7664
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007666 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7670 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7673 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7675 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7676
7677 // Second finger up.
7678 x3 += 30; y3 -= 20;
7679 processSlot(mapper, 1);
7680 processId(mapper, -1);
7681 processSlot(mapper, 0);
7682 processPosition(mapper, x3, y3);
7683 processSync(mapper);
7684
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007686 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7690 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7693 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7695 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7699 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // Last finger up.
7706 processId(mapper, -1);
7707 processSync(mapper);
7708
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7710 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7715 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7716
7717 // Should not have sent any more keys or motions.
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7720}
7721
7722TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 addConfigurationProperty("touch.deviceType", "touchScreen");
7724 prepareDisplay(DISPLAY_ORIENTATION_0);
7725 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007726 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007727
7728 // These calculations are based on the input device calibration documentation.
7729 int32_t rawX = 100;
7730 int32_t rawY = 200;
7731 int32_t rawTouchMajor = 7;
7732 int32_t rawTouchMinor = 6;
7733 int32_t rawToolMajor = 9;
7734 int32_t rawToolMinor = 8;
7735 int32_t rawPressure = 11;
7736 int32_t rawDistance = 0;
7737 int32_t rawOrientation = 3;
7738 int32_t id = 5;
7739
7740 float x = toDisplayX(rawX);
7741 float y = toDisplayY(rawY);
7742 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7743 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7744 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7745 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7746 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7747 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7748 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7749 float distance = float(rawDistance);
7750
7751 processPosition(mapper, rawX, rawY);
7752 processTouchMajor(mapper, rawTouchMajor);
7753 processTouchMinor(mapper, rawTouchMinor);
7754 processToolMajor(mapper, rawToolMajor);
7755 processToolMinor(mapper, rawToolMinor);
7756 processPressure(mapper, rawPressure);
7757 processOrientation(mapper, rawOrientation);
7758 processDistance(mapper, rawDistance);
7759 processId(mapper, id);
7760 processMTSync(mapper);
7761 processSync(mapper);
7762
7763 NotifyMotionArgs args;
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7765 ASSERT_EQ(0, args.pointerProperties[0].id);
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7767 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7768 orientation, distance));
7769}
7770
7771TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 addConfigurationProperty("touch.deviceType", "touchScreen");
7773 prepareDisplay(DISPLAY_ORIENTATION_0);
7774 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7775 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777
7778 // These calculations are based on the input device calibration documentation.
7779 int32_t rawX = 100;
7780 int32_t rawY = 200;
7781 int32_t rawTouchMajor = 140;
7782 int32_t rawTouchMinor = 120;
7783 int32_t rawToolMajor = 180;
7784 int32_t rawToolMinor = 160;
7785
7786 float x = toDisplayX(rawX);
7787 float y = toDisplayY(rawY);
7788 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7789 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7790 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7791 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7792 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7793
7794 processPosition(mapper, rawX, rawY);
7795 processTouchMajor(mapper, rawTouchMajor);
7796 processTouchMinor(mapper, rawTouchMinor);
7797 processToolMajor(mapper, rawToolMajor);
7798 processToolMinor(mapper, rawToolMinor);
7799 processMTSync(mapper);
7800 processSync(mapper);
7801
7802 NotifyMotionArgs args;
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7805 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7806}
7807
7808TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 addConfigurationProperty("touch.deviceType", "touchScreen");
7810 prepareDisplay(DISPLAY_ORIENTATION_0);
7811 prepareAxes(POSITION | TOUCH | TOOL);
7812 addConfigurationProperty("touch.size.calibration", "diameter");
7813 addConfigurationProperty("touch.size.scale", "10");
7814 addConfigurationProperty("touch.size.bias", "160");
7815 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007816 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817
7818 // These calculations are based on the input device calibration documentation.
7819 // Note: We only provide a single common touch/tool value because the device is assumed
7820 // not to emit separate values for each pointer (isSummed = 1).
7821 int32_t rawX = 100;
7822 int32_t rawY = 200;
7823 int32_t rawX2 = 150;
7824 int32_t rawY2 = 250;
7825 int32_t rawTouchMajor = 5;
7826 int32_t rawToolMajor = 8;
7827
7828 float x = toDisplayX(rawX);
7829 float y = toDisplayY(rawY);
7830 float x2 = toDisplayX(rawX2);
7831 float y2 = toDisplayY(rawY2);
7832 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7833 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7834 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7835
7836 processPosition(mapper, rawX, rawY);
7837 processTouchMajor(mapper, rawTouchMajor);
7838 processToolMajor(mapper, rawToolMajor);
7839 processMTSync(mapper);
7840 processPosition(mapper, rawX2, rawY2);
7841 processTouchMajor(mapper, rawTouchMajor);
7842 processToolMajor(mapper, rawToolMajor);
7843 processMTSync(mapper);
7844 processSync(mapper);
7845
7846 NotifyMotionArgs args;
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007851 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_EQ(size_t(2), args.pointerCount);
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7854 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7856 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7857}
7858
7859TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 addConfigurationProperty("touch.deviceType", "touchScreen");
7861 prepareDisplay(DISPLAY_ORIENTATION_0);
7862 prepareAxes(POSITION | TOUCH | TOOL);
7863 addConfigurationProperty("touch.size.calibration", "area");
7864 addConfigurationProperty("touch.size.scale", "43");
7865 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007867
7868 // These calculations are based on the input device calibration documentation.
7869 int32_t rawX = 100;
7870 int32_t rawY = 200;
7871 int32_t rawTouchMajor = 5;
7872 int32_t rawToolMajor = 8;
7873
7874 float x = toDisplayX(rawX);
7875 float y = toDisplayY(rawY);
7876 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7877 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7878 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7879
7880 processPosition(mapper, rawX, rawY);
7881 processTouchMajor(mapper, rawTouchMajor);
7882 processToolMajor(mapper, rawToolMajor);
7883 processMTSync(mapper);
7884 processSync(mapper);
7885
7886 NotifyMotionArgs args;
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7889 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7890}
7891
7892TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 addConfigurationProperty("touch.deviceType", "touchScreen");
7894 prepareDisplay(DISPLAY_ORIENTATION_0);
7895 prepareAxes(POSITION | PRESSURE);
7896 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7897 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007898 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899
Michael Wrightaa449c92017-12-13 21:21:43 +00007900 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007901 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007902 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7903 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7904 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7905
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906 // These calculations are based on the input device calibration documentation.
7907 int32_t rawX = 100;
7908 int32_t rawY = 200;
7909 int32_t rawPressure = 60;
7910
7911 float x = toDisplayX(rawX);
7912 float y = toDisplayY(rawY);
7913 float pressure = float(rawPressure) * 0.01f;
7914
7915 processPosition(mapper, rawX, rawY);
7916 processPressure(mapper, rawPressure);
7917 processMTSync(mapper);
7918 processSync(mapper);
7919
7920 NotifyMotionArgs args;
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7923 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7924}
7925
7926TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 addConfigurationProperty("touch.deviceType", "touchScreen");
7928 prepareDisplay(DISPLAY_ORIENTATION_0);
7929 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007930 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931
7932 NotifyMotionArgs motionArgs;
7933 NotifyKeyArgs keyArgs;
7934
7935 processId(mapper, 1);
7936 processPosition(mapper, 100, 200);
7937 processSync(mapper);
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7940 ASSERT_EQ(0, motionArgs.buttonState);
7941
7942 // press BTN_LEFT, release BTN_LEFT
7943 processKey(mapper, BTN_LEFT, 1);
7944 processSync(mapper);
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7947 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7948
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7951 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7952
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 processKey(mapper, BTN_LEFT, 0);
7954 processSync(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007961 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007962
7963 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7964 processKey(mapper, BTN_RIGHT, 1);
7965 processKey(mapper, BTN_MIDDLE, 1);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7968 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7969 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7970 motionArgs.buttonState);
7971
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7974 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7975
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7978 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7979 motionArgs.buttonState);
7980
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 processKey(mapper, BTN_RIGHT, 0);
7982 processSync(mapper);
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007985 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007986
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007989 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990
7991 processKey(mapper, BTN_MIDDLE, 0);
7992 processSync(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007996
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007999 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008000
8001 // press BTN_BACK, release BTN_BACK
8002 processKey(mapper, BTN_BACK, 1);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8005 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8006 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008007
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008010 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8011
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8014 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015
8016 processKey(mapper, BTN_BACK, 0);
8017 processSync(mapper);
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008021
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008024 ASSERT_EQ(0, motionArgs.buttonState);
8025
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8028 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8029
8030 // press BTN_SIDE, release BTN_SIDE
8031 processKey(mapper, BTN_SIDE, 1);
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008036
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008039 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8040
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8043 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044
8045 processKey(mapper, BTN_SIDE, 0);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008050
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008053 ASSERT_EQ(0, motionArgs.buttonState);
8054
Michael Wrightd02c5b62014-02-10 15:10:22 -08008055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8056 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8057 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8058
8059 // press BTN_FORWARD, release BTN_FORWARD
8060 processKey(mapper, BTN_FORWARD, 1);
8061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8064 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008065
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008068 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8069
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8072 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073
8074 processKey(mapper, BTN_FORWARD, 0);
8075 processSync(mapper);
8076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008082 ASSERT_EQ(0, motionArgs.buttonState);
8083
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8085 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8086 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8087
8088 // press BTN_EXTRA, release BTN_EXTRA
8089 processKey(mapper, BTN_EXTRA, 1);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8092 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8093 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8101 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 processKey(mapper, BTN_EXTRA, 0);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(0, motionArgs.buttonState);
8112
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8114 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8115 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8116
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8118
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 // press BTN_STYLUS, release BTN_STYLUS
8120 processKey(mapper, BTN_STYLUS, 1);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8125
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8128 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129
8130 processKey(mapper, BTN_STYLUS, 0);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139
8140 // press BTN_STYLUS2, release BTN_STYLUS2
8141 processKey(mapper, BTN_STYLUS2, 1);
8142 processSync(mapper);
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8146
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8149 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150
8151 processKey(mapper, BTN_STYLUS2, 0);
8152 processSync(mapper);
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008156
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160
8161 // release touch
8162 processId(mapper, -1);
8163 processSync(mapper);
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8166 ASSERT_EQ(0, motionArgs.buttonState);
8167}
8168
8169TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170 addConfigurationProperty("touch.deviceType", "touchScreen");
8171 prepareDisplay(DISPLAY_ORIENTATION_0);
8172 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008173 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174
8175 NotifyMotionArgs motionArgs;
8176
8177 // default tool type is finger
8178 processId(mapper, 1);
8179 processPosition(mapper, 100, 200);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8184
8185 // eraser
8186 processKey(mapper, BTN_TOOL_RUBBER, 1);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8191
8192 // stylus
8193 processKey(mapper, BTN_TOOL_RUBBER, 0);
8194 processKey(mapper, BTN_TOOL_PEN, 1);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8199
8200 // brush
8201 processKey(mapper, BTN_TOOL_PEN, 0);
8202 processKey(mapper, BTN_TOOL_BRUSH, 1);
8203 processSync(mapper);
8204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8207
8208 // pencil
8209 processKey(mapper, BTN_TOOL_BRUSH, 0);
8210 processKey(mapper, BTN_TOOL_PENCIL, 1);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8215
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008216 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 processKey(mapper, BTN_TOOL_PENCIL, 0);
8218 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8219 processSync(mapper);
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8222 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8223
8224 // mouse
8225 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8226 processKey(mapper, BTN_TOOL_MOUSE, 1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8231
8232 // lens
8233 processKey(mapper, BTN_TOOL_MOUSE, 0);
8234 processKey(mapper, BTN_TOOL_LENS, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
8239
8240 // double-tap
8241 processKey(mapper, BTN_TOOL_LENS, 0);
8242 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8247
8248 // triple-tap
8249 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8250 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8255
8256 // quad-tap
8257 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8258 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8263
8264 // finger
8265 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8266 processKey(mapper, BTN_TOOL_FINGER, 1);
8267 processSync(mapper);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8271
8272 // stylus trumps finger
8273 processKey(mapper, BTN_TOOL_PEN, 1);
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_STYLUS, motionArgs.pointerProperties[0].toolType);
8278
8279 // eraser trumps stylus
8280 processKey(mapper, BTN_TOOL_RUBBER, 1);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8285
8286 // mouse trumps eraser
8287 processKey(mapper, BTN_TOOL_MOUSE, 1);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8292
8293 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8294 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8295 processSync(mapper);
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8299
8300 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8301 processToolType(mapper, MT_TOOL_PEN);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8306
8307 // back to default tool type
8308 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8309 processKey(mapper, BTN_TOOL_MOUSE, 0);
8310 processKey(mapper, BTN_TOOL_RUBBER, 0);
8311 processKey(mapper, BTN_TOOL_PEN, 0);
8312 processKey(mapper, BTN_TOOL_FINGER, 0);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8315 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8317}
8318
8319TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 addConfigurationProperty("touch.deviceType", "touchScreen");
8321 prepareDisplay(DISPLAY_ORIENTATION_0);
8322 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008323 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008324 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325
8326 NotifyMotionArgs motionArgs;
8327
8328 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8329 processId(mapper, 1);
8330 processPosition(mapper, 100, 200);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8336
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8341
8342 // move a little
8343 processPosition(mapper, 150, 250);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8348 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8349
8350 // down when BTN_TOUCH is pressed, pressure defaults to 1
8351 processKey(mapper, BTN_TOUCH, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8356 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8357
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8359 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8361 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8362
8363 // up when BTN_TOUCH is released, hover restored
8364 processKey(mapper, BTN_TOUCH, 0);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8369 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8370
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8372 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8374 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8375
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8379 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8380
8381 // exit hover when pointer goes away
8382 processId(mapper, -1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8387 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8388}
8389
8390TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391 addConfigurationProperty("touch.deviceType", "touchScreen");
8392 prepareDisplay(DISPLAY_ORIENTATION_0);
8393 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008394 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395
8396 NotifyMotionArgs motionArgs;
8397
8398 // initially hovering because pressure is 0
8399 processId(mapper, 1);
8400 processPosition(mapper, 100, 200);
8401 processPressure(mapper, 0);
8402 processSync(mapper);
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(100), toDisplayY(200), 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(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8412
8413 // move a little
8414 processPosition(mapper, 150, 250);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
8422 processPressure(mapper, RAW_PRESSURE_MAX);
8423 processSync(mapper);
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8427 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8432 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8433
8434 // up when pressure becomes 0, hover restored
8435 processPressure(mapper, 0);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8440 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8441
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8445 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8450 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8451
8452 // exit hover when pointer goes away
8453 processId(mapper, -1);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8458 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8459}
8460
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008461/**
8462 * Set the input device port <--> display port associations, and check that the
8463 * events are routed to the display that matches the display port.
8464 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8465 */
8466TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008467 const std::string usb2 = "USB2";
8468 const uint8_t hdmi1 = 0;
8469 const uint8_t hdmi2 = 1;
8470 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008471 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008472
8473 addConfigurationProperty("touch.deviceType", "touchScreen");
8474 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008476
8477 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8478 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8479
8480 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8481 // for this input device is specified, and the matching viewport is not present,
8482 // the input device should be disabled (at the mapper level).
8483
8484 // Add viewport for display 2 on hdmi2
8485 prepareSecondaryDisplay(type, hdmi2);
8486 // Send a touch event
8487 processPosition(mapper, 100, 100);
8488 processSync(mapper);
8489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8490
8491 // Add viewport for display 1 on hdmi1
8492 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8493 // Send a touch event again
8494 processPosition(mapper, 100, 100);
8495 processSync(mapper);
8496
8497 NotifyMotionArgs args;
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8499 ASSERT_EQ(DISPLAY_ID, args.displayId);
8500}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008502TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8503 addConfigurationProperty("touch.deviceType", "touchScreen");
8504 prepareAxes(POSITION);
8505 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8506
8507 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8508
8509 prepareDisplay(DISPLAY_ORIENTATION_0);
8510 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8511
8512 // Send a touch event
8513 processPosition(mapper, 100, 100);
8514 processSync(mapper);
8515
8516 NotifyMotionArgs args;
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8518 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8519}
8520
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008521TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008522 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008523 std::shared_ptr<FakePointerController> fakePointerController =
8524 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008525 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008526 fakePointerController->setPosition(100, 200);
8527 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008528 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008529
Garfield Tan888a6a42020-01-09 11:39:16 -08008530 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008531 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008532
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008533 prepareDisplay(DISPLAY_ORIENTATION_0);
8534 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008535 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008536
8537 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008538 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008539
8540 NotifyMotionArgs motionArgs;
8541 processPosition(mapper, 100, 100);
8542 processSync(mapper);
8543
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8546 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8547}
8548
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008549/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008550 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8551 */
8552TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8553 addConfigurationProperty("touch.deviceType", "touchScreen");
8554 prepareAxes(POSITION);
8555 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8556
8557 prepareDisplay(DISPLAY_ORIENTATION_0);
8558 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8559 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8560 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8561 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8562
8563 NotifyMotionArgs args;
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8565 ASSERT_EQ(26, args.readTime);
8566
8567 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8568 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8569 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8570
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8572 ASSERT_EQ(33, args.readTime);
8573}
8574
8575/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008576 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8577 * events should not be delivered to the listener.
8578 */
8579TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8580 addConfigurationProperty("touch.deviceType", "touchScreen");
8581 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8582 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8583 ViewportType::INTERNAL);
8584 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8585 prepareAxes(POSITION);
8586 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8587
8588 NotifyMotionArgs motionArgs;
8589 processPosition(mapper, 100, 100);
8590 processSync(mapper);
8591
8592 mFakeListener->assertNotifyMotionWasNotCalled();
8593}
8594
Garfield Tanc734e4f2021-01-15 20:01:39 -08008595TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8596 addConfigurationProperty("touch.deviceType", "touchScreen");
8597 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8598 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8599 ViewportType::INTERNAL);
8600 std::optional<DisplayViewport> optionalDisplayViewport =
8601 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8602 ASSERT_TRUE(optionalDisplayViewport.has_value());
8603 DisplayViewport displayViewport = *optionalDisplayViewport;
8604
8605 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8606 prepareAxes(POSITION);
8607 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8608
8609 // Finger down
8610 int32_t x = 100, y = 100;
8611 processPosition(mapper, x, y);
8612 processSync(mapper);
8613
8614 NotifyMotionArgs motionArgs;
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8617
8618 // Deactivate display viewport
8619 displayViewport.isActive = false;
8620 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8621 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8622
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008623 // The ongoing touch should be canceled immediately
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8626
8627 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008628 x += 10, y += 10;
8629 processPosition(mapper, x, y);
8630 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008632
8633 // Reactivate display viewport
8634 displayViewport.isActive = true;
8635 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8636 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8637
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008638 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008639 x += 10, y += 10;
8640 processPosition(mapper, x, y);
8641 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008644}
8645
Arthur Hung7c645402019-01-25 17:45:42 +08008646TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8647 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008648 prepareAxes(POSITION | ID | SLOT);
8649 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008651
8652 // Create the second touch screen device, and enable multi fingers.
8653 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008654 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008655 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008656 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008657 std::shared_ptr<InputDevice> device2 =
8658 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008659 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008660
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008661 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8662 0 /*flat*/, 0 /*fuzz*/);
8663 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8664 0 /*flat*/, 0 /*fuzz*/);
8665 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8666 0 /*flat*/, 0 /*fuzz*/);
8667 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8668 0 /*flat*/, 0 /*fuzz*/);
8669 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8670 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8671 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008672
8673 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008674 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008675 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8676 device2->reset(ARBITRARY_TIME);
8677
8678 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008679 std::shared_ptr<FakePointerController> fakePointerController =
8680 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008681 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008682
8683 // Setup policy for associated displays and show touches.
8684 const uint8_t hdmi1 = 0;
8685 const uint8_t hdmi2 = 1;
8686 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8687 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8688 mFakePolicy->setShowTouches(true);
8689
8690 // Create displays.
8691 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008692 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008693
8694 // Default device will reconfigure above, need additional reconfiguration for another device.
8695 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan197e0862022-07-01 14:28:00 +00008696 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8697 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008698
8699 // Two fingers down at default display.
8700 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8701 processPosition(mapper, x1, y1);
8702 processId(mapper, 1);
8703 processSlot(mapper, 1);
8704 processPosition(mapper, x2, y2);
8705 processId(mapper, 2);
8706 processSync(mapper);
8707
8708 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8709 fakePointerController->getSpots().find(DISPLAY_ID);
8710 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8711 ASSERT_EQ(size_t(2), iter->second.size());
8712
8713 // Two fingers down at second display.
8714 processPosition(mapper2, x1, y1);
8715 processId(mapper2, 1);
8716 processSlot(mapper2, 1);
8717 processPosition(mapper2, x2, y2);
8718 processId(mapper2, 2);
8719 processSync(mapper2);
8720
8721 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8722 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8723 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008724
8725 // Disable the show touches configuration and ensure the spots are cleared.
8726 mFakePolicy->setShowTouches(false);
8727 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8728 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
8729
8730 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008731}
8732
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008733TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008734 prepareAxes(POSITION);
8735 addConfigurationProperty("touch.deviceType", "touchScreen");
8736 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008737 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008738
8739 NotifyMotionArgs motionArgs;
8740 // Unrotated video frame
8741 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8742 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008743 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008744 processPosition(mapper, 100, 200);
8745 processSync(mapper);
8746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8747 ASSERT_EQ(frames, motionArgs.videoFrames);
8748
8749 // Subsequent touch events should not have any videoframes
8750 // This is implemented separately in FakeEventHub,
8751 // but that should match the behaviour of TouchVideoDevice.
8752 processPosition(mapper, 200, 200);
8753 processSync(mapper);
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8755 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8756}
8757
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008758TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008759 prepareAxes(POSITION);
8760 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008761 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008762 // Unrotated video frame
8763 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8764 NotifyMotionArgs motionArgs;
8765
8766 // Test all 4 orientations
8767 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008768 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8769 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8770 clearViewports();
8771 prepareDisplay(orientation);
8772 std::vector<TouchVideoFrame> frames{frame};
8773 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8774 processPosition(mapper, 100, 200);
8775 processSync(mapper);
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8777 ASSERT_EQ(frames, motionArgs.videoFrames);
8778 }
8779}
8780
8781TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8782 prepareAxes(POSITION);
8783 addConfigurationProperty("touch.deviceType", "touchScreen");
8784 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8785 // orientation-aware are affected by display rotation.
8786 addConfigurationProperty("touch.orientationAware", "0");
8787 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8788 // Unrotated video frame
8789 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8790 NotifyMotionArgs motionArgs;
8791
8792 // Test all 4 orientations
8793 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008794 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8795 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8796 clearViewports();
8797 prepareDisplay(orientation);
8798 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008799 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008800 processPosition(mapper, 100, 200);
8801 processSync(mapper);
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008803 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8804 // compared to the display. This is so that when the window transform (which contains the
8805 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8806 // window's coordinate space.
8807 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008808 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008809
8810 // Release finger.
8811 processSync(mapper);
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008813 }
8814}
8815
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008816TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008817 prepareAxes(POSITION);
8818 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008819 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008820 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8821 // so mix these.
8822 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8823 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8824 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8825 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8826 NotifyMotionArgs motionArgs;
8827
8828 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008829 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008830 processPosition(mapper, 100, 200);
8831 processSync(mapper);
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008833 ASSERT_EQ(frames, motionArgs.videoFrames);
8834}
8835
8836TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8837 prepareAxes(POSITION);
8838 addConfigurationProperty("touch.deviceType", "touchScreen");
8839 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8840 // orientation-aware are affected by display rotation.
8841 addConfigurationProperty("touch.orientationAware", "0");
8842 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8843 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8844 // so mix these.
8845 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8846 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8847 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8848 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8849 NotifyMotionArgs motionArgs;
8850
8851 prepareDisplay(DISPLAY_ORIENTATION_90);
8852 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8853 processPosition(mapper, 100, 200);
8854 processSync(mapper);
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8856 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8857 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8858 // compared to the display. This is so that when the window transform (which contains the
8859 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8860 // window's coordinate space.
8861 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8862 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008863 ASSERT_EQ(frames, motionArgs.videoFrames);
8864}
8865
Arthur Hung9da14732019-09-02 16:16:58 +08008866/**
8867 * If we had defined port associations, but the viewport is not ready, the touch device would be
8868 * expected to be disabled, and it should be enabled after the viewport has found.
8869 */
8870TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008871 constexpr uint8_t hdmi2 = 1;
8872 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008873 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008874
8875 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8876
8877 addConfigurationProperty("touch.deviceType", "touchScreen");
8878 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008879 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008880
8881 ASSERT_EQ(mDevice->isEnabled(), false);
8882
8883 // Add display on hdmi2, the device should be enabled and can receive touch event.
8884 prepareSecondaryDisplay(type, hdmi2);
8885 ASSERT_EQ(mDevice->isEnabled(), true);
8886
8887 // Send a touch event.
8888 processPosition(mapper, 100, 100);
8889 processSync(mapper);
8890
8891 NotifyMotionArgs args;
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8893 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8894}
8895
Arthur Hung421eb1c2020-01-16 00:09:42 +08008896TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008897 addConfigurationProperty("touch.deviceType", "touchScreen");
8898 prepareDisplay(DISPLAY_ORIENTATION_0);
8899 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008900 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008901
8902 NotifyMotionArgs motionArgs;
8903
8904 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8905 // finger down
8906 processId(mapper, 1);
8907 processPosition(mapper, x1, y1);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8912
8913 // finger move
8914 processId(mapper, 1);
8915 processPosition(mapper, x2, y2);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920
8921 // finger up.
8922 processId(mapper, -1);
8923 processSync(mapper);
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8927
8928 // new finger down
8929 processId(mapper, 1);
8930 processPosition(mapper, x3, y3);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8935}
8936
8937/**
arthurhungcc7f9802020-04-30 17:55:40 +08008938 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8939 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008940 */
arthurhungcc7f9802020-04-30 17:55:40 +08008941TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008942 addConfigurationProperty("touch.deviceType", "touchScreen");
8943 prepareDisplay(DISPLAY_ORIENTATION_0);
8944 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008946
8947 NotifyMotionArgs motionArgs;
8948
8949 // default tool type is finger
8950 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008951 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008952 processPosition(mapper, x1, y1);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8957
8958 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8959 processToolType(mapper, MT_TOOL_PALM);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8962 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8963
8964 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008965 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008966 processPosition(mapper, x2, y2);
8967 processSync(mapper);
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8969
8970 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008971 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8974
8975 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008976 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008977 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008978 processPosition(mapper, x3, y3);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8983}
8984
arthurhungbf89a482020-04-17 17:37:55 +08008985/**
arthurhungcc7f9802020-04-30 17:55:40 +08008986 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8987 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008988 */
arthurhungcc7f9802020-04-30 17:55:40 +08008989TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008990 addConfigurationProperty("touch.deviceType", "touchScreen");
8991 prepareDisplay(DISPLAY_ORIENTATION_0);
8992 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8993 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8994
8995 NotifyMotionArgs motionArgs;
8996
8997 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008998 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8999 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009000 processPosition(mapper, x1, y1);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9005
9006 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009007 processSlot(mapper, SECOND_SLOT);
9008 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009009 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009012 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9014
9015 // If the tool type of the first finger changes to MT_TOOL_PALM,
9016 // we expect to receive ACTION_POINTER_UP with cancel flag.
9017 processSlot(mapper, FIRST_SLOT);
9018 processId(mapper, FIRST_TRACKING_ID);
9019 processToolType(mapper, MT_TOOL_PALM);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009022 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009023 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9024
9025 // The following MOVE events of second finger should be processed.
9026 processSlot(mapper, SECOND_SLOT);
9027 processId(mapper, SECOND_TRACKING_ID);
9028 processPosition(mapper, x2 + 1, y2 + 1);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9032 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9033
9034 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9035 // it. Second finger receive move.
9036 processSlot(mapper, FIRST_SLOT);
9037 processId(mapper, INVALID_TRACKING_ID);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9041 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9042
9043 // Second finger keeps moving.
9044 processSlot(mapper, SECOND_SLOT);
9045 processId(mapper, SECOND_TRACKING_ID);
9046 processPosition(mapper, x2 + 2, y2 + 2);
9047 processSync(mapper);
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9050 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9051
9052 // Second finger up.
9053 processId(mapper, INVALID_TRACKING_ID);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9057 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9058}
9059
9060/**
9061 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9062 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9063 */
9064TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9065 addConfigurationProperty("touch.deviceType", "touchScreen");
9066 prepareDisplay(DISPLAY_ORIENTATION_0);
9067 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9069
9070 NotifyMotionArgs motionArgs;
9071
9072 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9073 // First finger down.
9074 processId(mapper, FIRST_TRACKING_ID);
9075 processPosition(mapper, x1, y1);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9080
9081 // Second finger down.
9082 processSlot(mapper, SECOND_SLOT);
9083 processId(mapper, SECOND_TRACKING_ID);
9084 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009085 processSync(mapper);
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009087 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9089
arthurhungcc7f9802020-04-30 17:55:40 +08009090 // If the tool type of the first finger changes to MT_TOOL_PALM,
9091 // we expect to receive ACTION_POINTER_UP with cancel flag.
9092 processSlot(mapper, FIRST_SLOT);
9093 processId(mapper, FIRST_TRACKING_ID);
9094 processToolType(mapper, MT_TOOL_PALM);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009097 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009098 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9099
9100 // Second finger keeps moving.
9101 processSlot(mapper, SECOND_SLOT);
9102 processId(mapper, SECOND_TRACKING_ID);
9103 processPosition(mapper, x2 + 1, y2 + 1);
9104 processSync(mapper);
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9107
9108 // second finger becomes palm, receive cancel due to only 1 finger is active.
9109 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009110 processToolType(mapper, MT_TOOL_PALM);
9111 processSync(mapper);
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9114
arthurhungcc7f9802020-04-30 17:55:40 +08009115 // third finger down.
9116 processSlot(mapper, THIRD_SLOT);
9117 processId(mapper, THIRD_TRACKING_ID);
9118 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009119 processPosition(mapper, x3, y3);
9120 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009124 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9125
9126 // third finger move
9127 processId(mapper, THIRD_TRACKING_ID);
9128 processPosition(mapper, x3 + 1, y3 + 1);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9132
9133 // first finger up, third finger receive move.
9134 processSlot(mapper, FIRST_SLOT);
9135 processId(mapper, INVALID_TRACKING_ID);
9136 processSync(mapper);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9139 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9140
9141 // second finger up, third finger receive move.
9142 processSlot(mapper, SECOND_SLOT);
9143 processId(mapper, INVALID_TRACKING_ID);
9144 processSync(mapper);
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9147 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9148
9149 // third finger up.
9150 processSlot(mapper, THIRD_SLOT);
9151 processId(mapper, INVALID_TRACKING_ID);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9155 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9156}
9157
9158/**
9159 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9160 * and the active finger could still be allowed to receive the events
9161 */
9162TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9163 addConfigurationProperty("touch.deviceType", "touchScreen");
9164 prepareDisplay(DISPLAY_ORIENTATION_0);
9165 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9166 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9167
9168 NotifyMotionArgs motionArgs;
9169
9170 // default tool type is finger
9171 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9172 processId(mapper, FIRST_TRACKING_ID);
9173 processPosition(mapper, x1, y1);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9176 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9178
9179 // Second finger down.
9180 processSlot(mapper, SECOND_SLOT);
9181 processId(mapper, SECOND_TRACKING_ID);
9182 processPosition(mapper, x2, y2);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009185 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9187
9188 // If the tool type of the second finger changes to MT_TOOL_PALM,
9189 // we expect to receive ACTION_POINTER_UP with cancel flag.
9190 processId(mapper, SECOND_TRACKING_ID);
9191 processToolType(mapper, MT_TOOL_PALM);
9192 processSync(mapper);
9193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009194 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009195 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9196
9197 // The following MOVE event should be processed.
9198 processSlot(mapper, FIRST_SLOT);
9199 processId(mapper, FIRST_TRACKING_ID);
9200 processPosition(mapper, x1 + 1, y1 + 1);
9201 processSync(mapper);
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9204 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9205
9206 // second finger up.
9207 processSlot(mapper, SECOND_SLOT);
9208 processId(mapper, INVALID_TRACKING_ID);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9212
9213 // first finger keep moving
9214 processSlot(mapper, FIRST_SLOT);
9215 processId(mapper, FIRST_TRACKING_ID);
9216 processPosition(mapper, x1 + 2, y1 + 2);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9220
9221 // first finger up.
9222 processId(mapper, INVALID_TRACKING_ID);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9226 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009227}
9228
Arthur Hung9ad18942021-06-19 02:04:46 +00009229/**
9230 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9231 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9232 * cause slot be valid again.
9233 */
9234TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9235 addConfigurationProperty("touch.deviceType", "touchScreen");
9236 prepareDisplay(DISPLAY_ORIENTATION_0);
9237 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9238 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9239
9240 NotifyMotionArgs motionArgs;
9241
9242 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9243 // First finger down.
9244 processId(mapper, FIRST_TRACKING_ID);
9245 processPosition(mapper, x1, y1);
9246 processPressure(mapper, RAW_PRESSURE_MAX);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9250 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9251
9252 // First finger move.
9253 processId(mapper, FIRST_TRACKING_ID);
9254 processPosition(mapper, x1 + 1, y1 + 1);
9255 processPressure(mapper, RAW_PRESSURE_MAX);
9256 processSync(mapper);
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9259 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9260
9261 // Second finger down.
9262 processSlot(mapper, SECOND_SLOT);
9263 processId(mapper, SECOND_TRACKING_ID);
9264 processPosition(mapper, x2, y2);
9265 processPressure(mapper, RAW_PRESSURE_MAX);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009268 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009269 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9270
9271 // second finger up with some unexpected data.
9272 processSlot(mapper, SECOND_SLOT);
9273 processId(mapper, INVALID_TRACKING_ID);
9274 processPosition(mapper, x2, y2);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009277 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009278 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9279
9280 // first finger up with some unexpected data.
9281 processSlot(mapper, FIRST_SLOT);
9282 processId(mapper, INVALID_TRACKING_ID);
9283 processPosition(mapper, x2, y2);
9284 processPressure(mapper, RAW_PRESSURE_MAX);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9287 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9288 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9289}
9290
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009291// --- MultiTouchInputMapperTest_ExternalDevice ---
9292
9293class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9294protected:
Chris Yea52ade12020-08-27 16:49:20 -07009295 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009296};
9297
9298/**
9299 * Expect fallback to internal viewport if device is external and external viewport is not present.
9300 */
9301TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9302 prepareAxes(POSITION);
9303 addConfigurationProperty("touch.deviceType", "touchScreen");
9304 prepareDisplay(DISPLAY_ORIENTATION_0);
9305 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9306
9307 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9308
9309 NotifyMotionArgs motionArgs;
9310
9311 // Expect the event to be sent to the internal viewport,
9312 // because an external viewport is not present.
9313 processPosition(mapper, 100, 100);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9317
9318 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009319 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009320 processPosition(mapper, 100, 100);
9321 processSync(mapper);
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9323 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9324}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009325
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009326TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9327 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9328 std::shared_ptr<FakePointerController> fakePointerController =
9329 std::make_shared<FakePointerController>();
9330 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9331 fakePointerController->setPosition(0, 0);
9332 fakePointerController->setButtonState(0);
9333
9334 // prepare device and capture
9335 prepareDisplay(DISPLAY_ORIENTATION_0);
9336 prepareAxes(POSITION | ID | SLOT);
9337 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9338 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9339 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009340 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9342
9343 // captured touchpad should be a touchpad source
9344 NotifyDeviceResetArgs resetArgs;
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9346 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9347
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009348 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009349
9350 const InputDeviceInfo::MotionRange* relRangeX =
9351 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9352 ASSERT_NE(relRangeX, nullptr);
9353 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9354 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9355 const InputDeviceInfo::MotionRange* relRangeY =
9356 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9357 ASSERT_NE(relRangeY, nullptr);
9358 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9359 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9360
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009361 // run captured pointer tests - note that this is unscaled, so input listener events should be
9362 // identical to what the hardware sends (accounting for any
9363 // calibration).
9364 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009365 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009366 processId(mapper, 1);
9367 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9368 processKey(mapper, BTN_TOUCH, 1);
9369 processSync(mapper);
9370
9371 // expect coord[0] to contain initial location of touch 0
9372 NotifyMotionArgs args;
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9374 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9375 ASSERT_EQ(1U, args.pointerCount);
9376 ASSERT_EQ(0, args.pointerProperties[0].id);
9377 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9378 ASSERT_NO_FATAL_FAILURE(
9379 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9380
9381 // FINGER 1 DOWN
9382 processSlot(mapper, 1);
9383 processId(mapper, 2);
9384 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9385 processSync(mapper);
9386
9387 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009389 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009390 ASSERT_EQ(2U, args.pointerCount);
9391 ASSERT_EQ(0, args.pointerProperties[0].id);
9392 ASSERT_EQ(1, args.pointerProperties[1].id);
9393 ASSERT_NO_FATAL_FAILURE(
9394 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9395 ASSERT_NO_FATAL_FAILURE(
9396 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9397
9398 // FINGER 1 MOVE
9399 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9400 processSync(mapper);
9401
9402 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9403 // from move
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9406 ASSERT_NO_FATAL_FAILURE(
9407 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9408 ASSERT_NO_FATAL_FAILURE(
9409 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9410
9411 // FINGER 0 MOVE
9412 processSlot(mapper, 0);
9413 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9414 processSync(mapper);
9415
9416 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9419 ASSERT_NO_FATAL_FAILURE(
9420 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9421 ASSERT_NO_FATAL_FAILURE(
9422 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9423
9424 // BUTTON DOWN
9425 processKey(mapper, BTN_LEFT, 1);
9426 processSync(mapper);
9427
9428 // touchinputmapper design sends a move before button press
9429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9432 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9433
9434 // BUTTON UP
9435 processKey(mapper, BTN_LEFT, 0);
9436 processSync(mapper);
9437
9438 // touchinputmapper design sends a move after button release
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9440 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9443
9444 // FINGER 0 UP
9445 processId(mapper, -1);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9449
9450 // FINGER 1 MOVE
9451 processSlot(mapper, 1);
9452 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9453 processSync(mapper);
9454
9455 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9458 ASSERT_EQ(1U, args.pointerCount);
9459 ASSERT_EQ(1, args.pointerProperties[0].id);
9460 ASSERT_NO_FATAL_FAILURE(
9461 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9462
9463 // FINGER 1 UP
9464 processId(mapper, -1);
9465 processKey(mapper, BTN_TOUCH, 0);
9466 processSync(mapper);
9467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9469
9470 // non captured touchpad should be a mouse source
9471 mFakePolicy->setPointerCapture(false);
9472 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9474 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9475}
9476
9477TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9478 std::shared_ptr<FakePointerController> fakePointerController =
9479 std::make_shared<FakePointerController>();
9480 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9481 fakePointerController->setPosition(0, 0);
9482 fakePointerController->setButtonState(0);
9483
9484 // prepare device and capture
9485 prepareDisplay(DISPLAY_ORIENTATION_0);
9486 prepareAxes(POSITION | ID | SLOT);
9487 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9488 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009489 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009490 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9491 // run uncaptured pointer tests - pushes out generic events
9492 // FINGER 0 DOWN
9493 processId(mapper, 3);
9494 processPosition(mapper, 100, 100);
9495 processKey(mapper, BTN_TOUCH, 1);
9496 processSync(mapper);
9497
9498 // start at (100,100), cursor should be at (0,0) * scale
9499 NotifyMotionArgs args;
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9502 ASSERT_NO_FATAL_FAILURE(
9503 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9504
9505 // FINGER 0 MOVE
9506 processPosition(mapper, 200, 200);
9507 processSync(mapper);
9508
9509 // compute scaling to help with touch position checking
9510 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9511 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9512 float scale =
9513 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9514
9515 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9517 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9519 0, 0, 0, 0, 0, 0, 0));
9520}
9521
9522TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9523 std::shared_ptr<FakePointerController> fakePointerController =
9524 std::make_shared<FakePointerController>();
9525
9526 prepareDisplay(DISPLAY_ORIENTATION_0);
9527 prepareAxes(POSITION | ID | SLOT);
9528 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009529 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009530 mFakePolicy->setPointerCapture(false);
9531 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9532
9533 // uncaptured touchpad should be a pointer device
9534 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9535
9536 // captured touchpad should be a touchpad device
9537 mFakePolicy->setPointerCapture(true);
9538 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9539 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9540}
9541
HQ Liue6983c72022-04-19 22:14:56 +00009542class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9543protected:
9544 float mPointerMovementScale;
9545 float mPointerXZoomScale;
9546 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9547 addConfigurationProperty("touch.deviceType", "pointer");
9548 std::shared_ptr<FakePointerController> fakePointerController =
9549 std::make_shared<FakePointerController>();
9550 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9551 fakePointerController->setPosition(0, 0);
9552 fakePointerController->setButtonState(0);
9553 prepareDisplay(DISPLAY_ORIENTATION_0);
9554
9555 prepareAxes(POSITION);
9556 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9557 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9558 // needs to be disabled, and the pointer gesture needs to be enabled.
9559 mFakePolicy->setPointerCapture(false);
9560 mFakePolicy->setPointerGestureEnabled(true);
9561 mFakePolicy->setPointerController(fakePointerController);
9562
9563 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9564 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9565 mPointerMovementScale =
9566 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9567 mPointerXZoomScale =
9568 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9569 }
9570
9571 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9572 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9573 /*flat*/ 0,
9574 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9575 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9576 /*flat*/ 0,
9577 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9578 }
9579};
9580
9581/**
9582 * Two fingers down on a pointer mode touch pad. The width
9583 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9584 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9585 * be greater than the both value to be freeform gesture, so that after two
9586 * fingers start to move downwards, the gesture should be swipe.
9587 */
9588TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9589 // The min freeform gesture width is 25units/mm x 30mm = 750
9590 // which is greater than fraction of the diagnal length of the touchpad (349).
9591 // Thus, MaxSwipWidth is 750.
9592 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9594 NotifyMotionArgs motionArgs;
9595
9596 // Two fingers down at once.
9597 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9598 // Pointer's initial position is used the [0,0] coordinate.
9599 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9600
9601 processId(mapper, FIRST_TRACKING_ID);
9602 processPosition(mapper, x1, y1);
9603 processMTSync(mapper);
9604 processId(mapper, SECOND_TRACKING_ID);
9605 processPosition(mapper, x2, y2);
9606 processMTSync(mapper);
9607 processSync(mapper);
9608
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9610 ASSERT_EQ(1U, motionArgs.pointerCount);
9611 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9613 ASSERT_NO_FATAL_FAILURE(
9614 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9615
9616 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9617 // that there should be 1 pointer.
9618 int32_t movingDistance = 200;
9619 y1 += movingDistance;
9620 y2 += movingDistance;
9621
9622 processId(mapper, FIRST_TRACKING_ID);
9623 processPosition(mapper, x1, y1);
9624 processMTSync(mapper);
9625 processId(mapper, SECOND_TRACKING_ID);
9626 processPosition(mapper, x2, y2);
9627 processMTSync(mapper);
9628 processSync(mapper);
9629
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9631 ASSERT_EQ(1U, motionArgs.pointerCount);
9632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9633 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9635 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9636 0, 0, 0, 0));
9637}
9638
9639/**
9640 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9641 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9642 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9643 * value to be freeform gesture, so that after two fingers start to move downwards,
9644 * the gesture should be swipe.
9645 */
9646TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9647 // The min freeform gesture width is 5units/mm x 30mm = 150
9648 // which is greater than fraction of the diagnal length of the touchpad (349).
9649 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9650 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9651 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9652 NotifyMotionArgs motionArgs;
9653
9654 // Two fingers down at once.
9655 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9656 // Pointer's initial position is used the [0,0] coordinate.
9657 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9658
9659 processId(mapper, FIRST_TRACKING_ID);
9660 processPosition(mapper, x1, y1);
9661 processMTSync(mapper);
9662 processId(mapper, SECOND_TRACKING_ID);
9663 processPosition(mapper, x2, y2);
9664 processMTSync(mapper);
9665 processSync(mapper);
9666
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9668 ASSERT_EQ(1U, motionArgs.pointerCount);
9669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9671 ASSERT_NO_FATAL_FAILURE(
9672 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9673
9674 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9675 // and there should be 1 pointer.
9676 int32_t movingDistance = 200;
9677 y1 += movingDistance;
9678 y2 += movingDistance;
9679
9680 processId(mapper, FIRST_TRACKING_ID);
9681 processPosition(mapper, x1, y1);
9682 processMTSync(mapper);
9683 processId(mapper, SECOND_TRACKING_ID);
9684 processPosition(mapper, x2, y2);
9685 processMTSync(mapper);
9686 processSync(mapper);
9687
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9689 ASSERT_EQ(1U, motionArgs.pointerCount);
9690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9692 // New coordinate is the scaled relative coordinate from the initial coordinate.
9693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9694 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9695 0, 0, 0, 0));
9696}
9697
9698/**
9699 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9700 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9701 * freeform gestures after two fingers start to move downwards.
9702 */
9703TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9704 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9705 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9706
9707 NotifyMotionArgs motionArgs;
9708
9709 // Two fingers down at once. Wider than the max swipe width.
9710 // The gesture is expected to be PRESS, then transformed to FREEFORM
9711 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9712
9713 processId(mapper, FIRST_TRACKING_ID);
9714 processPosition(mapper, x1, y1);
9715 processMTSync(mapper);
9716 processId(mapper, SECOND_TRACKING_ID);
9717 processPosition(mapper, x2, y2);
9718 processMTSync(mapper);
9719 processSync(mapper);
9720
9721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9722 ASSERT_EQ(1U, motionArgs.pointerCount);
9723 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9725 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9726 ASSERT_NO_FATAL_FAILURE(
9727 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9728
9729 int32_t movingDistance = 200;
9730
9731 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9732 // then two down events for two pointers.
9733 y1 += movingDistance;
9734 y2 += movingDistance;
9735
9736 processId(mapper, FIRST_TRACKING_ID);
9737 processPosition(mapper, x1, y1);
9738 processMTSync(mapper);
9739 processId(mapper, SECOND_TRACKING_ID);
9740 processPosition(mapper, x2, y2);
9741 processMTSync(mapper);
9742 processSync(mapper);
9743
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9745 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9746 ASSERT_EQ(1U, motionArgs.pointerCount);
9747 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9750 ASSERT_EQ(1U, motionArgs.pointerCount);
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9754 ASSERT_EQ(2U, motionArgs.pointerCount);
9755 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9757 // Two pointers' scaled relative coordinates from their initial centroid.
9758 // Initial y coordinates are 0 as y1 and y2 have the same value.
9759 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9760 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9761 // When pointers move, the new coordinates equal to the initial coordinates plus
9762 // scaled moving distance.
9763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9764 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9765 0, 0, 0, 0));
9766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9767 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9768 0, 0, 0, 0));
9769
9770 // Move two fingers down again, expect one MOVE motion event.
9771 y1 += movingDistance;
9772 y2 += movingDistance;
9773
9774 processId(mapper, FIRST_TRACKING_ID);
9775 processPosition(mapper, x1, y1);
9776 processMTSync(mapper);
9777 processId(mapper, SECOND_TRACKING_ID);
9778 processPosition(mapper, x2, y2);
9779 processMTSync(mapper);
9780 processSync(mapper);
9781
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(2U, motionArgs.pointerCount);
9784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9787 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9788 0, 0, 0, 0, 0));
9789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9790 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9791 0, 0, 0, 0, 0));
9792}
9793
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009794// --- JoystickInputMapperTest ---
9795
9796class JoystickInputMapperTest : public InputMapperTest {
9797protected:
9798 static const int32_t RAW_X_MIN;
9799 static const int32_t RAW_X_MAX;
9800 static const int32_t RAW_Y_MIN;
9801 static const int32_t RAW_Y_MAX;
9802
9803 void SetUp() override {
9804 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9805 }
9806 void prepareAxes() {
9807 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9808 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9809 }
9810
9811 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9813 }
9814
9815 void processSync(JoystickInputMapper& mapper) {
9816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9817 }
9818
9819 void prepareVirtualDisplay(int32_t orientation) {
9820 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9821 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9822 NO_PORT, ViewportType::VIRTUAL);
9823 }
9824};
9825
9826const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9827const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9828const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9829const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9830
9831TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9832 prepareAxes();
9833 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9834
9835 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9836
9837 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9838
9839 // Send an axis event
9840 processAxis(mapper, ABS_X, 100);
9841 processSync(mapper);
9842
9843 NotifyMotionArgs args;
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9845 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9846
9847 // Send another axis event
9848 processAxis(mapper, ABS_Y, 100);
9849 processSync(mapper);
9850
9851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9852 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9853}
9854
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009855// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009856
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009857class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009858protected:
9859 static const char* DEVICE_NAME;
9860 static const char* DEVICE_LOCATION;
9861 static const int32_t DEVICE_ID;
9862 static const int32_t DEVICE_GENERATION;
9863 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009864 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009865 static const int32_t EVENTHUB_ID;
9866
9867 std::shared_ptr<FakeEventHub> mFakeEventHub;
9868 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009869 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009870 std::unique_ptr<InstrumentedInputReader> mReader;
9871 std::shared_ptr<InputDevice> mDevice;
9872
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009873 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009874 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009875 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009876 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009877 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009878 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009879 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9880 }
9881
9882 void SetUp() override { SetUp(DEVICE_CLASSES); }
9883
9884 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009885 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009886 mFakePolicy.clear();
9887 }
9888
9889 void configureDevice(uint32_t changes) {
9890 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9891 mReader->requestRefreshConfiguration(changes);
9892 mReader->loopOnce();
9893 }
9894 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9895 }
9896
9897 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9898 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009899 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009900 InputDeviceIdentifier identifier;
9901 identifier.name = name;
9902 identifier.location = location;
9903 std::shared_ptr<InputDevice> device =
9904 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9905 identifier);
9906 mReader->pushNextDevice(device);
9907 mFakeEventHub->addDevice(eventHubId, name, classes);
9908 mReader->loopOnce();
9909 return device;
9910 }
9911
9912 template <class T, typename... Args>
9913 T& addControllerAndConfigure(Args... args) {
9914 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9915
9916 return controller;
9917 }
9918};
9919
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009920const char* PeripheralControllerTest::DEVICE_NAME = "device";
9921const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9922const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9923const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9924const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009925const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
9926 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009927const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009928
9929// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009930class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009931protected:
9932 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009933 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009934 }
9935};
9936
9937TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009938 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009939
9940 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9941 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9942}
9943
9944TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009945 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009946
9947 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9948 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9949}
9950
9951// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009952class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009953protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009954 void SetUp() override {
9955 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9956 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009957};
9958
Chris Ye85758332021-05-16 23:05:17 -07009959TEST_F(LightControllerTest, MonoLight) {
9960 RawLightInfo infoMono = {.id = 1,
9961 .name = "Mono",
9962 .maxBrightness = 255,
9963 .flags = InputLightClass::BRIGHTNESS,
9964 .path = ""};
9965 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009966
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009967 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009968 InputDeviceInfo info;
9969 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009970 std::vector<InputDeviceLightInfo> lights = info.getLights();
9971 ASSERT_EQ(1U, lights.size());
9972 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009973
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009974 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9975 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009976}
9977
9978TEST_F(LightControllerTest, RGBLight) {
9979 RawLightInfo infoRed = {.id = 1,
9980 .name = "red",
9981 .maxBrightness = 255,
9982 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9983 .path = ""};
9984 RawLightInfo infoGreen = {.id = 2,
9985 .name = "green",
9986 .maxBrightness = 255,
9987 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9988 .path = ""};
9989 RawLightInfo infoBlue = {.id = 3,
9990 .name = "blue",
9991 .maxBrightness = 255,
9992 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9993 .path = ""};
9994 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9995 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9996 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9997
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009998 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009999 InputDeviceInfo info;
10000 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010001 std::vector<InputDeviceLightInfo> lights = info.getLights();
10002 ASSERT_EQ(1U, lights.size());
10003 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010004
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010005 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10006 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010007}
10008
10009TEST_F(LightControllerTest, MultiColorRGBLight) {
10010 RawLightInfo infoColor = {.id = 1,
10011 .name = "red",
10012 .maxBrightness = 255,
10013 .flags = InputLightClass::BRIGHTNESS |
10014 InputLightClass::MULTI_INTENSITY |
10015 InputLightClass::MULTI_INDEX,
10016 .path = ""};
10017
10018 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10019
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010020 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010021 InputDeviceInfo info;
10022 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010023 std::vector<InputDeviceLightInfo> lights = info.getLights();
10024 ASSERT_EQ(1U, lights.size());
10025 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010026
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010027 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10028 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010029}
10030
10031TEST_F(LightControllerTest, PlayerIdLight) {
10032 RawLightInfo info1 = {.id = 1,
10033 .name = "player1",
10034 .maxBrightness = 255,
10035 .flags = InputLightClass::BRIGHTNESS,
10036 .path = ""};
10037 RawLightInfo info2 = {.id = 2,
10038 .name = "player2",
10039 .maxBrightness = 255,
10040 .flags = InputLightClass::BRIGHTNESS,
10041 .path = ""};
10042 RawLightInfo info3 = {.id = 3,
10043 .name = "player3",
10044 .maxBrightness = 255,
10045 .flags = InputLightClass::BRIGHTNESS,
10046 .path = ""};
10047 RawLightInfo info4 = {.id = 4,
10048 .name = "player4",
10049 .maxBrightness = 255,
10050 .flags = InputLightClass::BRIGHTNESS,
10051 .path = ""};
10052 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10053 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10054 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10055 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10056
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010057 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010058 InputDeviceInfo info;
10059 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010060 std::vector<InputDeviceLightInfo> lights = info.getLights();
10061 ASSERT_EQ(1U, lights.size());
10062 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010063
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010064 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10065 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10066 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010067}
10068
Michael Wrightd02c5b62014-02-10 15:10:22 -080010069} // namespace android