blob: 9f33d23af5dfc16f067fef86d1be381484c026fa [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
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000023#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070024#include <KeyboardInputMapper.h>
25#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070026#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070027#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <SingleTouchInputMapper.h>
29#include <SwitchInputMapper.h>
30#include <TestInputListener.h>
31#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080032#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000033#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070034#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080035#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050036#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080037#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080038#include <math.h>
39
Michael Wright17db18e2020-06-26 20:51:44 +010040#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080041#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000042#include "input/DisplayViewport.h"
43#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010044
Michael Wrightd02c5b62014-02-10 15:10:22 -080045namespace android {
46
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070047using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070048using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070049
50// Timeout for waiting for an expected event
51static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000054static constexpr nsecs_t ARBITRARY_TIME = 1234;
55static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080056
57// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080058static constexpr int32_t DISPLAY_ID = 0;
59static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
60static constexpr int32_t DISPLAY_WIDTH = 480;
61static constexpr int32_t DISPLAY_HEIGHT = 800;
62static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
63static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
64static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070065static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070066static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080067
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t FIRST_SLOT = 0;
69static constexpr int32_t SECOND_SLOT = 1;
70static constexpr int32_t THIRD_SLOT = 2;
71static constexpr int32_t INVALID_TRACKING_ID = -1;
72static constexpr int32_t FIRST_TRACKING_ID = 0;
73static constexpr int32_t SECOND_TRACKING_ID = 1;
74static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080075static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080076static constexpr int32_t BATTERY_STATUS = 4;
77static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
94template<typename T>
95static inline T min(T a, T b) {
96 return a < b ? a : b;
97}
98
99static inline float avg(float x, float y) {
100 return (x + y) / 2;
101}
102
Chris Ye3fdbfef2021-01-06 18:45:18 -0800103// Mapping for light color name and the light color
104const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
105 {"green", LightColor::GREEN},
106 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700108static int32_t getInverseRotation(int32_t orientation) {
109 switch (orientation) {
110 case DISPLAY_ORIENTATION_90:
111 return DISPLAY_ORIENTATION_270;
112 case DISPLAY_ORIENTATION_270:
113 return DISPLAY_ORIENTATION_90;
114 default:
115 return orientation;
116 }
117}
118
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800119static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
120 InputDeviceInfo info;
121 mapper.populateDeviceInfo(&info);
122
123 const InputDeviceInfo::MotionRange* motionRange =
124 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
125 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
126}
127
128static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
129 InputDeviceInfo info;
130 mapper.populateDeviceInfo(&info);
131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_EQ(nullptr, motionRange);
135}
136
Michael Wrightd02c5b62014-02-10 15:10:22 -0800137// --- FakePointerController ---
138
139class FakePointerController : public PointerControllerInterface {
140 bool mHaveBounds;
141 float mMinX, mMinY, mMaxX, mMaxY;
142 float mX, mY;
143 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800144 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145
Michael Wrightd02c5b62014-02-10 15:10:22 -0800146public:
147 FakePointerController() :
148 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800149 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800150 }
151
Michael Wright17db18e2020-06-26 20:51:44 +0100152 virtual ~FakePointerController() {}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154 void setBounds(float minX, float minY, float maxX, float maxY) {
155 mHaveBounds = true;
156 mMinX = minX;
157 mMinY = minY;
158 mMaxX = maxX;
159 mMaxY = maxY;
160 }
161
Chris Yea52ade12020-08-27 16:49:20 -0700162 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163 mX = x;
164 mY = y;
165 }
166
Chris Yea52ade12020-08-27 16:49:20 -0700167 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Chris Yea52ade12020-08-27 16:49:20 -0700169 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Chris Yea52ade12020-08-27 16:49:20 -0700171 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 *outX = mX;
173 *outY = mY;
174 }
175
Chris Yea52ade12020-08-27 16:49:20 -0700176 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800177
Chris Yea52ade12020-08-27 16:49:20 -0700178 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800179 mDisplayId = viewport.displayId;
180 }
181
Arthur Hung7c645402019-01-25 17:45:42 +0800182 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
183 return mSpotsByDisplay;
184 }
185
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186private:
Chris Yea52ade12020-08-27 16:49:20 -0700187 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188 *outMinX = mMinX;
189 *outMinY = mMinY;
190 *outMaxX = mMaxX;
191 *outMaxY = mMaxY;
192 return mHaveBounds;
193 }
194
Chris Yea52ade12020-08-27 16:49:20 -0700195 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800196 mX += deltaX;
197 if (mX < mMinX) mX = mMinX;
198 if (mX > mMaxX) mX = mMaxX;
199 mY += deltaY;
200 if (mY < mMinY) mY = mMinY;
201 if (mY > mMaxY) mY = mMaxY;
202 }
203
Chris Yea52ade12020-08-27 16:49:20 -0700204 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205
Chris Yea52ade12020-08-27 16:49:20 -0700206 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207
Chris Yea52ade12020-08-27 16:49:20 -0700208 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209
Chris Yea52ade12020-08-27 16:49:20 -0700210 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
211 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800212 std::vector<int32_t> newSpots;
213 // Add spots for fingers that are down.
214 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
215 uint32_t id = idBits.clearFirstMarkedBit();
216 newSpots.push_back(id);
217 }
218
219 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800223
224 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225};
226
227
228// --- FakeInputReaderPolicy ---
229
230class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700231 std::mutex mLock;
232 std::condition_variable mDevicesChangedCondition;
233
Michael Wrightd02c5b62014-02-10 15:10:22 -0800234 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000235 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
237 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100238 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700239 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800240
241protected:
Chris Yea52ade12020-08-27 16:49:20 -0700242 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243
244public:
245 FakeInputReaderPolicy() {
246 }
247
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700248 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800249 waitForInputDevices([](bool devicesChanged) {
250 if (!devicesChanged) {
251 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
252 }
253 });
254 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700255
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800256 void assertInputDevicesNotChanged() {
257 waitForInputDevices([](bool devicesChanged) {
258 if (devicesChanged) {
259 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
260 }
261 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700262 }
263
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700264 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100266 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700267 }
268
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700269 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
270 return mConfig.getDisplayViewportByUniqueId(uniqueId);
271 }
272 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
273 return mConfig.getDisplayViewportByType(type);
274 }
275
276 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
277 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700278 }
279
Prabir Pradhan5632d622021-09-06 07:57:20 -0700280 void addDisplayViewport(DisplayViewport viewport) {
281 mViewports.push_back(std::move(viewport));
282 mConfig.setDisplayViewports(mViewports);
283 }
284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700285 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000286 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700287 std::optional<uint8_t> physicalPort, ViewportType type) {
288 const bool isRotated =
289 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
290 DisplayViewport v;
291 v.displayId = displayId;
292 v.orientation = orientation;
293 v.logicalLeft = 0;
294 v.logicalTop = 0;
295 v.logicalRight = isRotated ? height : width;
296 v.logicalBottom = isRotated ? width : height;
297 v.physicalLeft = 0;
298 v.physicalTop = 0;
299 v.physicalRight = isRotated ? height : width;
300 v.physicalBottom = isRotated ? width : height;
301 v.deviceWidth = isRotated ? height : width;
302 v.deviceHeight = isRotated ? width : height;
303 v.isActive = isActive;
304 v.uniqueId = uniqueId;
305 v.physicalPort = physicalPort;
306 v.type = type;
307
308 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 }
310
Arthur Hung6cd19a42019-08-30 19:04:12 +0800311 bool updateViewport(const DisplayViewport& viewport) {
312 size_t count = mViewports.size();
313 for (size_t i = 0; i < count; i++) {
314 const DisplayViewport& currentViewport = mViewports[i];
315 if (currentViewport.displayId == viewport.displayId) {
316 mViewports[i] = viewport;
317 mConfig.setDisplayViewports(mViewports);
318 return true;
319 }
320 }
321 // no viewport found.
322 return false;
323 }
324
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100325 void addExcludedDeviceName(const std::string& deviceName) {
326 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700329 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
330 mConfig.portAssociations.insert({inputPort, displayPort});
331 }
332
Christine Franks1ba71cc2021-04-07 14:37:42 -0700333 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
334 const std::string& displayUniqueId) {
335 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
336 }
337
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000338 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700339
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000340 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700341
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000342 void setPointerController(std::shared_ptr<FakePointerController> controller) {
343 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 }
345
346 const InputReaderConfiguration* getReaderConfiguration() const {
347 return &mConfig;
348 }
349
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800350 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return mInputDevices;
352 }
353
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100354 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700355 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700356 return transform;
357 }
358
359 void setTouchAffineTransformation(const TouchAffineTransformation t) {
360 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800361 }
362
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000363 PointerCaptureRequest setPointerCapture(bool enabled) {
364 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
365 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800366 }
367
Arthur Hung7c645402019-01-25 17:45:42 +0800368 void setShowTouches(bool enabled) {
369 mConfig.showTouches = enabled;
370 }
371
Garfield Tan888a6a42020-01-09 11:39:16 -0800372 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
373 mConfig.defaultPointerDisplayId = pointerDisplayId;
374 }
375
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800376 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
377
Michael Wrightd02c5b62014-02-10 15:10:22 -0800378private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000379 uint32_t mNextPointerCaptureSequenceNumber = 0;
380
Chris Yea52ade12020-08-27 16:49:20 -0700381 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 *outConfig = mConfig;
383 }
384
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000385 std::shared_ptr<PointerControllerInterface> obtainPointerController(
386 int32_t /*deviceId*/) override {
387 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800388 }
389
Chris Yea52ade12020-08-27 16:49:20 -0700390 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700391 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800392 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700393 mInputDevicesChanged = true;
394 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800395 }
396
Chris Yea52ade12020-08-27 16:49:20 -0700397 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
398 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700399 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400 }
401
Chris Yea52ade12020-08-27 16:49:20 -0700402 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800403
404 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
405 std::unique_lock<std::mutex> lock(mLock);
406 base::ScopedLockAssertion assumeLocked(mLock);
407
408 const bool devicesChanged =
409 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
410 return mInputDevicesChanged;
411 });
412 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
413 mInputDevicesChanged = false;
414 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415};
416
Michael Wrightd02c5b62014-02-10 15:10:22 -0800417// --- FakeEventHub ---
418
419class FakeEventHub : public EventHubInterface {
420 struct KeyInfo {
421 int32_t keyCode;
422 uint32_t flags;
423 };
424
Chris Yef59a2f42020-10-16 12:55:26 -0700425 struct SensorInfo {
426 InputDeviceSensorType sensorType;
427 int32_t sensorDataIndex;
428 };
429
Michael Wrightd02c5b62014-02-10 15:10:22 -0800430 struct Device {
431 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700432 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 PropertyMap configuration;
434 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
435 KeyedVector<int, bool> relativeAxes;
436 KeyedVector<int32_t, int32_t> keyCodeStates;
437 KeyedVector<int32_t, int32_t> scanCodeStates;
438 KeyedVector<int32_t, int32_t> switchStates;
439 KeyedVector<int32_t, int32_t> absoluteAxisValue;
440 KeyedVector<int32_t, KeyInfo> keysByScanCode;
441 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
442 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100443 // fake mapping which would normally come from keyCharacterMap
444 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700445 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
446 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800447 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700448 bool enabled;
449
450 status_t enable() {
451 enabled = true;
452 return OK;
453 }
454
455 status_t disable() {
456 enabled = false;
457 return OK;
458 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800459
Chris Ye1b0c7342020-07-28 21:57:03 -0700460 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800461 };
462
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700463 std::mutex mLock;
464 std::condition_variable mEventsCondition;
465
Michael Wrightd02c5b62014-02-10 15:10:22 -0800466 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100467 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000468 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600469 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000470 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800471 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
472 // Simulates a device light brightness, from light id to light brightness.
473 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
474 // Simulates a device light intensities, from light id to light intensities map.
475 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
476 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800477
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700478public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800479 virtual ~FakeEventHub() {
480 for (size_t i = 0; i < mDevices.size(); i++) {
481 delete mDevices.valueAt(i);
482 }
483 }
484
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 FakeEventHub() { }
486
Chris Ye1b0c7342020-07-28 21:57:03 -0700487 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800488 Device* device = new Device(classes);
489 device->identifier.name = name;
490 mDevices.add(deviceId, device);
491
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000492 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493 }
494
495 void removeDevice(int32_t deviceId) {
496 delete mDevices.valueFor(deviceId);
497 mDevices.removeItem(deviceId);
498
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000499 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800500 }
501
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700502 bool isDeviceEnabled(int32_t deviceId) {
503 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700504 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700505 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
506 return false;
507 }
508 return device->enabled;
509 }
510
511 status_t enableDevice(int32_t deviceId) {
512 status_t result;
513 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700514 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700515 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
516 return BAD_VALUE;
517 }
518 if (device->enabled) {
519 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
520 return OK;
521 }
522 result = device->enable();
523 return result;
524 }
525
526 status_t disableDevice(int32_t deviceId) {
527 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700528 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700529 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
530 return BAD_VALUE;
531 }
532 if (!device->enabled) {
533 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
534 return OK;
535 }
536 return device->disable();
537 }
538
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000540 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800541 }
542
543 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
544 Device* device = getDevice(deviceId);
545 device->configuration.addProperty(key, value);
546 }
547
548 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
549 Device* device = getDevice(deviceId);
550 device->configuration.addAll(configuration);
551 }
552
553 void addAbsoluteAxis(int32_t deviceId, int axis,
554 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
555 Device* device = getDevice(deviceId);
556
557 RawAbsoluteAxisInfo info;
558 info.valid = true;
559 info.minValue = minValue;
560 info.maxValue = maxValue;
561 info.flat = flat;
562 info.fuzz = fuzz;
563 info.resolution = resolution;
564 device->absoluteAxes.add(axis, info);
565 }
566
567 void addRelativeAxis(int32_t deviceId, int32_t axis) {
568 Device* device = getDevice(deviceId);
569 device->relativeAxes.add(axis, true);
570 }
571
572 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
573 Device* device = getDevice(deviceId);
574 device->keyCodeStates.replaceValueFor(keyCode, state);
575 }
576
577 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
578 Device* device = getDevice(deviceId);
579 device->scanCodeStates.replaceValueFor(scanCode, state);
580 }
581
582 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
583 Device* device = getDevice(deviceId);
584 device->switchStates.replaceValueFor(switchCode, state);
585 }
586
587 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
588 Device* device = getDevice(deviceId);
589 device->absoluteAxisValue.replaceValueFor(axis, value);
590 }
591
592 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
593 int32_t keyCode, uint32_t flags) {
594 Device* device = getDevice(deviceId);
595 KeyInfo info;
596 info.keyCode = keyCode;
597 info.flags = flags;
598 if (scanCode) {
599 device->keysByScanCode.add(scanCode, info);
600 }
601 if (usageCode) {
602 device->keysByUsageCode.add(usageCode, info);
603 }
604 }
605
Philip Junker4af3b3d2021-12-14 10:36:55 +0100606 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
607 Device* device = getDevice(deviceId);
608 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
609 }
610
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 void addLed(int32_t deviceId, int32_t led, bool initialState) {
612 Device* device = getDevice(deviceId);
613 device->leds.add(led, initialState);
614 }
615
Chris Yef59a2f42020-10-16 12:55:26 -0700616 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
617 int32_t sensorDataIndex) {
618 Device* device = getDevice(deviceId);
619 SensorInfo info;
620 info.sensorType = sensorType;
621 info.sensorDataIndex = sensorDataIndex;
622 device->sensorsByAbsCode.emplace(absCode, info);
623 }
624
625 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
626 Device* device = getDevice(deviceId);
627 typename BitArray<MSC_MAX>::Buffer buffer;
628 buffer[mscEvent / 32] = 1 << mscEvent % 32;
629 device->mscBitmask.loadFromBuffer(buffer);
630 }
631
Chris Ye3fdbfef2021-01-06 18:45:18 -0800632 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
633 mRawLightInfos.emplace(rawId, std::move(info));
634 }
635
636 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
637 mLightBrightness.emplace(rawId, brightness);
638 }
639
640 void fakeLightIntensities(int32_t rawId,
641 const std::unordered_map<LightColor, int32_t> intensities) {
642 mLightIntensities.emplace(rawId, std::move(intensities));
643 }
644
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 bool getLedState(int32_t deviceId, int32_t led) {
646 Device* device = getDevice(deviceId);
647 return device->leds.valueFor(led);
648 }
649
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100650 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800651 return mExcludedDevices;
652 }
653
654 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
655 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800656 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 }
658
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000659 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
660 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700661 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800662 RawEvent event;
663 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000664 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665 event.deviceId = deviceId;
666 event.type = type;
667 event.code = code;
668 event.value = value;
669 mEvents.push_back(event);
670
671 if (type == EV_ABS) {
672 setAbsoluteAxisValue(deviceId, code, value);
673 }
674 }
675
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600676 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
677 std::vector<TouchVideoFrame>> videoFrames) {
678 mVideoFrames = std::move(videoFrames);
679 }
680
Michael Wrightd02c5b62014-02-10 15:10:22 -0800681 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700682 std::unique_lock<std::mutex> lock(mLock);
683 base::ScopedLockAssertion assumeLocked(mLock);
684 const bool queueIsEmpty =
685 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
686 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
687 if (!queueIsEmpty) {
688 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
689 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800690 }
691
692private:
693 Device* getDevice(int32_t deviceId) const {
694 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100695 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 }
697
Chris Yea52ade12020-08-27 16:49:20 -0700698 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700700 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701 }
702
Chris Yea52ade12020-08-27 16:49:20 -0700703 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800704 Device* device = getDevice(deviceId);
705 return device ? device->identifier : InputDeviceIdentifier();
706 }
707
Chris Yea52ade12020-08-27 16:49:20 -0700708 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709
Chris Yea52ade12020-08-27 16:49:20 -0700710 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 Device* device = getDevice(deviceId);
712 if (device) {
713 *outConfiguration = device->configuration;
714 }
715 }
716
Chris Yea52ade12020-08-27 16:49:20 -0700717 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
718 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800719 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800720 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800721 ssize_t index = device->absoluteAxes.indexOfKey(axis);
722 if (index >= 0) {
723 *outAxisInfo = device->absoluteAxes.valueAt(index);
724 return OK;
725 }
726 }
727 outAxisInfo->clear();
728 return -1;
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732 Device* device = getDevice(deviceId);
733 if (device) {
734 return device->relativeAxes.indexOfKey(axis) >= 0;
735 }
736 return false;
737 }
738
Chris Yea52ade12020-08-27 16:49:20 -0700739 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800740
Chris Yef59a2f42020-10-16 12:55:26 -0700741 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
742 Device* device = getDevice(deviceId);
743 if (device) {
744 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
745 }
746 return false;
747 }
748
Chris Yea52ade12020-08-27 16:49:20 -0700749 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
750 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
752 if (device) {
753 const KeyInfo* key = getKey(device, scanCode, usageCode);
754 if (key) {
755 if (outKeycode) {
756 *outKeycode = key->keyCode;
757 }
758 if (outFlags) {
759 *outFlags = key->flags;
760 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700761 if (outMetaState) {
762 *outMetaState = metaState;
763 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800764 return OK;
765 }
766 }
767 return NAME_NOT_FOUND;
768 }
769
770 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
771 if (usageCode) {
772 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
773 if (index >= 0) {
774 return &device->keysByUsageCode.valueAt(index);
775 }
776 }
777 if (scanCode) {
778 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
779 if (index >= 0) {
780 return &device->keysByScanCode.valueAt(index);
781 }
782 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700783 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 }
785
Chris Yea52ade12020-08-27 16:49:20 -0700786 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
Chris Yef59a2f42020-10-16 12:55:26 -0700788 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
789 int32_t absCode) {
790 Device* device = getDevice(deviceId);
791 if (!device) {
792 return Errorf("Sensor device not found.");
793 }
794 auto it = device->sensorsByAbsCode.find(absCode);
795 if (it == device->sensorsByAbsCode.end()) {
796 return Errorf("Sensor map not found.");
797 }
798 const SensorInfo& info = it->second;
799 return std::make_pair(info.sensorType, info.sensorDataIndex);
800 }
801
Chris Yea52ade12020-08-27 16:49:20 -0700802 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 mExcludedDevices = devices;
804 }
805
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000806 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
807 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800808
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000809 const size_t filledSize = std::min(mEvents.size(), bufferSize);
810 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
811
812 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700813 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000814 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800815 }
816
Chris Yea52ade12020-08-27 16:49:20 -0700817 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600818 auto it = mVideoFrames.find(deviceId);
819 if (it != mVideoFrames.end()) {
820 std::vector<TouchVideoFrame> frames = std::move(it->second);
821 mVideoFrames.erase(deviceId);
822 return frames;
823 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800824 return {};
825 }
826
Chris Yea52ade12020-08-27 16:49:20 -0700827 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800828 Device* device = getDevice(deviceId);
829 if (device) {
830 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
831 if (index >= 0) {
832 return device->scanCodeStates.valueAt(index);
833 }
834 }
835 return AKEY_STATE_UNKNOWN;
836 }
837
Chris Yea52ade12020-08-27 16:49:20 -0700838 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839 Device* device = getDevice(deviceId);
840 if (device) {
841 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
842 if (index >= 0) {
843 return device->keyCodeStates.valueAt(index);
844 }
845 }
846 return AKEY_STATE_UNKNOWN;
847 }
848
Chris Yea52ade12020-08-27 16:49:20 -0700849 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850 Device* device = getDevice(deviceId);
851 if (device) {
852 ssize_t index = device->switchStates.indexOfKey(sw);
853 if (index >= 0) {
854 return device->switchStates.valueAt(index);
855 }
856 }
857 return AKEY_STATE_UNKNOWN;
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
861 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862 Device* device = getDevice(deviceId);
863 if (device) {
864 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
865 if (index >= 0) {
866 *outValue = device->absoluteAxisValue.valueAt(index);
867 return OK;
868 }
869 }
870 *outValue = 0;
871 return -1;
872 }
873
Philip Junker4af3b3d2021-12-14 10:36:55 +0100874 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
875 Device* device = getDevice(deviceId);
876 if (!device) {
877 return AKEYCODE_UNKNOWN;
878 }
879 auto it = device->keyCodeMapping.find(locationKeyCode);
880 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
881 }
882
Chris Yea52ade12020-08-27 16:49:20 -0700883 // Return true if the device has non-empty key layout.
884 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
885 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886 bool result = false;
887 Device* device = getDevice(deviceId);
888 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700889 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 for (size_t i = 0; i < numCodes; i++) {
891 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
892 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
893 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894 }
895 }
896 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
897 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
898 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 }
900 }
901 }
902 }
903 return result;
904 }
905
Chris Yea52ade12020-08-27 16:49:20 -0700906 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907 Device* device = getDevice(deviceId);
908 if (device) {
909 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
910 return index >= 0;
911 }
912 return false;
913 }
914
Arthur Hungcb40a002021-08-03 14:31:01 +0000915 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
916 Device* device = getDevice(deviceId);
917 if (!device) {
918 return false;
919 }
920 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
921 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
922 return true;
923 }
924 }
925 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
926 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
927 return true;
928 }
929 }
930 return false;
931 }
932
Chris Yea52ade12020-08-27 16:49:20 -0700933 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 Device* device = getDevice(deviceId);
935 return device && device->leds.indexOfKey(led) >= 0;
936 }
937
Chris Yea52ade12020-08-27 16:49:20 -0700938 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 Device* device = getDevice(deviceId);
940 if (device) {
941 ssize_t index = device->leds.indexOfKey(led);
942 if (index >= 0) {
943 device->leds.replaceValueAt(led, on);
944 } else {
945 ADD_FAILURE()
946 << "Attempted to set the state of an LED that the EventHub declared "
947 "was not present. led=" << led;
948 }
949 }
950 }
951
Chris Yea52ade12020-08-27 16:49:20 -0700952 void getVirtualKeyDefinitions(
953 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 outVirtualKeys.clear();
955
956 Device* device = getDevice(deviceId);
957 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800958 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 }
960 }
961
Chris Yea52ade12020-08-27 16:49:20 -0700962 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700963 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 }
965
Chris Yea52ade12020-08-27 16:49:20 -0700966 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967 return false;
968 }
969
Chris Yea52ade12020-08-27 16:49:20 -0700970 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971
Chris Yea52ade12020-08-27 16:49:20 -0700972 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973
Chris Ye87143712020-11-10 05:05:58 +0000974 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
975
Chris Yee2b1e5c2021-03-10 22:45:12 -0800976 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
977 return BATTERY_CAPACITY;
978 }
Kim Low03ea0352020-11-06 12:45:07 -0800979
Chris Yee2b1e5c2021-03-10 22:45:12 -0800980 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
981 return BATTERY_STATUS;
982 }
983
984 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
985
986 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
987 return std::nullopt;
988 }
Kim Low03ea0352020-11-06 12:45:07 -0800989
Chris Ye3fdbfef2021-01-06 18:45:18 -0800990 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
991 std::vector<int32_t> ids;
992 for (const auto& [rawId, info] : mRawLightInfos) {
993 ids.push_back(rawId);
994 }
995 return ids;
996 }
997
998 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
999 auto it = mRawLightInfos.find(lightId);
1000 if (it == mRawLightInfos.end()) {
1001 return std::nullopt;
1002 }
1003 return it->second;
1004 }
1005
1006 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1007 mLightBrightness.emplace(lightId, brightness);
1008 }
1009
1010 void setLightIntensities(int32_t deviceId, int32_t lightId,
1011 std::unordered_map<LightColor, int32_t> intensities) override {
1012 mLightIntensities.emplace(lightId, intensities);
1013 };
1014
1015 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1016 auto lightIt = mLightBrightness.find(lightId);
1017 if (lightIt == mLightBrightness.end()) {
1018 return std::nullopt;
1019 }
1020 return lightIt->second;
1021 }
1022
1023 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1024 int32_t deviceId, int32_t lightId) override {
1025 auto lightIt = mLightIntensities.find(lightId);
1026 if (lightIt == mLightIntensities.end()) {
1027 return std::nullopt;
1028 }
1029 return lightIt->second;
1030 };
1031
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001032 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001033 return false;
1034 }
1035
Chris Yea52ade12020-08-27 16:49:20 -07001036 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001037
Chris Yea52ade12020-08-27 16:49:20 -07001038 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001039
Chris Yea52ade12020-08-27 16:49:20 -07001040 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001041
Chris Yea52ade12020-08-27 16:49:20 -07001042 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001043};
1044
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045// --- FakeInputMapper ---
1046
1047class FakeInputMapper : public InputMapper {
1048 uint32_t mSources;
1049 int32_t mKeyboardType;
1050 int32_t mMetaState;
1051 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1052 KeyedVector<int32_t, int32_t> mScanCodeStates;
1053 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001054 // fake mapping which would normally come from keyCharacterMap
1055 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001056 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001057
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001058 std::mutex mLock;
1059 std::condition_variable mStateChangedCondition;
1060 bool mConfigureWasCalled GUARDED_BY(mLock);
1061 bool mResetWasCalled GUARDED_BY(mLock);
1062 bool mProcessWasCalled GUARDED_BY(mLock);
1063 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001064
Arthur Hungc23540e2018-11-29 20:42:11 +08001065 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001066public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001067 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1068 : InputMapper(deviceContext),
1069 mSources(sources),
1070 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001071 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001072 mConfigureWasCalled(false),
1073 mResetWasCalled(false),
1074 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001075
Chris Yea52ade12020-08-27 16:49:20 -07001076 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077
1078 void setKeyboardType(int32_t keyboardType) {
1079 mKeyboardType = keyboardType;
1080 }
1081
1082 void setMetaState(int32_t metaState) {
1083 mMetaState = metaState;
1084 }
1085
1086 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001087 std::unique_lock<std::mutex> lock(mLock);
1088 base::ScopedLockAssertion assumeLocked(mLock);
1089 const bool configureCalled =
1090 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1091 return mConfigureWasCalled;
1092 });
1093 if (!configureCalled) {
1094 FAIL() << "Expected configure() to have been called.";
1095 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001096 mConfigureWasCalled = false;
1097 }
1098
1099 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001100 std::unique_lock<std::mutex> lock(mLock);
1101 base::ScopedLockAssertion assumeLocked(mLock);
1102 const bool resetCalled =
1103 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1104 return mResetWasCalled;
1105 });
1106 if (!resetCalled) {
1107 FAIL() << "Expected reset() to have been called.";
1108 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109 mResetWasCalled = false;
1110 }
1111
Yi Kong9b14ac62018-07-17 13:48:38 -07001112 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001113 std::unique_lock<std::mutex> lock(mLock);
1114 base::ScopedLockAssertion assumeLocked(mLock);
1115 const bool processCalled =
1116 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1117 return mProcessWasCalled;
1118 });
1119 if (!processCalled) {
1120 FAIL() << "Expected process() to have been called.";
1121 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122 if (outLastEvent) {
1123 *outLastEvent = mLastEvent;
1124 }
1125 mProcessWasCalled = false;
1126 }
1127
1128 void setKeyCodeState(int32_t keyCode, int32_t state) {
1129 mKeyCodeStates.replaceValueFor(keyCode, state);
1130 }
1131
1132 void setScanCodeState(int32_t scanCode, int32_t state) {
1133 mScanCodeStates.replaceValueFor(scanCode, state);
1134 }
1135
1136 void setSwitchState(int32_t switchCode, int32_t state) {
1137 mSwitchStates.replaceValueFor(switchCode, state);
1138 }
1139
1140 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001141 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001142 }
1143
Philip Junker4af3b3d2021-12-14 10:36:55 +01001144 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1145 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1146 }
1147
Michael Wrightd02c5b62014-02-10 15:10:22 -08001148private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001149 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001150
Chris Yea52ade12020-08-27 16:49:20 -07001151 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152 InputMapper::populateDeviceInfo(deviceInfo);
1153
1154 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1155 deviceInfo->setKeyboardType(mKeyboardType);
1156 }
1157 }
1158
Chris Yea52ade12020-08-27 16:49:20 -07001159 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001160 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001161 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001162
1163 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001164 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001165 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1166 mViewport = config->getDisplayViewportByPort(*displayPort);
1167 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001168
1169 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001170 }
1171
Chris Yea52ade12020-08-27 16:49:20 -07001172 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001173 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001174 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001175 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001176 }
1177
Chris Yea52ade12020-08-27 16:49:20 -07001178 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001179 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 mLastEvent = *rawEvent;
1181 mProcessWasCalled = 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 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001186 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1187 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1188 }
1189
Philip Junker4af3b3d2021-12-14 10:36:55 +01001190 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1191 auto it = mKeyCodeMapping.find(locationKeyCode);
1192 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1193 }
1194
Chris Yea52ade12020-08-27 16:49:20 -07001195 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001196 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1197 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1198 }
1199
Chris Yea52ade12020-08-27 16:49:20 -07001200 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001201 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1202 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1203 }
1204
Chris Yea52ade12020-08-27 16:49:20 -07001205 // Return true if the device has non-empty key layout.
1206 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1207 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001208 for (size_t i = 0; i < numCodes; i++) {
1209 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1210 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1211 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001212 }
1213 }
1214 }
Chris Yea52ade12020-08-27 16:49:20 -07001215 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216 return result;
1217 }
1218
1219 virtual int32_t getMetaState() {
1220 return mMetaState;
1221 }
1222
1223 virtual void fadePointer() {
1224 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001225
1226 virtual std::optional<int32_t> getAssociatedDisplay() {
1227 if (mViewport) {
1228 return std::make_optional(mViewport->displayId);
1229 }
1230 return std::nullopt;
1231 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001232};
1233
1234
1235// --- InstrumentedInputReader ---
1236
1237class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001238 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239
1240public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001241 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1242 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001243 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001244 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001246 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001247
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001248 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001249
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001250 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001251 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252 InputDeviceIdentifier identifier;
1253 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001254 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001255 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001256 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001257 }
1258
Prabir Pradhan28efc192019-11-05 01:10:04 +00001259 // Make the protected loopOnce method accessible to tests.
1260 using InputReader::loopOnce;
1261
Michael Wrightd02c5b62014-02-10 15:10:22 -08001262protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001263 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1264 const InputDeviceIdentifier& identifier)
1265 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001266 if (!mNextDevices.empty()) {
1267 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1268 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269 return device;
1270 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001271 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001272 }
1273
arthurhungdcef2dc2020-08-11 14:47:50 +08001274 // --- FakeInputReaderContext ---
1275 class FakeInputReaderContext : public ContextImpl {
1276 int32_t mGlobalMetaState;
1277 bool mUpdateGlobalMetaStateWasCalled;
1278 int32_t mGeneration;
1279
1280 public:
1281 FakeInputReaderContext(InputReader* reader)
1282 : ContextImpl(reader),
1283 mGlobalMetaState(0),
1284 mUpdateGlobalMetaStateWasCalled(false),
1285 mGeneration(1) {}
1286
1287 virtual ~FakeInputReaderContext() {}
1288
1289 void assertUpdateGlobalMetaStateWasCalled() {
1290 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1291 << "Expected updateGlobalMetaState() to have been called.";
1292 mUpdateGlobalMetaStateWasCalled = false;
1293 }
1294
1295 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1296
1297 uint32_t getGeneration() { return mGeneration; }
1298
1299 void updateGlobalMetaState() override {
1300 mUpdateGlobalMetaStateWasCalled = true;
1301 ContextImpl::updateGlobalMetaState();
1302 }
1303
1304 int32_t getGlobalMetaState() override {
1305 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1306 }
1307
1308 int32_t bumpGeneration() override {
1309 mGeneration = ContextImpl::bumpGeneration();
1310 return mGeneration;
1311 }
1312 } mFakeContext;
1313
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001315
1316public:
1317 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318};
1319
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320// --- InputReaderPolicyTest ---
1321class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001322protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323 sp<FakeInputReaderPolicy> mFakePolicy;
1324
Chris Yea52ade12020-08-27 16:49:20 -07001325 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1326 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001327};
1328
1329/**
1330 * Check that empty set of viewports is an acceptable configuration.
1331 * Also try to get internal viewport two different ways - by type and by uniqueId.
1332 *
1333 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1334 * Such configuration is not currently allowed.
1335 */
1336TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001337 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001338
1339 // We didn't add any viewports yet, so there shouldn't be any.
1340 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001341 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001342 ASSERT_FALSE(internalViewport);
1343
1344 // Add an internal viewport, then clear it
1345 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001346 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001347 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001348
1349 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001350 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001352 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001353
1354 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001355 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001356 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001357 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001358
1359 mFakePolicy->clearViewports();
1360 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001361 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001362 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001363 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001364 ASSERT_FALSE(internalViewport);
1365}
1366
1367TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1368 const std::string internalUniqueId = "local:0";
1369 const std::string externalUniqueId = "local:1";
1370 const std::string virtualUniqueId1 = "virtual:2";
1371 const std::string virtualUniqueId2 = "virtual:3";
1372 constexpr int32_t virtualDisplayId1 = 2;
1373 constexpr int32_t virtualDisplayId2 = 3;
1374
1375 // Add an internal viewport
1376 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001377 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1378 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 // Add an external viewport
1380 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001381 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1382 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001383 // Add an virtual viewport
1384 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001385 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1386 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 // Add another virtual viewport
1388 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001389 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1390 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391
1392 // Check matching by type for internal
1393 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001394 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001395 ASSERT_TRUE(internalViewport);
1396 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1397
1398 // Check matching by type for external
1399 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001400 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001401 ASSERT_TRUE(externalViewport);
1402 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1403
1404 // Check matching by uniqueId for virtual viewport #1
1405 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001406 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1410 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1411
1412 // Check matching by uniqueId for virtual viewport #2
1413 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001414 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001416 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1418 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1419}
1420
1421
1422/**
1423 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1424 * that lookup works by checking display id.
1425 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1426 */
1427TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1428 const std::string uniqueId1 = "uniqueId1";
1429 const std::string uniqueId2 = "uniqueId2";
1430 constexpr int32_t displayId1 = 2;
1431 constexpr int32_t displayId2 = 3;
1432
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1434 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 for (const ViewportType& type : types) {
1436 mFakePolicy->clearViewports();
1437 // Add a viewport
1438 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001439 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1440 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001441 // Add another viewport
1442 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001443 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1444 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445
1446 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001447 std::optional<DisplayViewport> viewport1 =
1448 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 ASSERT_TRUE(viewport1);
1450 ASSERT_EQ(displayId1, viewport1->displayId);
1451 ASSERT_EQ(type, viewport1->type);
1452
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 std::optional<DisplayViewport> viewport2 =
1454 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001455 ASSERT_TRUE(viewport2);
1456 ASSERT_EQ(displayId2, viewport2->displayId);
1457 ASSERT_EQ(type, viewport2->type);
1458
1459 // When there are multiple viewports of the same kind, and uniqueId is not specified
1460 // in the call to getDisplayViewport, then that situation is not supported.
1461 // The viewports can be stored in any order, so we cannot rely on the order, since that
1462 // is just implementation detail.
1463 // However, we can check that it still returns *a* viewport, we just cannot assert
1464 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001465 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001466 ASSERT_TRUE(someViewport);
1467 }
1468}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001469
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001470/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001471 * When we have multiple internal displays make sure we always return the default display when
1472 * querying by type.
1473 */
1474TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1475 const std::string uniqueId1 = "uniqueId1";
1476 const std::string uniqueId2 = "uniqueId2";
1477 constexpr int32_t nonDefaultDisplayId = 2;
1478 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1479 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1480
1481 // Add the default display first and ensure it gets returned.
1482 mFakePolicy->clearViewports();
1483 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001484 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001485 ViewportType::INTERNAL);
1486 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001487 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001488 ViewportType::INTERNAL);
1489
1490 std::optional<DisplayViewport> viewport =
1491 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1492 ASSERT_TRUE(viewport);
1493 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1494 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1495
1496 // Add the default display second to make sure order doesn't matter.
1497 mFakePolicy->clearViewports();
1498 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001499 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001500 ViewportType::INTERNAL);
1501 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001502 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001503 ViewportType::INTERNAL);
1504
1505 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1506 ASSERT_TRUE(viewport);
1507 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1508 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1509}
1510
1511/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001512 * Check getDisplayViewportByPort
1513 */
1514TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001515 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001516 const std::string uniqueId1 = "uniqueId1";
1517 const std::string uniqueId2 = "uniqueId2";
1518 constexpr int32_t displayId1 = 1;
1519 constexpr int32_t displayId2 = 2;
1520 const uint8_t hdmi1 = 0;
1521 const uint8_t hdmi2 = 1;
1522 const uint8_t hdmi3 = 2;
1523
1524 mFakePolicy->clearViewports();
1525 // Add a viewport that's associated with some display port that's not of interest.
1526 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001527 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1528 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001529 // Add another viewport, connected to HDMI1 port
1530 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1532 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001533
1534 // Check that correct display viewport was returned by comparing the display ports.
1535 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1536 ASSERT_TRUE(hdmi1Viewport);
1537 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1538 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1539
1540 // Check that we can still get the same viewport using the uniqueId
1541 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1542 ASSERT_TRUE(hdmi1Viewport);
1543 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1544 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1545 ASSERT_EQ(type, hdmi1Viewport->type);
1546
1547 // Check that we cannot find a port with "HDMI2", because we never added one
1548 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1549 ASSERT_FALSE(hdmi2Viewport);
1550}
1551
Michael Wrightd02c5b62014-02-10 15:10:22 -08001552// --- InputReaderTest ---
1553
1554class InputReaderTest : public testing::Test {
1555protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001556 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001557 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001558 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001559 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001560
Chris Yea52ade12020-08-27 16:49:20 -07001561 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001562 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001563 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001564 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001565
Prabir Pradhan28efc192019-11-05 01:10:04 +00001566 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001567 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001568 }
1569
Chris Yea52ade12020-08-27 16:49:20 -07001570 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001571 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001573 }
1574
Chris Ye1b0c7342020-07-28 21:57:03 -07001575 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001576 const PropertyMap* configuration) {
1577 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001578
1579 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001580 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001581 }
1582 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001583 mReader->loopOnce();
1584 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001585 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1586 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587 }
1588
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001589 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001590 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001591 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001592 }
1593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001594 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001595 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001596 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001597 }
1598
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001599 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001600 const std::string& name,
1601 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001602 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001603 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1604 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001605 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001606 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001607 return mapper;
1608 }
1609};
1610
Chris Ye98d3f532020-10-01 21:48:59 -07001611TEST_F(InputReaderTest, PolicyGetInputDevices) {
1612 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1613 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1614 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001615
1616 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001617 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001618 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001619 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001620 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1622 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001623 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624}
1625
Chris Yee7310032020-09-22 15:36:28 -07001626TEST_F(InputReaderTest, GetMergedInputDevices) {
1627 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1628 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1629 // Add two subdevices to device
1630 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1631 // Must add at least one mapper or the device will be ignored!
1632 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1633 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1634
1635 // Push same device instance for next device to be added, so they'll have same identifier.
1636 mReader->pushNextDevice(device);
1637 mReader->pushNextDevice(device);
1638 ASSERT_NO_FATAL_FAILURE(
1639 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1640 ASSERT_NO_FATAL_FAILURE(
1641 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1642
1643 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001644 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001645}
1646
Chris Yee14523a2020-12-19 13:46:00 -08001647TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1648 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1649 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1650 // Add two subdevices to device
1651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1652 // Must add at least one mapper or the device will be ignored!
1653 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1654 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1655
1656 // Push same device instance for next device to be added, so they'll have same identifier.
1657 mReader->pushNextDevice(device);
1658 mReader->pushNextDevice(device);
1659 // Sensor device is initially disabled
1660 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1661 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1662 nullptr));
1663 // Device is disabled because the only sub device is a sensor device and disabled initially.
1664 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1665 ASSERT_FALSE(device->isEnabled());
1666 ASSERT_NO_FATAL_FAILURE(
1667 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1668 // The merged device is enabled if any sub device is enabled
1669 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1670 ASSERT_TRUE(device->isEnabled());
1671}
1672
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001673TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001675 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001676 constexpr int32_t eventHubId = 1;
1677 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001678 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001679 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001680 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001681 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682
Yi Kong9b14ac62018-07-17 13:48:38 -07001683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684
1685 NotifyDeviceResetArgs resetArgs;
1686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 ASSERT_EQ(deviceId, resetArgs.deviceId);
1688
1689 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001690 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001691 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001692
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001694 ASSERT_EQ(deviceId, resetArgs.deviceId);
1695 ASSERT_EQ(device->isEnabled(), false);
1696
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001697 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001698 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001701 ASSERT_EQ(device->isEnabled(), false);
1702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001703 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001704 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001706 ASSERT_EQ(deviceId, resetArgs.deviceId);
1707 ASSERT_EQ(device->isEnabled(), true);
1708}
1709
Michael Wrightd02c5b62014-02-10 15:10:22 -08001710TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001711 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001712 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001713 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001714 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001715 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001716 AINPUT_SOURCE_KEYBOARD, nullptr);
1717 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001718
1719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1720 AINPUT_SOURCE_ANY, AKEYCODE_A))
1721 << "Should return unknown when the device id is >= 0 but unknown.";
1722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001723 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1724 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1725 << "Should return unknown when the device id is valid but the sources are not "
1726 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 ASSERT_EQ(AKEY_STATE_DOWN,
1729 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1730 AKEYCODE_A))
1731 << "Should return value provided by mapper when device id is valid and the device "
1732 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001733
1734 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1735 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1736 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1737
1738 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1739 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1740 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1741}
1742
Philip Junker4af3b3d2021-12-14 10:36:55 +01001743TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1744 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1745 constexpr int32_t eventHubId = 1;
1746 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1747 InputDeviceClass::KEYBOARD,
1748 AINPUT_SOURCE_KEYBOARD, nullptr);
1749 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1750
1751 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1752 << "Should return unknown when the device with the specified id is not found.";
1753
1754 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1755 << "Should return correct mapping when device id is valid and mapping exists.";
1756
1757 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1758 << "Should return the location key code when device id is valid and there's no "
1759 "mapping.";
1760}
1761
1762TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1763 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1764 constexpr int32_t eventHubId = 1;
1765 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1766 InputDeviceClass::JOYSTICK,
1767 AINPUT_SOURCE_GAMEPAD, nullptr);
1768 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1769
1770 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1771 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1772}
1773
Michael Wrightd02c5b62014-02-10 15:10:22 -08001774TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001776 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001777 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001778 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001780 AINPUT_SOURCE_KEYBOARD, nullptr);
1781 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001782
1783 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1784 AINPUT_SOURCE_ANY, KEY_A))
1785 << "Should return unknown when the device id is >= 0 but unknown.";
1786
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1788 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1789 << "Should return unknown when the device id is valid but the sources are not "
1790 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001792 ASSERT_EQ(AKEY_STATE_DOWN,
1793 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1794 KEY_A))
1795 << "Should return value provided by mapper when device id is valid and the device "
1796 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001797
1798 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1799 AINPUT_SOURCE_TRACKBALL, KEY_A))
1800 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1801
1802 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1803 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1804 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1805}
1806
1807TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001809 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001811 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 AINPUT_SOURCE_KEYBOARD, nullptr);
1814 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001815
1816 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1817 AINPUT_SOURCE_ANY, SW_LID))
1818 << "Should return unknown when the device id is >= 0 but unknown.";
1819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001820 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1821 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1822 << "Should return unknown when the device id is valid but the sources are not "
1823 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 ASSERT_EQ(AKEY_STATE_DOWN,
1826 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1827 SW_LID))
1828 << "Should return value provided by mapper when device id is valid and the device "
1829 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001830
1831 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1832 AINPUT_SOURCE_TRACKBALL, SW_LID))
1833 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1834
1835 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1836 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1837 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1838}
1839
1840TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001841 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001842 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001844 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 mapper.addSupportedKeyCode(AKEYCODE_A);
1849 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
1851 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1852 uint8_t flags[4] = { 0, 0, 0, 1 };
1853
1854 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1855 << "Should return false when device id is >= 0 but unknown.";
1856 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1857
1858 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001859 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1860 << "Should return false when device id is valid but the sources are not supported by "
1861 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001862 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1863
1864 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1866 keyCodes, flags))
1867 << "Should return value provided by mapper when device id is valid and the device "
1868 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001869 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1870
1871 flags[3] = 1;
1872 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1873 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1874 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1875
1876 flags[3] = 1;
1877 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1878 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1879 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1880}
1881
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001882TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001883 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001884 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001885
1886 NotifyConfigurationChangedArgs args;
1887
1888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1889 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1890}
1891
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001892TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001894 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001895 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001896 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001897 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001898 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001900 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001901
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001902 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001903 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1905
1906 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001907 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001908 ASSERT_EQ(when, event.when);
1909 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001911 ASSERT_EQ(EV_KEY, event.type);
1912 ASSERT_EQ(KEY_A, event.code);
1913 ASSERT_EQ(1, event.value);
1914}
1915
Garfield Tan1c7bc862020-01-28 13:24:04 -08001916TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001918 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 constexpr int32_t eventHubId = 1;
1920 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001921 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001922 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001923 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001925
1926 NotifyDeviceResetArgs resetArgs;
1927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001928 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001929
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001930 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001931 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001933 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001934 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001936 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001937 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001939 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001940 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001942 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001943 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001945 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001946 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001947}
1948
Garfield Tan1c7bc862020-01-28 13:24:04 -08001949TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1950 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001951 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001952 constexpr int32_t eventHubId = 1;
1953 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1954 // Must add at least one mapper or the device will be ignored!
1955 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001956 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001957 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1958
1959 NotifyDeviceResetArgs resetArgs;
1960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1961 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1962}
1963
Arthur Hungc23540e2018-11-29 20:42:11 +08001964TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001966 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001967 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001968 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001969 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1970 FakeInputMapper& mapper =
1971 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001972 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001973
1974 const uint8_t hdmi1 = 1;
1975
1976 // Associated touch screen with second display.
1977 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1978
1979 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001980 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001981 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001982 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001983 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001984 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001985 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001986 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001987 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001988 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001989
1990 // Add the device, and make sure all of the callbacks are triggered.
1991 // The device is added after the input port associations are processed since
1992 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001993 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001997
Arthur Hung2c9a3342019-07-23 14:18:59 +08001998 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001999 ASSERT_EQ(deviceId, device->getId());
2000 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2001 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002002
2003 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002004 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002005 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002006 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002007}
2008
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002009TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2010 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2011 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
2012 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2013 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2014 // Must add at least one mapper or the device will be ignored!
2015 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2016 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2017 mReader->pushNextDevice(device);
2018 mReader->pushNextDevice(device);
2019 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2021
2022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2023
2024 NotifyDeviceResetArgs resetArgs;
2025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2026 ASSERT_EQ(deviceId, resetArgs.deviceId);
2027 ASSERT_TRUE(device->isEnabled());
2028 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2029 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2030
2031 disableDevice(deviceId);
2032 mReader->loopOnce();
2033
2034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2035 ASSERT_EQ(deviceId, resetArgs.deviceId);
2036 ASSERT_FALSE(device->isEnabled());
2037 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2038 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2039
2040 enableDevice(deviceId);
2041 mReader->loopOnce();
2042
2043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2044 ASSERT_EQ(deviceId, resetArgs.deviceId);
2045 ASSERT_TRUE(device->isEnabled());
2046 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2047 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2048}
2049
2050TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2051 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2052 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
2053 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2054 // Add two subdevices to device
2055 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2056 FakeInputMapper& mapperDevice1 =
2057 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2058 FakeInputMapper& mapperDevice2 =
2059 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2060 mReader->pushNextDevice(device);
2061 mReader->pushNextDevice(device);
2062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2063 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2064
2065 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2066 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2067
2068 ASSERT_EQ(AKEY_STATE_DOWN,
2069 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2070 ASSERT_EQ(AKEY_STATE_DOWN,
2071 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2072 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2073 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2074}
2075
Prabir Pradhan7e186182020-11-10 13:56:45 -08002076TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2077 NotifyPointerCaptureChangedArgs args;
2078
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002079 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002080 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2081 mReader->loopOnce();
2082 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002083 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2084 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002085
2086 mFakePolicy->setPointerCapture(false);
2087 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2088 mReader->loopOnce();
2089 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002090 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002091
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002092 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002093 // does not change.
2094 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2095 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002096 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002097}
2098
Chris Ye87143712020-11-10 05:05:58 +00002099class FakeVibratorInputMapper : public FakeInputMapper {
2100public:
2101 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2102 : FakeInputMapper(deviceContext, sources) {}
2103
2104 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2105};
2106
2107TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2108 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2109 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2110 constexpr int32_t eventHubId = 1;
2111 const char* DEVICE_LOCATION = "BLUETOOTH";
2112 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2113 FakeVibratorInputMapper& mapper =
2114 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2115 mReader->pushNextDevice(device);
2116
2117 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2118 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2119
2120 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2121 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2122}
2123
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002124// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002125
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002126class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002127public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002128 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002129
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002130 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002131
2132 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2133
2134 void dump(std::string& dump) override {}
2135
2136 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2137 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002138 }
2139
Chris Yee2b1e5c2021-03-10 22:45:12 -08002140 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2141 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002142 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002143
2144 bool setLightColor(int32_t lightId, int32_t color) override {
2145 getDeviceContext().setLightBrightness(lightId, color >> 24);
2146 return true;
2147 }
2148
2149 std::optional<int32_t> getLightColor(int32_t lightId) override {
2150 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2151 if (!result.has_value()) {
2152 return std::nullopt;
2153 }
2154 return result.value() << 24;
2155 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002156
2157 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2158
2159 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2160
2161private:
2162 InputDeviceContext& mDeviceContext;
2163 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2164 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002165};
2166
Chris Yee2b1e5c2021-03-10 22:45:12 -08002167TEST_F(InputReaderTest, BatteryGetCapacity) {
2168 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2169 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2170 constexpr int32_t eventHubId = 1;
2171 const char* DEVICE_LOCATION = "BLUETOOTH";
2172 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002173 FakePeripheralController& controller =
2174 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002175 mReader->pushNextDevice(device);
2176
2177 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2178
2179 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2180 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2181}
2182
2183TEST_F(InputReaderTest, BatteryGetStatus) {
2184 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2185 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2186 constexpr int32_t eventHubId = 1;
2187 const char* DEVICE_LOCATION = "BLUETOOTH";
2188 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002189 FakePeripheralController& controller =
2190 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002191 mReader->pushNextDevice(device);
2192
2193 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2194
2195 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2196 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2197}
2198
Chris Ye3fdbfef2021-01-06 18:45:18 -08002199TEST_F(InputReaderTest, LightGetColor) {
2200 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2201 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2202 constexpr int32_t eventHubId = 1;
2203 const char* DEVICE_LOCATION = "BLUETOOTH";
2204 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002205 FakePeripheralController& controller =
2206 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002207 mReader->pushNextDevice(device);
2208 RawLightInfo info = {.id = 1,
2209 .name = "Mono",
2210 .maxBrightness = 255,
2211 .flags = InputLightClass::BRIGHTNESS,
2212 .path = ""};
2213 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2214 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2215
2216 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217
Chris Yee2b1e5c2021-03-10 22:45:12 -08002218 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2219 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002220 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2221 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2222}
2223
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002224// --- InputReaderIntegrationTest ---
2225
2226// These tests create and interact with the InputReader only through its interface.
2227// The InputReader is started during SetUp(), which starts its processing in its own
2228// thread. The tests use linux uinput to emulate input devices.
2229// NOTE: Interacting with the physical device while these tests are running may cause
2230// the tests to fail.
2231class InputReaderIntegrationTest : public testing::Test {
2232protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002233 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002234 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002235 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002236
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002237 std::shared_ptr<FakePointerController> mFakePointerController;
2238
Chris Yea52ade12020-08-27 16:49:20 -07002239 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002240 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002241 mFakePointerController = std::make_shared<FakePointerController>();
2242 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002243 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2244 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002245
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002246 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2247 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002248 ASSERT_EQ(mReader->start(), OK);
2249
2250 // Since this test is run on a real device, all the input devices connected
2251 // to the test device will show up in mReader. We wait for those input devices to
2252 // show up before beginning the tests.
2253 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2255 }
2256
Chris Yea52ade12020-08-27 16:49:20 -07002257 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002258 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002259 mReader.reset();
2260 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002261 mFakePolicy.clear();
2262 }
2263};
2264
2265TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2266 // An invalid input device that is only used for this test.
2267 class InvalidUinputDevice : public UinputDevice {
2268 public:
2269 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2270
2271 private:
2272 void configureDevice(int fd, uinput_user_dev* device) override {}
2273 };
2274
2275 const size_t numDevices = mFakePolicy->getInputDevices().size();
2276
2277 // UinputDevice does not set any event or key bits, so InputReader should not
2278 // consider it as a valid device.
2279 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2280 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2281 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2282 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2283
2284 invalidDevice.reset();
2285 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2286 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2287 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2288}
2289
2290TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2291 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2292
2293 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2294 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2295 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2296 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2297
2298 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002299 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002300 const auto& it =
2301 std::find_if(inputDevices.begin(), inputDevices.end(),
2302 [&keyboard](const InputDeviceInfo& info) {
2303 return info.getIdentifier().name == keyboard->getName();
2304 });
2305
2306 ASSERT_NE(it, inputDevices.end());
2307 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2308 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2309 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002310
2311 keyboard.reset();
2312 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2313 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2314 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2315}
2316
2317TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2318 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2319 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2320
2321 NotifyConfigurationChangedArgs configChangedArgs;
2322 ASSERT_NO_FATAL_FAILURE(
2323 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002324 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002325 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2326
2327 NotifyKeyArgs keyArgs;
2328 keyboard->pressAndReleaseHomeKey();
2329 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002331 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002332 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002333 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002334 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002335 prevTimestamp = keyArgs.eventTime;
2336
2337 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2338 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002339 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002341 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002342}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002343
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002344/**
2345 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2346 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2347 * are passed to the listener.
2348 */
2349static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2350TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2351 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2352 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2353 NotifyKeyArgs keyArgs;
2354
2355 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2357 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2358 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2359
2360 controller->pressAndReleaseKey(BTN_GEAR_UP);
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2362 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2363 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2364}
2365
Arthur Hungaab25622020-01-16 11:22:11 +08002366// --- TouchProcessTest ---
2367class TouchIntegrationTest : public InputReaderIntegrationTest {
2368protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002369 const std::string UNIQUE_ID = "local:0";
2370
Chris Yea52ade12020-08-27 16:49:20 -07002371 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002372 InputReaderIntegrationTest::SetUp();
2373 // At least add an internal display.
2374 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2375 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002376 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002377
2378 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2381 }
2382
2383 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2384 int32_t orientation, const std::string& uniqueId,
2385 std::optional<uint8_t> physicalPort,
2386 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002387 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2388 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002389 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2390 }
2391
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002392 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2393 NotifyMotionArgs args;
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2395 EXPECT_EQ(action, args.action);
2396 ASSERT_EQ(points.size(), args.pointerCount);
2397 for (size_t i = 0; i < args.pointerCount; i++) {
2398 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2399 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2400 }
2401 }
2402
Arthur Hungaab25622020-01-16 11:22:11 +08002403 std::unique_ptr<UinputTouchScreen> mDevice;
2404};
2405
2406TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2407 NotifyMotionArgs args;
2408 const Point centerPoint = mDevice->getCenterPoint();
2409
2410 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002411 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002412 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002413 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2415 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2416
2417 // ACTION_MOVE
2418 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002419 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2422
2423 // ACTION_UP
2424 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002425 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2428}
2429
2430TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2431 NotifyMotionArgs args;
2432 const Point centerPoint = mDevice->getCenterPoint();
2433
2434 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002435 mDevice->sendSlot(FIRST_SLOT);
2436 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002437 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002438 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2440 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2441
2442 // ACTION_POINTER_DOWN (Second slot)
2443 const Point secondPoint = centerPoint + Point(100, 100);
2444 mDevice->sendSlot(SECOND_SLOT);
2445 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002446 mDevice->sendDown(secondPoint);
2447 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002448 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002449 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002450
2451 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002452 mDevice->sendMove(secondPoint + Point(1, 1));
2453 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2456
2457 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002458 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002459 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002461 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002462
2463 // ACTION_UP
2464 mDevice->sendSlot(FIRST_SLOT);
2465 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002466 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2469}
2470
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002471/**
2472 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2473 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2474 * data?
2475 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2476 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2477 * for Pointer 0 only is generated after.
2478 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2479 * events, we will not miss any information.
2480 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2481 * event generated afterwards that contains the newest movement of pointer 0.
2482 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2483 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2484 * losing information about non-palm pointers.
2485 */
2486TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2487 NotifyMotionArgs args;
2488 const Point centerPoint = mDevice->getCenterPoint();
2489
2490 // ACTION_DOWN
2491 mDevice->sendSlot(FIRST_SLOT);
2492 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2493 mDevice->sendDown(centerPoint);
2494 mDevice->sendSync();
2495 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2496
2497 // ACTION_POINTER_DOWN (Second slot)
2498 const Point secondPoint = centerPoint + Point(100, 100);
2499 mDevice->sendSlot(SECOND_SLOT);
2500 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2501 mDevice->sendDown(secondPoint);
2502 mDevice->sendSync();
2503 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2504
2505 // ACTION_MOVE (First slot)
2506 mDevice->sendSlot(FIRST_SLOT);
2507 mDevice->sendMove(centerPoint + Point(5, 5));
2508 // ACTION_POINTER_UP (Second slot)
2509 mDevice->sendSlot(SECOND_SLOT);
2510 mDevice->sendPointerUp();
2511 // Send a single sync for the above 2 pointer updates
2512 mDevice->sendSync();
2513
2514 // First, we should get POINTER_UP for the second pointer
2515 assertReceivedMotion(ACTION_POINTER_1_UP,
2516 {/*first pointer */ centerPoint + Point(5, 5),
2517 /*second pointer*/ secondPoint});
2518
2519 // Next, the MOVE event for the first pointer
2520 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2521}
2522
2523/**
2524 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2525 * move, and then it will go up, all in the same frame.
2526 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2527 * gets sent to the listener.
2528 */
2529TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2530 NotifyMotionArgs args;
2531 const Point centerPoint = mDevice->getCenterPoint();
2532
2533 // ACTION_DOWN
2534 mDevice->sendSlot(FIRST_SLOT);
2535 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2536 mDevice->sendDown(centerPoint);
2537 mDevice->sendSync();
2538 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2539
2540 // ACTION_POINTER_DOWN (Second slot)
2541 const Point secondPoint = centerPoint + Point(100, 100);
2542 mDevice->sendSlot(SECOND_SLOT);
2543 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2544 mDevice->sendDown(secondPoint);
2545 mDevice->sendSync();
2546 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2547
2548 // ACTION_MOVE (First slot)
2549 mDevice->sendSlot(FIRST_SLOT);
2550 mDevice->sendMove(centerPoint + Point(5, 5));
2551 // ACTION_POINTER_UP (Second slot)
2552 mDevice->sendSlot(SECOND_SLOT);
2553 mDevice->sendMove(secondPoint + Point(6, 6));
2554 mDevice->sendPointerUp();
2555 // Send a single sync for the above 2 pointer updates
2556 mDevice->sendSync();
2557
2558 // First, we should get POINTER_UP for the second pointer
2559 // The movement of the second pointer during the liftoff frame is ignored.
2560 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2561 assertReceivedMotion(ACTION_POINTER_1_UP,
2562 {/*first pointer */ centerPoint + Point(5, 5),
2563 /*second pointer*/ secondPoint});
2564
2565 // Next, the MOVE event for the first pointer
2566 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2567}
2568
Arthur Hungaab25622020-01-16 11:22:11 +08002569TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2570 NotifyMotionArgs args;
2571 const Point centerPoint = mDevice->getCenterPoint();
2572
2573 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002574 mDevice->sendSlot(FIRST_SLOT);
2575 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002576 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002577 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002578 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2579 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2580
arthurhungcc7f9802020-04-30 17:55:40 +08002581 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002582 const Point secondPoint = centerPoint + Point(100, 100);
2583 mDevice->sendSlot(SECOND_SLOT);
2584 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2585 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002586 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002588 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002589
arthurhungcc7f9802020-04-30 17:55:40 +08002590 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002591 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002592 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002593 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2595
arthurhungcc7f9802020-04-30 17:55:40 +08002596 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2597 // a palm event.
2598 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002599 mDevice->sendToolType(MT_TOOL_PALM);
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_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002603 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002604
arthurhungcc7f9802020-04-30 17:55:40 +08002605 // Send up to second slot, expect first slot send moving.
2606 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002607 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002610
arthurhungcc7f9802020-04-30 17:55:40 +08002611 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002612 mDevice->sendSlot(FIRST_SLOT);
2613 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615
arthurhungcc7f9802020-04-30 17:55:40 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2617 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002618}
2619
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002621class InputDeviceTest : public testing::Test {
2622protected:
2623 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002624 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 static const int32_t DEVICE_ID;
2626 static const int32_t DEVICE_GENERATION;
2627 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002628 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002629 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002631 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002632 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002633 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002634 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002635 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636
Chris Yea52ade12020-08-27 16:49:20 -07002637 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002638 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002640 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002641 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002642 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643 InputDeviceIdentifier identifier;
2644 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002645 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002646 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002647 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002648 mReader->pushNextDevice(mDevice);
2649 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2650 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 }
2652
Chris Yea52ade12020-08-27 16:49:20 -07002653 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002654 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656 }
2657};
2658
2659const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002660const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002661const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2663const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002664const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2665 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002666const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667
2668TEST_F(InputDeviceTest, ImmutableProperties) {
2669 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002670 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002671 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672}
2673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002674TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2675 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002676}
2677
Michael Wrightd02c5b62014-02-10 15:10:22 -08002678TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2679 // Configuration.
2680 InputReaderConfiguration config;
2681 mDevice->configure(ARBITRARY_TIME, &config, 0);
2682
2683 // Reset.
2684 mDevice->reset(ARBITRARY_TIME);
2685
2686 NotifyDeviceResetArgs resetArgs;
2687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2688 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2689 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2690
2691 // Metadata.
2692 ASSERT_TRUE(mDevice->isIgnored());
2693 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2694
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002695 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002697 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2699 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2700
2701 // State queries.
2702 ASSERT_EQ(0, mDevice->getMetaState());
2703
2704 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2705 << "Ignored device should return unknown key code state.";
2706 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2707 << "Ignored device should return unknown scan code state.";
2708 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2709 << "Ignored device should return unknown switch state.";
2710
2711 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2712 uint8_t flags[2] = { 0, 1 };
2713 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2714 << "Ignored device should never mark any key codes.";
2715 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2716 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2717}
2718
2719TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2720 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002721 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002723 FakeInputMapper& mapper1 =
2724 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002725 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2726 mapper1.setMetaState(AMETA_ALT_ON);
2727 mapper1.addSupportedKeyCode(AKEYCODE_A);
2728 mapper1.addSupportedKeyCode(AKEYCODE_B);
2729 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2730 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2731 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2732 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2733 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002735 FakeInputMapper& mapper2 =
2736 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002737 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738
2739 InputReaderConfiguration config;
2740 mDevice->configure(ARBITRARY_TIME, &config, 0);
2741
2742 String8 propertyValue;
2743 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2744 << "Device should have read configuration during configuration phase.";
2745 ASSERT_STREQ("value", propertyValue.string());
2746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002747 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2748 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002749
2750 // Reset
2751 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002752 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2753 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002754
2755 NotifyDeviceResetArgs resetArgs;
2756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2757 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2758 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2759
2760 // Metadata.
2761 ASSERT_FALSE(mDevice->isIgnored());
2762 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2763
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002764 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002766 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2768 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2769
2770 // State queries.
2771 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2772 << "Should query mappers and combine meta states.";
2773
2774 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2775 << "Should return unknown key code state when source not supported.";
2776 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2777 << "Should return unknown scan code state when source not supported.";
2778 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2779 << "Should return unknown switch state when source not supported.";
2780
2781 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2782 << "Should query mapper when source is supported.";
2783 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2784 << "Should query mapper when source is supported.";
2785 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2786 << "Should query mapper when source is supported.";
2787
2788 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2789 uint8_t flags[4] = { 0, 0, 0, 1 };
2790 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2791 << "Should do nothing when source is unsupported.";
2792 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2793 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2794 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2795 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2796
2797 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2798 << "Should query mapper when source is supported.";
2799 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2800 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2801 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2802 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2803
2804 // Event handling.
2805 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807 mDevice->process(&event, 1);
2808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002809 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2810 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002811}
2812
Arthur Hung2c9a3342019-07-23 14:18:59 +08002813// A single input device is associated with a specific display. Check that:
2814// 1. Device is disabled if the viewport corresponding to the associated display is not found
2815// 2. Device is disabled when setEnabled API is called
2816TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002817 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002818
2819 // First Configuration.
2820 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2821
2822 // Device should be enabled by default.
2823 ASSERT_TRUE(mDevice->isEnabled());
2824
2825 // Prepare associated info.
2826 constexpr uint8_t hdmi = 1;
2827 const std::string UNIQUE_ID = "local:1";
2828
2829 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2830 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2831 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2832 // Device should be disabled because it is associated with a specific display via
2833 // input port <-> display port association, but the corresponding display is not found
2834 ASSERT_FALSE(mDevice->isEnabled());
2835
2836 // Prepare displays.
2837 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002838 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2839 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002840 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2841 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2842 ASSERT_TRUE(mDevice->isEnabled());
2843
2844 // Device should be disabled after set disable.
2845 mFakePolicy->addDisabledDevice(mDevice->getId());
2846 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2847 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2848 ASSERT_FALSE(mDevice->isEnabled());
2849
2850 // Device should still be disabled even found the associated display.
2851 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2852 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2853 ASSERT_FALSE(mDevice->isEnabled());
2854}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002855
Christine Franks1ba71cc2021-04-07 14:37:42 -07002856TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2857 // Device should be enabled by default.
2858 mFakePolicy->clearViewports();
2859 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2860 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2861 ASSERT_TRUE(mDevice->isEnabled());
2862
2863 // Device should be disabled because it is associated with a specific display, but the
2864 // corresponding display is not found.
2865 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002866 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002867 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2868 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2869 ASSERT_FALSE(mDevice->isEnabled());
2870
2871 // Device should be enabled when a display is found.
2872 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2873 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2874 NO_PORT, ViewportType::INTERNAL);
2875 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2876 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2877 ASSERT_TRUE(mDevice->isEnabled());
2878
2879 // Device should be disabled after set disable.
2880 mFakePolicy->addDisabledDevice(mDevice->getId());
2881 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2882 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2883 ASSERT_FALSE(mDevice->isEnabled());
2884
2885 // Device should still be disabled even found the associated display.
2886 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2887 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2888 ASSERT_FALSE(mDevice->isEnabled());
2889}
2890
Christine Franks2a2293c2022-01-18 11:51:16 -08002891TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2892 mFakePolicy->clearViewports();
2893 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2894 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2895
2896 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2897 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2898 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2899 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2900 NO_PORT, ViewportType::INTERNAL);
2901 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2902 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2903 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2904}
2905
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906// --- InputMapperTest ---
2907
2908class InputMapperTest : public testing::Test {
2909protected:
2910 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002911 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912 static const int32_t DEVICE_ID;
2913 static const int32_t DEVICE_GENERATION;
2914 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002915 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002916 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002917
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002918 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002920 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002921 std::unique_ptr<InstrumentedInputReader> mReader;
2922 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923
Chris Ye1b0c7342020-07-28 21:57:03 -07002924 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002925 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002927 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002928 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002929 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002930 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 }
2932
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002933 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002934 SetUp(DEVICE_CLASSES);
2935 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002936
Chris Yea52ade12020-08-27 16:49:20 -07002937 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002938 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 }
2941
2942 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002943 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944 }
2945
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002946 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002947 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002948 mReader->requestRefreshConfiguration(changes);
2949 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002950 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002951 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2952 }
2953
arthurhungdcef2dc2020-08-11 14:47:50 +08002954 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2955 const std::string& location, int32_t eventHubId,
2956 Flags<InputDeviceClass> classes) {
2957 InputDeviceIdentifier identifier;
2958 identifier.name = name;
2959 identifier.location = location;
2960 std::shared_ptr<InputDevice> device =
2961 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2962 identifier);
2963 mReader->pushNextDevice(device);
2964 mFakeEventHub->addDevice(eventHubId, name, classes);
2965 mReader->loopOnce();
2966 return device;
2967 }
2968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002969 template <class T, typename... Args>
2970 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002971 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002972 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002974 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002975 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 }
2977
2978 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002979 int32_t orientation, const std::string& uniqueId,
2980 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002981 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2982 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002983 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2984 }
2985
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002986 void clearViewports() {
2987 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 }
2989
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002990 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2991 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 RawEvent event;
2993 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002994 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996 event.type = type;
2997 event.code = code;
2998 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002999 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08003000 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001 }
3002
3003 static void assertMotionRange(const InputDeviceInfo& info,
3004 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3005 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003006 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3008 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3009 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3010 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3011 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3012 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3013 }
3014
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003015 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3016 float size, float touchMajor, float touchMinor, float toolMajor,
3017 float toolMinor, float orientation, float distance,
3018 float scaledAxisEpsilon = 1.f) {
3019 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3020 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3022 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003023 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3024 scaledAxisEpsilon);
3025 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3026 scaledAxisEpsilon);
3027 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3028 scaledAxisEpsilon);
3029 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3030 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3032 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3033 }
3034
Michael Wright17db18e2020-06-26 20:51:44 +01003035 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003037 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003038 ASSERT_NEAR(x, actualX, 1);
3039 ASSERT_NEAR(y, actualY, 1);
3040 }
3041};
3042
3043const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003044const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003045const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3047const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07003048const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3049 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003050const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051
3052// --- SwitchInputMapperTest ---
3053
3054class SwitchInputMapperTest : public InputMapperTest {
3055protected:
3056};
3057
3058TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003061 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062}
3063
3064TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003067 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003068 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003069
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003070 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003071 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072}
3073
3074TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003075 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081
3082 NotifySwitchArgs args;
3083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3084 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003085 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3086 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003087 args.switchMask);
3088 ASSERT_EQ(uint32_t(0), args.policyFlags);
3089}
3090
Chris Ye87143712020-11-10 05:05:58 +00003091// --- VibratorInputMapperTest ---
3092class VibratorInputMapperTest : public InputMapperTest {
3093protected:
3094 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3095};
3096
3097TEST_F(VibratorInputMapperTest, GetSources) {
3098 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3099
3100 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3101}
3102
3103TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3104 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3105
3106 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3107}
3108
3109TEST_F(VibratorInputMapperTest, Vibrate) {
3110 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003111 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003112 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3113
3114 VibrationElement pattern(2);
3115 VibrationSequence sequence(2);
3116 pattern.duration = std::chrono::milliseconds(200);
3117 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3118 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3119 sequence.addElement(pattern);
3120 pattern.duration = std::chrono::milliseconds(500);
3121 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3122 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3123 sequence.addElement(pattern);
3124
3125 std::vector<int64_t> timings = {0, 1};
3126 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3127
3128 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003129 // Start vibrating
3130 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003131 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003132 // Verify vibrator state listener was notified.
3133 mReader->loopOnce();
3134 NotifyVibratorStateArgs args;
3135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3136 ASSERT_EQ(DEVICE_ID, args.deviceId);
3137 ASSERT_TRUE(args.isOn);
3138 // Stop vibrating
3139 mapper.cancelVibrate(VIBRATION_TOKEN);
3140 ASSERT_FALSE(mapper.isVibrating());
3141 // Verify vibrator state listener was notified.
3142 mReader->loopOnce();
3143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3144 ASSERT_EQ(DEVICE_ID, args.deviceId);
3145 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003146}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
Chris Yef59a2f42020-10-16 12:55:26 -07003148// --- SensorInputMapperTest ---
3149
3150class SensorInputMapperTest : public InputMapperTest {
3151protected:
3152 static const int32_t ACCEL_RAW_MIN;
3153 static const int32_t ACCEL_RAW_MAX;
3154 static const int32_t ACCEL_RAW_FUZZ;
3155 static const int32_t ACCEL_RAW_FLAT;
3156 static const int32_t ACCEL_RAW_RESOLUTION;
3157
3158 static const int32_t GYRO_RAW_MIN;
3159 static const int32_t GYRO_RAW_MAX;
3160 static const int32_t GYRO_RAW_FUZZ;
3161 static const int32_t GYRO_RAW_FLAT;
3162 static const int32_t GYRO_RAW_RESOLUTION;
3163
3164 static const float GRAVITY_MS2_UNIT;
3165 static const float DEGREE_RADIAN_UNIT;
3166
3167 void prepareAccelAxes();
3168 void prepareGyroAxes();
3169 void setAccelProperties();
3170 void setGyroProperties();
3171 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3172};
3173
3174const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3175const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3176const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3177const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3178const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3179
3180const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3181const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3182const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3183const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3184const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3185
3186const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3187const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3188
3189void SensorInputMapperTest::prepareAccelAxes() {
3190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3191 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3192 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3193 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3194 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3195 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3196}
3197
3198void SensorInputMapperTest::prepareGyroAxes() {
3199 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3200 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3202 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3204 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3205}
3206
3207void SensorInputMapperTest::setAccelProperties() {
3208 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3209 /* sensorDataIndex */ 0);
3210 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3211 /* sensorDataIndex */ 1);
3212 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3213 /* sensorDataIndex */ 2);
3214 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3215 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3216 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3217 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3218 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3219}
3220
3221void SensorInputMapperTest::setGyroProperties() {
3222 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3223 /* sensorDataIndex */ 0);
3224 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3225 /* sensorDataIndex */ 1);
3226 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3227 /* sensorDataIndex */ 2);
3228 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3229 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3230 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3231 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3232 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3233}
3234
3235TEST_F(SensorInputMapperTest, GetSources) {
3236 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3237
3238 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3239}
3240
3241TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3242 setAccelProperties();
3243 prepareAccelAxes();
3244 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3245
3246 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3247 std::chrono::microseconds(10000),
3248 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003249 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003255
3256 NotifySensorArgs args;
3257 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3258 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3259 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3260
3261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3262 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3263 ASSERT_EQ(args.deviceId, DEVICE_ID);
3264 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3265 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3266 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3267 ASSERT_EQ(args.values, values);
3268 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3269}
3270
3271TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3272 setGyroProperties();
3273 prepareGyroAxes();
3274 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3275
3276 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3277 std::chrono::microseconds(10000),
3278 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003279 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003285
3286 NotifySensorArgs args;
3287 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3288 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3289 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3290
3291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3292 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3293 ASSERT_EQ(args.deviceId, DEVICE_ID);
3294 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3295 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3296 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3297 ASSERT_EQ(args.values, values);
3298 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3299}
3300
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301// --- KeyboardInputMapperTest ---
3302
3303class KeyboardInputMapperTest : public InputMapperTest {
3304protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003305 const std::string UNIQUE_ID = "local:0";
3306
3307 void prepareDisplay(int32_t orientation);
3308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003309 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003310 int32_t originalKeyCode, int32_t rotatedKeyCode,
3311 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312};
3313
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003314/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3315 * orientation.
3316 */
3317void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003318 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3319 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320}
3321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003323 int32_t originalScanCode, int32_t originalKeyCode,
3324 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 NotifyKeyArgs args;
3326
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3329 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3330 ASSERT_EQ(originalScanCode, args.scanCode);
3331 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003332 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3337 ASSERT_EQ(originalScanCode, args.scanCode);
3338 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340}
3341
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 KeyboardInputMapper& mapper =
3344 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3345 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348}
3349
3350TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3351 const int32_t USAGE_A = 0x070004;
3352 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3354 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003355 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3356 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3357 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003359 KeyboardInputMapper& mapper =
3360 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3361 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003362 // Initial metastate to AMETA_NONE.
3363 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3364 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
3366 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 NotifyKeyArgs args;
3369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3370 ASSERT_EQ(DEVICE_ID, args.deviceId);
3371 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3372 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3373 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3374 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3375 ASSERT_EQ(KEY_HOME, args.scanCode);
3376 ASSERT_EQ(AMETA_NONE, args.metaState);
3377 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3378 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3379 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3380
3381 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(DEVICE_ID, args.deviceId);
3385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3386 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3387 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3388 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3389 ASSERT_EQ(KEY_HOME, args.scanCode);
3390 ASSERT_EQ(AMETA_NONE, args.metaState);
3391 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3392 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3393 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3394
3395 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3399 ASSERT_EQ(DEVICE_ID, args.deviceId);
3400 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3401 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3402 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3403 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3404 ASSERT_EQ(0, args.scanCode);
3405 ASSERT_EQ(AMETA_NONE, args.metaState);
3406 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3407 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3408 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3409
3410 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3412 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3414 ASSERT_EQ(DEVICE_ID, args.deviceId);
3415 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3416 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3417 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3418 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3419 ASSERT_EQ(0, args.scanCode);
3420 ASSERT_EQ(AMETA_NONE, args.metaState);
3421 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3422 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3423 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3424
3425 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3429 ASSERT_EQ(DEVICE_ID, args.deviceId);
3430 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3431 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3432 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3433 ASSERT_EQ(0, args.keyCode);
3434 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3435 ASSERT_EQ(AMETA_NONE, args.metaState);
3436 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3437 ASSERT_EQ(0U, args.policyFlags);
3438 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3439
3440 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3442 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3444 ASSERT_EQ(DEVICE_ID, args.deviceId);
3445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3446 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3447 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3448 ASSERT_EQ(0, args.keyCode);
3449 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3450 ASSERT_EQ(AMETA_NONE, args.metaState);
3451 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3452 ASSERT_EQ(0U, args.policyFlags);
3453 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3454}
3455
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456/**
3457 * Ensure that the readTime is set to the time when the EV_KEY is received.
3458 */
3459TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3461
3462 KeyboardInputMapper& mapper =
3463 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3464 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3465 NotifyKeyArgs args;
3466
3467 // Key down
3468 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3470 ASSERT_EQ(12, args.readTime);
3471
3472 // Key up
3473 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3475 ASSERT_EQ(15, args.readTime);
3476}
3477
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003481 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3482 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3483 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 KeyboardInputMapper& mapper =
3486 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3487 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
arthurhungc903df12020-08-11 15:08:42 +08003489 // Initial metastate to AMETA_NONE.
3490 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3491 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
3493 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 NotifyKeyArgs args;
3496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003499 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
3501 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
3513 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003514 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3516 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003517 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003518 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003519}
3520
3521TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3523 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3524 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3525 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 KeyboardInputMapper& mapper =
3528 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3529 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003531 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3533 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3534 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3535 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3536 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3537 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3538 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3539 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3540}
3541
3542TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3546 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547
Michael Wrightd02c5b62014-02-10 15:10:22 -08003548 addConfigurationProperty("keyboard.orientationAware", "1");
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_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003554 ASSERT_NO_FATAL_FAILURE(
3555 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3556 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3557 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3558 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3559 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3560 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3561 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003563 clearViewports();
3564 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003565 ASSERT_NO_FATAL_FAILURE(
3566 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3567 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3568 AKEYCODE_DPAD_UP, DISPLAY_ID));
3569 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3570 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3571 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3572 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003573
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003574 clearViewports();
3575 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003576 ASSERT_NO_FATAL_FAILURE(
3577 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3578 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3579 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3580 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3581 AKEYCODE_DPAD_UP, DISPLAY_ID));
3582 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3583 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003585 clearViewports();
3586 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003587 ASSERT_NO_FATAL_FAILURE(
3588 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3590 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3591 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3592 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3593 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3594 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
3596 // Special case: if orientation changes while key is down, we still emit the same keycode
3597 // in the key up as we did in the key down.
3598 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
3600 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3604 ASSERT_EQ(KEY_UP, args.scanCode);
3605 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3606
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003607 clearViewports();
3608 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3611 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3612 ASSERT_EQ(KEY_UP, args.scanCode);
3613 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3614}
3615
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003616TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3617 // If the keyboard is not orientation aware,
3618 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003620
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003621 KeyboardInputMapper& mapper =
3622 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3623 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003624 NotifyKeyArgs args;
3625
3626 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3631 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3632
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003633 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3638 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3639}
3640
3641TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3642 // If the keyboard is orientation aware,
3643 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003646 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003647 KeyboardInputMapper& mapper =
3648 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3649 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650 NotifyKeyArgs args;
3651
3652 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3653 // ^--- already checked by the previous test
3654
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003655 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003656 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3661 ASSERT_EQ(DISPLAY_ID, args.displayId);
3662
3663 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003664 clearViewports();
3665 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003666 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3671 ASSERT_EQ(newDisplayId, args.displayId);
3672}
3673
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 KeyboardInputMapper& mapper =
3676 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3677 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003679 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003682 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003683 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684}
3685
Philip Junker4af3b3d2021-12-14 10:36:55 +01003686TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3687 KeyboardInputMapper& mapper =
3688 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3689 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3690
3691 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3692 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3693 << "If a mapping is available, the result is equal to the mapping";
3694
3695 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3696 << "If no mapping is available, the result is the key location";
3697}
3698
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 KeyboardInputMapper& mapper =
3701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003704 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003707 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
3711TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
3718 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3719 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003720 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721 ASSERT_TRUE(flags[0]);
3722 ASSERT_FALSE(flags[1]);
3723}
3724
3725TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3727 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3728 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3731 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003733 KeyboardInputMapper& mapper =
3734 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3735 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003736 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003737 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3738 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739
3740 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3742 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3743 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3750 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752
3753 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3757 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3758 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
3761 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003767 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
3769 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3774 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
3777 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3781 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3782 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003783 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
3785 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3790 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792}
3793
Chris Yea52ade12020-08-27 16:49:20 -07003794TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3796 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3797 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3798 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3799
3800 KeyboardInputMapper& mapper =
3801 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3802 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3803
3804 // Initial metastate should be AMETA_NONE as no meta keys added.
3805 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3806 // Meta state should be AMETA_NONE after reset
3807 mapper.reset(ARBITRARY_TIME);
3808 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3809 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3810 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3812
3813 NotifyKeyArgs args;
3814 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(AMETA_NONE, args.metaState);
3818 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3819 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3820 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3821
3822 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003823 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3825 ASSERT_EQ(AMETA_NONE, args.metaState);
3826 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3827 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3828 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3829}
3830
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3832 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3835 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3836 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003837
3838 // keyboard 2.
3839 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003840 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003841 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003842 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003843 std::shared_ptr<InputDevice> device2 =
3844 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3845 Flags<InputDeviceClass>(0));
3846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3850 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003852 KeyboardInputMapper& mapper =
3853 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3854 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003856 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003857 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3860 device2->reset(ARBITRARY_TIME);
3861
3862 // Prepared displays and associated info.
3863 constexpr uint8_t hdmi1 = 0;
3864 constexpr uint8_t hdmi2 = 1;
3865 const std::string SECONDARY_UNIQUE_ID = "local:1";
3866
3867 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3868 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3869
3870 // No associated display viewport found, should disable the device.
3871 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3872 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3873 ASSERT_FALSE(device2->isEnabled());
3874
3875 // Prepare second display.
3876 constexpr int32_t newDisplayId = 2;
3877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003878 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003879 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003880 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003881 // Default device will reconfigure above, need additional reconfiguration for another device.
3882 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3883 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3884
3885 // Device should be enabled after the associated display is found.
3886 ASSERT_TRUE(mDevice->isEnabled());
3887 ASSERT_TRUE(device2->isEnabled());
3888
3889 // Test pad key events
3890 ASSERT_NO_FATAL_FAILURE(
3891 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3892 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3893 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3894 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3895 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3896 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3897 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3898
3899 ASSERT_NO_FATAL_FAILURE(
3900 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3902 AKEYCODE_DPAD_RIGHT, newDisplayId));
3903 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3904 AKEYCODE_DPAD_DOWN, newDisplayId));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3906 AKEYCODE_DPAD_LEFT, newDisplayId));
3907}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
arthurhungc903df12020-08-11 15:08:42 +08003909TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3910 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3911 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3912 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3915 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3916
3917 KeyboardInputMapper& mapper =
3918 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3919 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3920 // Initial metastate to AMETA_NONE.
3921 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3922 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3923
3924 // Initialization should have turned all of the lights off.
3925 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3926 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3927 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3928
3929 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003932 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3933 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3934
3935 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003938 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3939 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3940
3941 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003944 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3945 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3946
3947 mFakeEventHub->removeDevice(EVENTHUB_ID);
3948 mReader->loopOnce();
3949
3950 // keyboard 2 should default toggle keys.
3951 const std::string USB2 = "USB2";
3952 const std::string DEVICE_NAME2 = "KEYBOARD2";
3953 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3954 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3955 std::shared_ptr<InputDevice> device2 =
3956 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3957 Flags<InputDeviceClass>(0));
3958 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3959 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3960 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3961 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3962 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3963 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3964
arthurhung6fe95782020-10-05 22:41:16 +08003965 KeyboardInputMapper& mapper2 =
3966 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003968 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3969 device2->reset(ARBITRARY_TIME);
3970
3971 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3972 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3973 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003974 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3975 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003976}
3977
Arthur Hungcb40a002021-08-03 14:31:01 +00003978TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3981 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3982
3983 // Suppose we have two mappers. (DPAD + KEYBOARD)
3984 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3985 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3986 KeyboardInputMapper& mapper =
3987 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3988 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3989 // Initialize metastate to AMETA_NUM_LOCK_ON.
3990 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3991 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3992
3993 mReader->toggleCapsLockState(DEVICE_ID);
3994 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3995}
3996
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003997// --- KeyboardInputMapperTest_ExternalDevice ---
3998
3999class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4000protected:
Chris Yea52ade12020-08-27 16:49:20 -07004001 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004002};
4003
4004TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004005 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4006 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004007
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4010 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4011 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013 KeyboardInputMapper& mapper =
4014 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4015 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004016
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004018 NotifyKeyArgs args;
4019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4020 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4021
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4024 ASSERT_EQ(uint32_t(0), args.policyFlags);
4025
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4028 ASSERT_EQ(uint32_t(0), args.policyFlags);
4029
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004030 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4032 ASSERT_EQ(uint32_t(0), args.policyFlags);
4033
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4036 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4037
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004038 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4040 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4041}
4042
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004043TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004044 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004045
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4048 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004049
Powei Fengd041c5d2019-05-03 17:11:33 -07004050 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004051 KeyboardInputMapper& mapper =
4052 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4053 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004054
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004056 NotifyKeyArgs args;
4057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4059
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4062 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4063
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4066 ASSERT_EQ(uint32_t(0), args.policyFlags);
4067
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004068 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4070 ASSERT_EQ(uint32_t(0), args.policyFlags);
4071
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4074 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4075
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004076 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4078 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4079}
4080
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081// --- CursorInputMapperTest ---
4082
4083class CursorInputMapperTest : public InputMapperTest {
4084protected:
4085 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4086
Michael Wright17db18e2020-06-26 20:51:44 +01004087 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
Chris Yea52ade12020-08-27 16:49:20 -07004089 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 InputMapperTest::SetUp();
4091
Michael Wright17db18e2020-06-26 20:51:44 +01004092 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004093 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 }
4095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004096 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4097 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004098
4099 void prepareDisplay(int32_t orientation) {
4100 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004101 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004102 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4103 orientation, uniqueId, NO_PORT, viewportType);
4104 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004105
4106 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4107 float pressure) {
4108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4109 0.0f, 0.0f, 0.0f, EPSILON));
4110 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111};
4112
4113const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004115void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4116 int32_t originalY, int32_t rotatedX,
4117 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 NotifyMotionArgs args;
4119
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004125 ASSERT_NO_FATAL_FAILURE(
4126 assertCursorPointerCoords(args.pointerCoords[0],
4127 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4128 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129}
4130
4131TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004133 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004134
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004135 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136}
4137
4138TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143}
4144
4145TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
4149 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004150 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
4152 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004153 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4154 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4156 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4157
4158 // When the bounds are set, then there should be a valid motion range.
4159 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4160
4161 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004162 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
4164 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4165 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4166 1, 800 - 1, 0.0f, 0.0f));
4167 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4168 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4169 2, 480 - 1, 0.0f, 0.0f));
4170 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4171 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4172 0.0f, 1.0f, 0.0f, 0.0f));
4173}
4174
4175TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004177 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178
4179 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004180 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
4182 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4183 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4184 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4185 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4186 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4187 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4188 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4189 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4190 0.0f, 1.0f, 0.0f, 0.0f));
4191}
4192
4193TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004195 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196
arthurhungdcef2dc2020-08-11 14:47:50 +08004197 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198
4199 NotifyMotionArgs args;
4200
4201 // Button press.
4202 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4206 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4207 ASSERT_EQ(DEVICE_ID, args.deviceId);
4208 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4209 ASSERT_EQ(uint32_t(0), args.policyFlags);
4210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4211 ASSERT_EQ(0, args.flags);
4212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4213 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4214 ASSERT_EQ(0, args.edgeFlags);
4215 ASSERT_EQ(uint32_t(1), args.pointerCount);
4216 ASSERT_EQ(0, args.pointerProperties[0].id);
4217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4220 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4221 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4224 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4225 ASSERT_EQ(DEVICE_ID, args.deviceId);
4226 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4227 ASSERT_EQ(uint32_t(0), args.policyFlags);
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4229 ASSERT_EQ(0, args.flags);
4230 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4231 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4232 ASSERT_EQ(0, args.edgeFlags);
4233 ASSERT_EQ(uint32_t(1), args.pointerCount);
4234 ASSERT_EQ(0, args.pointerProperties[0].id);
4235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004236 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4238 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4239 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4240
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004242 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4243 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4245 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4246 ASSERT_EQ(DEVICE_ID, args.deviceId);
4247 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4248 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4250 ASSERT_EQ(0, args.flags);
4251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4252 ASSERT_EQ(0, args.buttonState);
4253 ASSERT_EQ(0, args.edgeFlags);
4254 ASSERT_EQ(uint32_t(1), args.pointerCount);
4255 ASSERT_EQ(0, args.pointerProperties[0].id);
4256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004258 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4259 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4260 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4263 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4264 ASSERT_EQ(DEVICE_ID, args.deviceId);
4265 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4266 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4268 ASSERT_EQ(0, args.flags);
4269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4270 ASSERT_EQ(0, args.buttonState);
4271 ASSERT_EQ(0, args.edgeFlags);
4272 ASSERT_EQ(uint32_t(1), args.pointerCount);
4273 ASSERT_EQ(0, args.pointerProperties[0].id);
4274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004275 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4277 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4278 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4279}
4280
4281TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004283 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
4285 NotifyMotionArgs args;
4286
4287 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004292 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4293 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4294 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
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(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004301 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4302 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303}
4304
4305TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004307 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
4309 NotifyMotionArgs args;
4310
4311 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4315 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004316 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004320 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004327 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004331 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332}
4333
4334TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004336 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 NotifyMotionArgs args;
4339
4340 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4348 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4349 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4352 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4354 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4355 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004356
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004363 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4364 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4365 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
4367 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004372 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004373
4374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004376 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377}
4378
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004379TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004381 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4382 // need to be rotated.
4383 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004384 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004386 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4395}
4396
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004397TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004399 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4400 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004401 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004403 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4405 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4406 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4407 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4408 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4412
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004413 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4417 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4418 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4421 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004423 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4432
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004433 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4438 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4441 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442}
4443
4444TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004446 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4449 mFakePointerController->setPosition(100, 200);
4450 mFakePointerController->setButtonState(0);
4451
4452 NotifyMotionArgs motionArgs;
4453 NotifyKeyArgs keyArgs;
4454
4455 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4460 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4461 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4467 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4468 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(
4470 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 ASSERT_EQ(0, motionArgs.buttonState);
4477 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004478 ASSERT_NO_FATAL_FAILURE(
4479 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480
4481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004482 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_EQ(0, motionArgs.buttonState);
4484 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004485 ASSERT_NO_FATAL_FAILURE(
4486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004490 ASSERT_EQ(0, motionArgs.buttonState);
4491 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004492 ASSERT_NO_FATAL_FAILURE(
4493 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004494
4495 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4501 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4502 motionArgs.buttonState);
4503 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4504 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004505 ASSERT_NO_FATAL_FAILURE(
4506 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4510 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4511 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4512 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004513 ASSERT_NO_FATAL_FAILURE(
4514 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515
4516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4517 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4518 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4519 motionArgs.buttonState);
4520 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4521 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004522 ASSERT_NO_FATAL_FAILURE(
4523 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4530 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004536 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004538 ASSERT_NO_FATAL_FAILURE(
4539 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4545 ASSERT_EQ(0, motionArgs.buttonState);
4546 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(
4548 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004551
4552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_EQ(0, motionArgs.buttonState);
4554 ASSERT_EQ(0, mFakePointerController->getButtonState());
4555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004556 ASSERT_NO_FATAL_FAILURE(
4557 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4560 ASSERT_EQ(0, motionArgs.buttonState);
4561 ASSERT_EQ(0, mFakePointerController->getButtonState());
4562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004563 ASSERT_NO_FATAL_FAILURE(
4564 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565
4566 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4570 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4571 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004572
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4576 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004577 ASSERT_NO_FATAL_FAILURE(
4578 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4582 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4583 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004584 ASSERT_NO_FATAL_FAILURE(
4585 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_EQ(0, motionArgs.buttonState);
4592 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004593 ASSERT_NO_FATAL_FAILURE(
4594 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595
4596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598 ASSERT_EQ(0, motionArgs.buttonState);
4599 ASSERT_EQ(0, mFakePointerController->getButtonState());
4600
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004601 ASSERT_NO_FATAL_FAILURE(
4602 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4604 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4605 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4606
4607 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4611 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4612 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004613
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004618 ASSERT_NO_FATAL_FAILURE(
4619 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4624 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_EQ(0, motionArgs.buttonState);
4633 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004634 ASSERT_NO_FATAL_FAILURE(
4635 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004636
4637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4638 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004643
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4645 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4646 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4647
4648 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4652 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4653 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004654
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4664 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4665 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(0, motionArgs.buttonState);
4674 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004675 ASSERT_NO_FATAL_FAILURE(
4676 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004684
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4686 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4687 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4688
4689 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4693 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4694 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004695
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4699 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4705 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4706 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004707 ASSERT_NO_FATAL_FAILURE(
4708 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714 ASSERT_EQ(0, motionArgs.buttonState);
4715 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004716 ASSERT_NO_FATAL_FAILURE(
4717 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4721 ASSERT_EQ(0, motionArgs.buttonState);
4722 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(
4724 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4727 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4728 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4729}
4730
4731TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004733 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734
4735 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4736 mFakePointerController->setPosition(100, 200);
4737 mFakePointerController->setButtonState(0);
4738
4739 NotifyMotionArgs args;
4740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004745 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4746 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4748 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 +01004749 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004750}
4751
4752TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004753 addConfigurationProperty("cursor.mode", "pointer");
4754 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004755 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004756
4757 NotifyDeviceResetArgs resetArgs;
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4759 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4760 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4761
4762 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4763 mFakePointerController->setPosition(100, 200);
4764 mFakePointerController->setButtonState(0);
4765
4766 NotifyMotionArgs args;
4767
4768 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4773 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4776 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 +01004777 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004778
4779 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4783 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4786 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4788 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4791 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4792
4793 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004794 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4795 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4797 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4798 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4800 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4802 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4803 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4805 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4806
4807 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4812 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4815 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 +01004816 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004817
4818 // Disable pointer capture and check that the device generation got bumped
4819 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004820 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004821 mFakePolicy->setPointerCapture(false);
4822 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004823 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004824
4825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4826 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4827 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4833 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4836 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 +01004837 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004840TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004841 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004842
Garfield Tan888a6a42020-01-09 11:39:16 -08004843 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004844 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004845 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4846 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004847 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4848 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004849 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4850 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4851
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004852 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4853 mFakePointerController->setPosition(100, 200);
4854 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004855
4856 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4861 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4864 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 +01004865 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004866 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4867}
4868
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869// --- TouchInputMapperTest ---
4870
4871class TouchInputMapperTest : public InputMapperTest {
4872protected:
4873 static const int32_t RAW_X_MIN;
4874 static const int32_t RAW_X_MAX;
4875 static const int32_t RAW_Y_MIN;
4876 static const int32_t RAW_Y_MAX;
4877 static const int32_t RAW_TOUCH_MIN;
4878 static const int32_t RAW_TOUCH_MAX;
4879 static const int32_t RAW_TOOL_MIN;
4880 static const int32_t RAW_TOOL_MAX;
4881 static const int32_t RAW_PRESSURE_MIN;
4882 static const int32_t RAW_PRESSURE_MAX;
4883 static const int32_t RAW_ORIENTATION_MIN;
4884 static const int32_t RAW_ORIENTATION_MAX;
4885 static const int32_t RAW_DISTANCE_MIN;
4886 static const int32_t RAW_DISTANCE_MAX;
4887 static const int32_t RAW_TILT_MIN;
4888 static const int32_t RAW_TILT_MAX;
4889 static const int32_t RAW_ID_MIN;
4890 static const int32_t RAW_ID_MAX;
4891 static const int32_t RAW_SLOT_MIN;
4892 static const int32_t RAW_SLOT_MAX;
4893 static const float X_PRECISION;
4894 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004895 static const float X_PRECISION_VIRTUAL;
4896 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897
4898 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004899 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
4901 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4902
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004903 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004904 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004905
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 enum Axes {
4907 POSITION = 1 << 0,
4908 TOUCH = 1 << 1,
4909 TOOL = 1 << 2,
4910 PRESSURE = 1 << 3,
4911 ORIENTATION = 1 << 4,
4912 MINOR = 1 << 5,
4913 ID = 1 << 6,
4914 DISTANCE = 1 << 7,
4915 TILT = 1 << 8,
4916 SLOT = 1 << 9,
4917 TOOL_TYPE = 1 << 10,
4918 };
4919
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004920 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4921 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004922 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004923 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004924 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 int32_t toRawX(float displayX);
4926 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004927 int32_t toRotatedRawX(float displayX);
4928 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004929 float toCookedX(float rawX, float rawY);
4930 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004934 float toDisplayY(int32_t rawY, int32_t displayHeight);
4935
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936};
4937
4938const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4939const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4940const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4941const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4942const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4943const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4944const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4945const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004946const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4947const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4949const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4950const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4951const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4952const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4953const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4954const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4955const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4956const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4957const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4958const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4959const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004960const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4961 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4962const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4963 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004964const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4965 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967const float TouchInputMapperTest::GEOMETRIC_SCALE =
4968 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4969 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4970
4971const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4972 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4973 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4974};
4975
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004976void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004977 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4978 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004979}
4980
4981void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4982 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4983 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984}
4985
Santos Cordonfa5cf462017-04-05 10:37:00 -07004986void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004987 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4988 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4989 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004990}
4991
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004993 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4994 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997}
4998
Jason Gerecke489fda82012-09-07 17:19:40 -07004999void TouchInputMapperTest::prepareLocationCalibration() {
5000 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5001}
5002
Michael Wrightd02c5b62014-02-10 15:10:22 -08005003int32_t TouchInputMapperTest::toRawX(float displayX) {
5004 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5005}
5006
5007int32_t TouchInputMapperTest::toRawY(float displayY) {
5008 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5009}
5010
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005011int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5012 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5013}
5014
5015int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5016 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5017}
5018
Jason Gerecke489fda82012-09-07 17:19:40 -07005019float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5020 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5021 return rawX;
5022}
5023
5024float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5025 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5026 return rawY;
5027}
5028
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005030 return toDisplayX(rawX, DISPLAY_WIDTH);
5031}
5032
5033float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5034 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035}
5036
5037float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005038 return toDisplayY(rawY, DISPLAY_HEIGHT);
5039}
5040
5041float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5042 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043}
5044
5045
5046// --- SingleTouchInputMapperTest ---
5047
5048class SingleTouchInputMapperTest : public TouchInputMapperTest {
5049protected:
5050 void prepareButtons();
5051 void prepareAxes(int axes);
5052
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005053 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5054 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5055 void processUp(SingleTouchInputMapper& mappery);
5056 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5057 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5058 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5059 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5060 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5061 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062};
5063
5064void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005065 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066}
5067
5068void SingleTouchInputMapperTest::prepareAxes(int axes) {
5069 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 }
5073 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5075 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 }
5077 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005078 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5079 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 }
5081 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005082 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5083 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 }
5085 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088 }
5089}
5090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005091void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095}
5096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005097void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100}
5101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104}
5105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005106void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108}
5109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5111 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113}
5114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005115void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117}
5118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005119void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5120 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123}
5124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005125void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5126 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128}
5129
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005130void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132}
5133
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 prepareButtons();
5136 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005137 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005139 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140}
5141
5142TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005143 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5144 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 prepareButtons();
5146 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150}
5151
5152TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 prepareButtons();
5154 prepareAxes(POSITION);
5155 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159}
5160
5161TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005162 prepareButtons();
5163 prepareAxes(POSITION);
5164 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005165 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005168}
5169
5170TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171 addConfigurationProperty("touch.deviceType", "touchScreen");
5172 prepareDisplay(DISPLAY_ORIENTATION_0);
5173 prepareButtons();
5174 prepareAxes(POSITION);
5175 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005176 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177
5178 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005179 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180
5181 // Virtual key is down.
5182 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5183 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5184 processDown(mapper, x, y);
5185 processSync(mapper);
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005188 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
5190 // Virtual key is up.
5191 processUp(mapper);
5192 processSync(mapper);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5194
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005195 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196}
5197
5198TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 addConfigurationProperty("touch.deviceType", "touchScreen");
5200 prepareDisplay(DISPLAY_ORIENTATION_0);
5201 prepareButtons();
5202 prepareAxes(POSITION);
5203 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005204 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205
5206 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208
5209 // Virtual key is down.
5210 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5211 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5212 processDown(mapper, x, y);
5213 processSync(mapper);
5214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005216 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217
5218 // Virtual key is up.
5219 processUp(mapper);
5220 processSync(mapper);
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224}
5225
5226TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227 addConfigurationProperty("touch.deviceType", "touchScreen");
5228 prepareDisplay(DISPLAY_ORIENTATION_0);
5229 prepareButtons();
5230 prepareAxes(POSITION);
5231 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233
5234 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5235 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005236 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 ASSERT_TRUE(flags[0]);
5238 ASSERT_FALSE(flags[1]);
5239}
5240
5241TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242 addConfigurationProperty("touch.deviceType", "touchScreen");
5243 prepareDisplay(DISPLAY_ORIENTATION_0);
5244 prepareButtons();
5245 prepareAxes(POSITION);
5246 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005247 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248
arthurhungdcef2dc2020-08-11 14:47:50 +08005249 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250
5251 NotifyKeyArgs args;
5252
5253 // Press virtual key.
5254 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5255 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5256 processDown(mapper, x, y);
5257 processSync(mapper);
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5260 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5261 ASSERT_EQ(DEVICE_ID, args.deviceId);
5262 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5263 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5264 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5265 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5266 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5267 ASSERT_EQ(KEY_HOME, args.scanCode);
5268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5269 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5270
5271 // Release virtual key.
5272 processUp(mapper);
5273 processSync(mapper);
5274
5275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5276 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5277 ASSERT_EQ(DEVICE_ID, args.deviceId);
5278 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5279 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5280 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5281 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5282 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5283 ASSERT_EQ(KEY_HOME, args.scanCode);
5284 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5285 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5286
5287 // Should not have sent any motions.
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5289}
5290
5291TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 addConfigurationProperty("touch.deviceType", "touchScreen");
5293 prepareDisplay(DISPLAY_ORIENTATION_0);
5294 prepareButtons();
5295 prepareAxes(POSITION);
5296 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
arthurhungdcef2dc2020-08-11 14:47:50 +08005299 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
5301 NotifyKeyArgs keyArgs;
5302
5303 // Press virtual key.
5304 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5305 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5306 processDown(mapper, x, y);
5307 processSync(mapper);
5308
5309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5310 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5311 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5312 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5313 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5314 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5315 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5316 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5317 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5319 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5320
5321 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5322 // into the display area.
5323 y -= 100;
5324 processMove(mapper, x, y);
5325 processSync(mapper);
5326
5327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5328 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5329 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5330 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5331 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5332 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5333 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5334 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5335 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5336 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5337 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5338 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5339
5340 NotifyMotionArgs motionArgs;
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5343 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5344 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5345 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5347 ASSERT_EQ(0, motionArgs.flags);
5348 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5349 ASSERT_EQ(0, motionArgs.buttonState);
5350 ASSERT_EQ(0, motionArgs.edgeFlags);
5351 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5352 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5355 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5356 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5357 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5359
5360 // Keep moving out of bounds. Should generate a pointer move.
5361 y -= 50;
5362 processMove(mapper, x, y);
5363 processSync(mapper);
5364
5365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5367 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5368 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5369 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5371 ASSERT_EQ(0, motionArgs.flags);
5372 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5373 ASSERT_EQ(0, motionArgs.buttonState);
5374 ASSERT_EQ(0, motionArgs.edgeFlags);
5375 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5379 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5380 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5381 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5383
5384 // Release out of bounds. Should generate a pointer up.
5385 processUp(mapper);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5391 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5392 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5394 ASSERT_EQ(0, motionArgs.flags);
5395 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5396 ASSERT_EQ(0, motionArgs.buttonState);
5397 ASSERT_EQ(0, motionArgs.edgeFlags);
5398 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5399 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5402 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // Should not have sent any more keys or motions.
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5410}
5411
5412TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 addConfigurationProperty("touch.deviceType", "touchScreen");
5414 prepareDisplay(DISPLAY_ORIENTATION_0);
5415 prepareButtons();
5416 prepareAxes(POSITION);
5417 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005418 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419
arthurhungdcef2dc2020-08-11 14:47:50 +08005420 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421
5422 NotifyMotionArgs motionArgs;
5423
5424 // Initially go down out of bounds.
5425 int32_t x = -10;
5426 int32_t y = -10;
5427 processDown(mapper, x, y);
5428 processSync(mapper);
5429
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5431
5432 // Move into the display area. Should generate a pointer down.
5433 x = 50;
5434 y = 75;
5435 processMove(mapper, x, y);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5440 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5441 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5442 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5444 ASSERT_EQ(0, motionArgs.flags);
5445 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5446 ASSERT_EQ(0, motionArgs.buttonState);
5447 ASSERT_EQ(0, motionArgs.edgeFlags);
5448 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5452 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5453 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5454 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5456
5457 // Release. Should generate a pointer up.
5458 processUp(mapper);
5459 processSync(mapper);
5460
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5462 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5463 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5464 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5465 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5467 ASSERT_EQ(0, motionArgs.flags);
5468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5469 ASSERT_EQ(0, motionArgs.buttonState);
5470 ASSERT_EQ(0, motionArgs.edgeFlags);
5471 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5472 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5475 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5476 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5477 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5478 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5479
5480 // Should not have sent any more keys or motions.
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5483}
5484
Santos Cordonfa5cf462017-04-05 10:37:00 -07005485TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005486 addConfigurationProperty("touch.deviceType", "touchScreen");
5487 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5488
5489 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5490 prepareButtons();
5491 prepareAxes(POSITION);
5492 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005493 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005494
arthurhungdcef2dc2020-08-11 14:47:50 +08005495 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005496
5497 NotifyMotionArgs motionArgs;
5498
5499 // Down.
5500 int32_t x = 100;
5501 int32_t y = 125;
5502 processDown(mapper, x, y);
5503 processSync(mapper);
5504
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(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5509 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5510 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5512 ASSERT_EQ(0, motionArgs.flags);
5513 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5514 ASSERT_EQ(0, motionArgs.buttonState);
5515 ASSERT_EQ(0, motionArgs.edgeFlags);
5516 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5517 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5520 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5521 1, 0, 0, 0, 0, 0, 0, 0));
5522 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5523 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5525
5526 // Move.
5527 x += 50;
5528 y += 75;
5529 processMove(mapper, x, y);
5530 processSync(mapper);
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5534 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5535 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5537 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5539 ASSERT_EQ(0, motionArgs.flags);
5540 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5541 ASSERT_EQ(0, motionArgs.buttonState);
5542 ASSERT_EQ(0, motionArgs.edgeFlags);
5543 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5544 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5547 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5548 1, 0, 0, 0, 0, 0, 0, 0));
5549 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5550 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5552
5553 // Up.
5554 processUp(mapper);
5555 processSync(mapper);
5556
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5559 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5560 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5564 ASSERT_EQ(0, motionArgs.flags);
5565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5566 ASSERT_EQ(0, motionArgs.buttonState);
5567 ASSERT_EQ(0, motionArgs.edgeFlags);
5568 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5572 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5573 1, 0, 0, 0, 0, 0, 0, 0));
5574 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5575 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5577
5578 // Should not have sent any more keys or motions.
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5581}
5582
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 addConfigurationProperty("touch.deviceType", "touchScreen");
5585 prepareDisplay(DISPLAY_ORIENTATION_0);
5586 prepareButtons();
5587 prepareAxes(POSITION);
5588 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005589 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
arthurhungdcef2dc2020-08-11 14:47:50 +08005591 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 NotifyMotionArgs motionArgs;
5594
5595 // Down.
5596 int32_t x = 100;
5597 int32_t y = 125;
5598 processDown(mapper, x, y);
5599 processSync(mapper);
5600
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5603 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5604 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5605 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5607 ASSERT_EQ(0, motionArgs.flags);
5608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5609 ASSERT_EQ(0, motionArgs.buttonState);
5610 ASSERT_EQ(0, motionArgs.edgeFlags);
5611 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5615 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5616 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5617 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5619
5620 // Move.
5621 x += 50;
5622 y += 75;
5623 processMove(mapper, x, y);
5624 processSync(mapper);
5625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5628 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5630 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5632 ASSERT_EQ(0, motionArgs.flags);
5633 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5634 ASSERT_EQ(0, motionArgs.buttonState);
5635 ASSERT_EQ(0, motionArgs.edgeFlags);
5636 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5640 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5641 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5642 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5643 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5644
5645 // Up.
5646 processUp(mapper);
5647 processSync(mapper);
5648
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5651 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5652 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5653 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5655 ASSERT_EQ(0, motionArgs.flags);
5656 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5657 ASSERT_EQ(0, motionArgs.buttonState);
5658 ASSERT_EQ(0, motionArgs.edgeFlags);
5659 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5660 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5661 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5663 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5664 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5665 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5666 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5667
5668 // Should not have sent any more keys or motions.
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5671}
5672
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005673TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 addConfigurationProperty("touch.deviceType", "touchScreen");
5675 prepareButtons();
5676 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005677 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5678 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005679 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680
5681 NotifyMotionArgs args;
5682
5683 // Rotation 90.
5684 prepareDisplay(DISPLAY_ORIENTATION_90);
5685 processDown(mapper, toRawX(50), toRawY(75));
5686 processSync(mapper);
5687
5688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5689 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5690 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5691
5692 processUp(mapper);
5693 processSync(mapper);
5694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5695}
5696
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005697TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698 addConfigurationProperty("touch.deviceType", "touchScreen");
5699 prepareButtons();
5700 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005701 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5702 // orientation-aware are affected by display rotation.
5703 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005704 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 NotifyMotionArgs args;
5707
5708 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005709 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 prepareDisplay(DISPLAY_ORIENTATION_0);
5711 processDown(mapper, toRawX(50), toRawY(75));
5712 processSync(mapper);
5713
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5715 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5716 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5717
5718 processUp(mapper);
5719 processSync(mapper);
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5721
5722 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005723 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005725 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726 processSync(mapper);
5727
5728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5729 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5730 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5731
5732 processUp(mapper);
5733 processSync(mapper);
5734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5735
5736 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005737 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 prepareDisplay(DISPLAY_ORIENTATION_180);
5739 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5743 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5744 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5745
5746 processUp(mapper);
5747 processSync(mapper);
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5749
5750 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005751 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005753 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754 processSync(mapper);
5755
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5757 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5758 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5759
5760 processUp(mapper);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5763}
5764
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005765TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareButtons();
5768 prepareAxes(POSITION);
5769 addConfigurationProperty("touch.orientationAware", "1");
5770 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5771 clearViewports();
5772 prepareDisplay(DISPLAY_ORIENTATION_0);
5773 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5774 NotifyMotionArgs args;
5775
5776 // Orientation 0.
5777 processDown(mapper, toRawX(50), toRawY(75));
5778 processSync(mapper);
5779
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5781 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5782 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5783
5784 processUp(mapper);
5785 processSync(mapper);
5786 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5787}
5788
5789TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5790 addConfigurationProperty("touch.deviceType", "touchScreen");
5791 prepareButtons();
5792 prepareAxes(POSITION);
5793 addConfigurationProperty("touch.orientationAware", "1");
5794 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5795 clearViewports();
5796 prepareDisplay(DISPLAY_ORIENTATION_0);
5797 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5798 NotifyMotionArgs args;
5799
5800 // Orientation 90.
5801 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5802 processSync(mapper);
5803
5804 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5805 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5806 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5807
5808 processUp(mapper);
5809 processSync(mapper);
5810 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5811}
5812
5813TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5814 addConfigurationProperty("touch.deviceType", "touchScreen");
5815 prepareButtons();
5816 prepareAxes(POSITION);
5817 addConfigurationProperty("touch.orientationAware", "1");
5818 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5819 clearViewports();
5820 prepareDisplay(DISPLAY_ORIENTATION_0);
5821 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5822 NotifyMotionArgs args;
5823
5824 // Orientation 180.
5825 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5826 processSync(mapper);
5827
5828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5829 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5830 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5831
5832 processUp(mapper);
5833 processSync(mapper);
5834 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5835}
5836
5837TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5838 addConfigurationProperty("touch.deviceType", "touchScreen");
5839 prepareButtons();
5840 prepareAxes(POSITION);
5841 addConfigurationProperty("touch.orientationAware", "1");
5842 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5843 clearViewports();
5844 prepareDisplay(DISPLAY_ORIENTATION_0);
5845 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5846 NotifyMotionArgs args;
5847
5848 // Orientation 270.
5849 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5850 processSync(mapper);
5851
5852 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5853 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5854 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5855
5856 processUp(mapper);
5857 processSync(mapper);
5858 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5859}
5860
5861TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5862 addConfigurationProperty("touch.deviceType", "touchScreen");
5863 prepareButtons();
5864 prepareAxes(POSITION);
5865 // 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");
5868 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5869 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5870
5871 NotifyMotionArgs args;
5872
5873 // Orientation 90, Rotation 0.
5874 clearViewports();
5875 prepareDisplay(DISPLAY_ORIENTATION_0);
5876 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5877 processSync(mapper);
5878
5879 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5880 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5881 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5882
5883 processUp(mapper);
5884 processSync(mapper);
5885 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5886
5887 // Orientation 90, Rotation 90.
5888 clearViewports();
5889 prepareDisplay(DISPLAY_ORIENTATION_90);
5890 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5891 processSync(mapper);
5892
5893 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5894 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5895 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5896
5897 processUp(mapper);
5898 processSync(mapper);
5899 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5900
5901 // Orientation 90, Rotation 180.
5902 clearViewports();
5903 prepareDisplay(DISPLAY_ORIENTATION_180);
5904 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5905 processSync(mapper);
5906
5907 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5908 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5909 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5910
5911 processUp(mapper);
5912 processSync(mapper);
5913 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5914
5915 // Orientation 90, Rotation 270.
5916 clearViewports();
5917 prepareDisplay(DISPLAY_ORIENTATION_270);
5918 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5919 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5920 processSync(mapper);
5921
5922 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5923 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5924 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5925
5926 processUp(mapper);
5927 processSync(mapper);
5928 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5929}
5930
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932 addConfigurationProperty("touch.deviceType", "touchScreen");
5933 prepareDisplay(DISPLAY_ORIENTATION_0);
5934 prepareButtons();
5935 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005936 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937
5938 // These calculations are based on the input device calibration documentation.
5939 int32_t rawX = 100;
5940 int32_t rawY = 200;
5941 int32_t rawPressure = 10;
5942 int32_t rawToolMajor = 12;
5943 int32_t rawDistance = 2;
5944 int32_t rawTiltX = 30;
5945 int32_t rawTiltY = 110;
5946
5947 float x = toDisplayX(rawX);
5948 float y = toDisplayY(rawY);
5949 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5950 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5951 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5952 float distance = float(rawDistance);
5953
5954 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5955 float tiltScale = M_PI / 180;
5956 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5957 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5958 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5959 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5960
5961 processDown(mapper, rawX, rawY);
5962 processPressure(mapper, rawPressure);
5963 processToolMajor(mapper, rawToolMajor);
5964 processDistance(mapper, rawDistance);
5965 processTilt(mapper, rawTiltX, rawTiltY);
5966 processSync(mapper);
5967
5968 NotifyMotionArgs args;
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5971 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5972 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5973}
5974
Jason Gerecke489fda82012-09-07 17:19:40 -07005975TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005976 addConfigurationProperty("touch.deviceType", "touchScreen");
5977 prepareDisplay(DISPLAY_ORIENTATION_0);
5978 prepareLocationCalibration();
5979 prepareButtons();
5980 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005981 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005982
5983 int32_t rawX = 100;
5984 int32_t rawY = 200;
5985
5986 float x = toDisplayX(toCookedX(rawX, rawY));
5987 float y = toDisplayY(toCookedY(rawX, rawY));
5988
5989 processDown(mapper, rawX, rawY);
5990 processSync(mapper);
5991
5992 NotifyMotionArgs args;
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5995 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5996}
5997
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 addConfigurationProperty("touch.deviceType", "touchScreen");
6000 prepareDisplay(DISPLAY_ORIENTATION_0);
6001 prepareButtons();
6002 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006003 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004
6005 NotifyMotionArgs motionArgs;
6006 NotifyKeyArgs keyArgs;
6007
6008 processDown(mapper, 100, 200);
6009 processSync(mapper);
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6012 ASSERT_EQ(0, motionArgs.buttonState);
6013
6014 // press BTN_LEFT, release BTN_LEFT
6015 processKey(mapper, BTN_LEFT, 1);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6019 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6020
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6023 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6024
Michael Wrightd02c5b62014-02-10 15:10:22 -08006025 processKey(mapper, BTN_LEFT, 0);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006030
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034
6035 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6036 processKey(mapper, BTN_RIGHT, 1);
6037 processKey(mapper, BTN_MIDDLE, 1);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6041 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6042 motionArgs.buttonState);
6043
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6046 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6051 motionArgs.buttonState);
6052
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 processKey(mapper, BTN_RIGHT, 0);
6054 processSync(mapper);
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006058
6059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006061 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062
6063 processKey(mapper, BTN_MIDDLE, 0);
6064 processSync(mapper);
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006071 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072
6073 // press BTN_BACK, release BTN_BACK
6074 processKey(mapper, BTN_BACK, 1);
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6077 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6078 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006079
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006082 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6083
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6086 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087
6088 processKey(mapper, BTN_BACK, 0);
6089 processSync(mapper);
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006093
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006096 ASSERT_EQ(0, motionArgs.buttonState);
6097
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6099 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6100 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6101
6102 // press BTN_SIDE, release BTN_SIDE
6103 processKey(mapper, BTN_SIDE, 1);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6106 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6107 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006108
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6112
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6115 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116
6117 processKey(mapper, BTN_SIDE, 0);
6118 processSync(mapper);
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006122
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_EQ(0, motionArgs.buttonState);
6126
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6128 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6129 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6130
6131 // press BTN_FORWARD, release BTN_FORWARD
6132 processKey(mapper, BTN_FORWARD, 1);
6133 processSync(mapper);
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6135 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6136 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006137
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145
6146 processKey(mapper, BTN_FORWARD, 0);
6147 processSync(mapper);
6148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006151
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006154 ASSERT_EQ(0, motionArgs.buttonState);
6155
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6158 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6159
6160 // press BTN_EXTRA, release BTN_EXTRA
6161 processKey(mapper, BTN_EXTRA, 1);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6164 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6165 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6173 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174
6175 processKey(mapper, BTN_EXTRA, 0);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006183 ASSERT_EQ(0, motionArgs.buttonState);
6184
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6186 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6187 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6188
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6190
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 // press BTN_STYLUS, release BTN_STYLUS
6192 processKey(mapper, BTN_STYLUS, 1);
6193 processSync(mapper);
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006196 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6197
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6200 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201
6202 processKey(mapper, BTN_STYLUS, 0);
6203 processSync(mapper);
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 // press BTN_STYLUS2, release BTN_STYLUS2
6213 processKey(mapper, BTN_STYLUS2, 1);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
6223 processKey(mapper, BTN_STYLUS2, 0);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006228
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006231 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006232
6233 // release touch
6234 processUp(mapper);
6235 processSync(mapper);
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6238 ASSERT_EQ(0, motionArgs.buttonState);
6239}
6240
6241TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242 addConfigurationProperty("touch.deviceType", "touchScreen");
6243 prepareDisplay(DISPLAY_ORIENTATION_0);
6244 prepareButtons();
6245 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006246 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247
6248 NotifyMotionArgs motionArgs;
6249
6250 // default tool type is finger
6251 processDown(mapper, 100, 200);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6256
6257 // eraser
6258 processKey(mapper, BTN_TOOL_RUBBER, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6263
6264 // stylus
6265 processKey(mapper, BTN_TOOL_RUBBER, 0);
6266 processKey(mapper, BTN_TOOL_PEN, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6271
6272 // brush
6273 processKey(mapper, BTN_TOOL_PEN, 0);
6274 processKey(mapper, BTN_TOOL_BRUSH, 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6279
6280 // pencil
6281 processKey(mapper, BTN_TOOL_BRUSH, 0);
6282 processKey(mapper, BTN_TOOL_PENCIL, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6287
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006288 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289 processKey(mapper, BTN_TOOL_PENCIL, 0);
6290 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6295
6296 // mouse
6297 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6298 processKey(mapper, BTN_TOOL_MOUSE, 1);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6303
6304 // lens
6305 processKey(mapper, BTN_TOOL_MOUSE, 0);
6306 processKey(mapper, BTN_TOOL_LENS, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6311
6312 // double-tap
6313 processKey(mapper, BTN_TOOL_LENS, 0);
6314 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6319
6320 // triple-tap
6321 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6322 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327
6328 // quad-tap
6329 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6330 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6331 processSync(mapper);
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6335
6336 // finger
6337 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6338 processKey(mapper, BTN_TOOL_FINGER, 1);
6339 processSync(mapper);
6340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6343
6344 // stylus trumps finger
6345 processKey(mapper, BTN_TOOL_PEN, 1);
6346 processSync(mapper);
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6350
6351 // eraser trumps stylus
6352 processKey(mapper, BTN_TOOL_RUBBER, 1);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6357
6358 // mouse trumps eraser
6359 processKey(mapper, BTN_TOOL_MOUSE, 1);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6364
6365 // back to default tool type
6366 processKey(mapper, BTN_TOOL_MOUSE, 0);
6367 processKey(mapper, BTN_TOOL_RUBBER, 0);
6368 processKey(mapper, BTN_TOOL_PEN, 0);
6369 processKey(mapper, BTN_TOOL_FINGER, 0);
6370 processSync(mapper);
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6374}
6375
6376TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006377 addConfigurationProperty("touch.deviceType", "touchScreen");
6378 prepareDisplay(DISPLAY_ORIENTATION_0);
6379 prepareButtons();
6380 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006381 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006382 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006383
6384 NotifyMotionArgs motionArgs;
6385
6386 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6387 processKey(mapper, BTN_TOOL_FINGER, 1);
6388 processMove(mapper, 100, 200);
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6393 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6394
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 // move a little
6401 processMove(mapper, 150, 250);
6402 processSync(mapper);
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6406 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6407
6408 // down when BTN_TOUCH is pressed, pressure defaults to 1
6409 processKey(mapper, BTN_TOUCH, 1);
6410 processSync(mapper);
6411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6414 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6415
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6420
6421 // up when BTN_TOUCH is released, hover restored
6422 processKey(mapper, BTN_TOUCH, 0);
6423 processSync(mapper);
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6427 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6432 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6433
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6437 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6438
6439 // exit hover when pointer goes away
6440 processKey(mapper, BTN_TOOL_FINGER, 0);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6443 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6445 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6446}
6447
6448TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449 addConfigurationProperty("touch.deviceType", "touchScreen");
6450 prepareDisplay(DISPLAY_ORIENTATION_0);
6451 prepareButtons();
6452 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006453 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454
6455 NotifyMotionArgs motionArgs;
6456
6457 // initially hovering because pressure is 0
6458 processDown(mapper, 100, 200);
6459 processPressure(mapper, 0);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6464 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6465
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6469 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6470
6471 // move a little
6472 processMove(mapper, 150, 250);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6477 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6478
6479 // down when pressure is non-zero
6480 processPressure(mapper, RAW_PRESSURE_MAX);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6483 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6485 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6486
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6490 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6491
6492 // up when pressure becomes 0, hover restored
6493 processPressure(mapper, 0);
6494 processSync(mapper);
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6496 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6498 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6504
6505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6508 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6509
6510 // exit hover when pointer goes away
6511 processUp(mapper);
6512 processSync(mapper);
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6516 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6517}
6518
Prabir Pradhan5632d622021-09-06 07:57:20 -07006519// --- TouchDisplayProjectionTest ---
6520
6521class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6522public:
6523 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6524 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6525 // rotated equivalent of the given un-rotated physical display bounds.
6526 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6527 uint32_t inverseRotationFlags;
6528 auto width = DISPLAY_WIDTH;
6529 auto height = DISPLAY_HEIGHT;
6530 switch (orientation) {
6531 case DISPLAY_ORIENTATION_90:
6532 inverseRotationFlags = ui::Transform::ROT_270;
6533 std::swap(width, height);
6534 break;
6535 case DISPLAY_ORIENTATION_180:
6536 inverseRotationFlags = ui::Transform::ROT_180;
6537 break;
6538 case DISPLAY_ORIENTATION_270:
6539 inverseRotationFlags = ui::Transform::ROT_90;
6540 std::swap(width, height);
6541 break;
6542 case DISPLAY_ORIENTATION_0:
6543 inverseRotationFlags = ui::Transform::ROT_0;
6544 break;
6545 default:
6546 FAIL() << "Invalid orientation: " << orientation;
6547 }
6548
6549 const ui::Transform rotation(inverseRotationFlags, width, height);
6550 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6551
6552 std::optional<DisplayViewport> internalViewport =
6553 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6554 DisplayViewport& v = *internalViewport;
6555 v.displayId = DISPLAY_ID;
6556 v.orientation = orientation;
6557
6558 v.logicalLeft = 0;
6559 v.logicalTop = 0;
6560 v.logicalRight = 100;
6561 v.logicalBottom = 100;
6562
6563 v.physicalLeft = rotatedPhysicalDisplay.left;
6564 v.physicalTop = rotatedPhysicalDisplay.top;
6565 v.physicalRight = rotatedPhysicalDisplay.right;
6566 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6567
6568 v.deviceWidth = width;
6569 v.deviceHeight = height;
6570
6571 v.isActive = true;
6572 v.uniqueId = UNIQUE_ID;
6573 v.type = ViewportType::INTERNAL;
6574 mFakePolicy->updateViewport(v);
6575 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6576 }
6577
6578 void assertReceivedMove(const Point& point) {
6579 NotifyMotionArgs motionArgs;
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6584 1, 0, 0, 0, 0, 0, 0, 0));
6585 }
6586};
6587
6588TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6589 addConfigurationProperty("touch.deviceType", "touchScreen");
6590 prepareDisplay(DISPLAY_ORIENTATION_0);
6591
6592 prepareButtons();
6593 prepareAxes(POSITION);
6594 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6595
6596 NotifyMotionArgs motionArgs;
6597
6598 // Configure the DisplayViewport such that the logical display maps to a subsection of
6599 // the display panel called the physical display. Here, the physical display is bounded by the
6600 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6601 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6602 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6603 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6604
6605 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6606 DISPLAY_ORIENTATION_270}) {
6607 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6608
6609 // Touches outside the physical display should be ignored, and should not generate any
6610 // events. Ensure touches at the following points that lie outside of the physical display
6611 // area do not generate any events.
6612 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6613 processDown(mapper, toRawX(point.x), toRawY(point.y));
6614 processSync(mapper);
6615 processUp(mapper);
6616 processSync(mapper);
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6618 << "Unexpected event generated for touch outside physical display at point: "
6619 << point.x << ", " << point.y;
6620 }
6621 }
6622}
6623
6624TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6625 addConfigurationProperty("touch.deviceType", "touchScreen");
6626 prepareDisplay(DISPLAY_ORIENTATION_0);
6627
6628 prepareButtons();
6629 prepareAxes(POSITION);
6630 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6631
6632 NotifyMotionArgs motionArgs;
6633
6634 // Configure the DisplayViewport such that the logical display maps to a subsection of
6635 // the display panel called the physical display. Here, the physical display is bounded by the
6636 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6637 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6638
6639 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6640 DISPLAY_ORIENTATION_270}) {
6641 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6642
6643 // Touches that start outside the physical display should be ignored until it enters the
6644 // physical display bounds, at which point it should generate a down event. Start a touch at
6645 // the point (5, 100), which is outside the physical display bounds.
6646 static const Point kOutsidePoint{5, 100};
6647 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6648 processSync(mapper);
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6650
6651 // Move the touch into the physical display area. This should generate a pointer down.
6652 processMove(mapper, toRawX(11), toRawY(21));
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6656 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6657 ASSERT_NO_FATAL_FAILURE(
6658 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6659
6660 // Move the touch inside the physical display area. This should generate a pointer move.
6661 processMove(mapper, toRawX(69), toRawY(159));
6662 processSync(mapper);
6663 assertReceivedMove({69, 159});
6664
6665 // Move outside the physical display area. Since the pointer is already down, this should
6666 // now continue generating events.
6667 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6668 processSync(mapper);
6669 assertReceivedMove(kOutsidePoint);
6670
6671 // Release. This should generate a pointer up.
6672 processUp(mapper);
6673 processSync(mapper);
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6677 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6678
6679 // Ensure no more events were generated.
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6682 }
6683}
6684
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685// --- MultiTouchInputMapperTest ---
6686
6687class MultiTouchInputMapperTest : public TouchInputMapperTest {
6688protected:
6689 void prepareAxes(int axes);
6690
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006691 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6692 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6693 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6694 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6695 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6696 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6697 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6698 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6699 void processId(MultiTouchInputMapper& mapper, int32_t id);
6700 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6701 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6702 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6703 void processMTSync(MultiTouchInputMapper& mapper);
6704 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705};
6706
6707void MultiTouchInputMapperTest::prepareAxes(int axes) {
6708 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006709 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6710 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711 }
6712 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6714 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006716 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6717 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718 }
6719 }
6720 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006721 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6722 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006724 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006725 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006726 }
6727 }
6728 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6730 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006731 }
6732 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6734 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006735 }
6736 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6738 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006739 }
6740 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006741 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6742 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006743 }
6744 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006745 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6746 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006747 }
6748 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006750 }
6751}
6752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006753void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6754 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006757}
6758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006759void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6760 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006762}
6763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006764void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6765 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006767}
6768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006769void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006771}
6772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006773void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775}
6776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006777void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6778 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780}
6781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006782void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784}
6785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006786void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006788}
6789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006790void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792}
6793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006794void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006796}
6797
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006798void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800}
6801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006802void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6803 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805}
6806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006807void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006809}
6810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006811void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813}
6814
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006816 addConfigurationProperty("touch.deviceType", "touchScreen");
6817 prepareDisplay(DISPLAY_ORIENTATION_0);
6818 prepareAxes(POSITION);
6819 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006820 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821
arthurhungdcef2dc2020-08-11 14:47:50 +08006822 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823
6824 NotifyMotionArgs motionArgs;
6825
6826 // Two fingers down at once.
6827 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6828 processPosition(mapper, x1, y1);
6829 processMTSync(mapper);
6830 processPosition(mapper, x2, y2);
6831 processMTSync(mapper);
6832 processSync(mapper);
6833
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6836 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6837 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6838 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6839 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6840 ASSERT_EQ(0, motionArgs.flags);
6841 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6842 ASSERT_EQ(0, motionArgs.buttonState);
6843 ASSERT_EQ(0, motionArgs.edgeFlags);
6844 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6845 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6846 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6848 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6849 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6850 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6851 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6855 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6856 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6857 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006858 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859 ASSERT_EQ(0, motionArgs.flags);
6860 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6861 ASSERT_EQ(0, motionArgs.buttonState);
6862 ASSERT_EQ(0, motionArgs.edgeFlags);
6863 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6866 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6869 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6871 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6872 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6873 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6875
6876 // Move.
6877 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6878 processPosition(mapper, x1, y1);
6879 processMTSync(mapper);
6880 processPosition(mapper, x2, y2);
6881 processMTSync(mapper);
6882 processSync(mapper);
6883
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6886 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6887 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6888 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6890 ASSERT_EQ(0, motionArgs.flags);
6891 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6892 ASSERT_EQ(0, motionArgs.buttonState);
6893 ASSERT_EQ(0, motionArgs.edgeFlags);
6894 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6895 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6897 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6900 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6902 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6906
6907 // First finger up.
6908 x2 += 15; y2 -= 20;
6909 processPosition(mapper, x2, y2);
6910 processMTSync(mapper);
6911 processSync(mapper);
6912
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6914 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6915 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6916 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6917 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006918 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919 ASSERT_EQ(0, motionArgs.flags);
6920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6921 ASSERT_EQ(0, motionArgs.buttonState);
6922 ASSERT_EQ(0, motionArgs.edgeFlags);
6923 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6924 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6926 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6929 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6931 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6932 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6933 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6934 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6935
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6938 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6939 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6940 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6942 ASSERT_EQ(0, motionArgs.flags);
6943 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6944 ASSERT_EQ(0, motionArgs.buttonState);
6945 ASSERT_EQ(0, motionArgs.edgeFlags);
6946 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6947 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6948 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6950 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6951 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6952 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6954
6955 // Move.
6956 x2 += 20; y2 -= 25;
6957 processPosition(mapper, x2, y2);
6958 processMTSync(mapper);
6959 processSync(mapper);
6960
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6963 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6964 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6965 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(0, motionArgs.flags);
6968 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6969 ASSERT_EQ(0, motionArgs.buttonState);
6970 ASSERT_EQ(0, motionArgs.edgeFlags);
6971 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6972 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6975 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6976 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6977 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6979
6980 // New finger down.
6981 int32_t x3 = 700, y3 = 300;
6982 processPosition(mapper, x2, y2);
6983 processMTSync(mapper);
6984 processPosition(mapper, x3, y3);
6985 processMTSync(mapper);
6986 processSync(mapper);
6987
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6990 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6991 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6992 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006993 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 ASSERT_EQ(0, motionArgs.flags);
6995 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6996 ASSERT_EQ(0, motionArgs.buttonState);
6997 ASSERT_EQ(0, motionArgs.edgeFlags);
6998 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6999 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7000 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7001 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7004 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7006 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7007 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7008 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7009 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7010
7011 // Second finger up.
7012 x3 += 30; y3 -= 20;
7013 processPosition(mapper, x3, y3);
7014 processMTSync(mapper);
7015 processSync(mapper);
7016
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7019 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7020 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7021 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007022 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023 ASSERT_EQ(0, motionArgs.flags);
7024 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7025 ASSERT_EQ(0, motionArgs.buttonState);
7026 ASSERT_EQ(0, motionArgs.edgeFlags);
7027 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7028 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7030 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7031 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7033 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7035 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7036 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7037 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7038 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7039
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7042 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7043 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7044 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7046 ASSERT_EQ(0, motionArgs.flags);
7047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7048 ASSERT_EQ(0, motionArgs.buttonState);
7049 ASSERT_EQ(0, motionArgs.edgeFlags);
7050 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7051 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7054 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7055 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7056 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7058
7059 // Last finger up.
7060 processMTSync(mapper);
7061 processSync(mapper);
7062
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7065 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7067 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7069 ASSERT_EQ(0, motionArgs.flags);
7070 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7071 ASSERT_EQ(0, motionArgs.buttonState);
7072 ASSERT_EQ(0, motionArgs.edgeFlags);
7073 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7074 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7077 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7081
7082 // Should not have sent any more keys or motions.
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7085}
7086
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007087TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7088 addConfigurationProperty("touch.deviceType", "touchScreen");
7089 prepareDisplay(DISPLAY_ORIENTATION_0);
7090
7091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7092 /*fuzz*/ 0, /*resolution*/ 10);
7093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7094 /*fuzz*/ 0, /*resolution*/ 11);
7095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7096 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7098 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7100 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7102 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7103
7104 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7105
7106 // X and Y axes
7107 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7108 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7109 // Touch major and minor
7110 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7111 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7112 // Tool major and minor
7113 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7114 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7115}
7116
7117TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7118 addConfigurationProperty("touch.deviceType", "touchScreen");
7119 prepareDisplay(DISPLAY_ORIENTATION_0);
7120
7121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7122 /*fuzz*/ 0, /*resolution*/ 10);
7123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7124 /*fuzz*/ 0, /*resolution*/ 11);
7125
7126 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7127
7128 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7129
7130 // Touch major and minor
7131 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7132 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7133 // Tool major and minor
7134 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7135 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7136}
7137
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 addConfigurationProperty("touch.deviceType", "touchScreen");
7140 prepareDisplay(DISPLAY_ORIENTATION_0);
7141 prepareAxes(POSITION | ID);
7142 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144
arthurhungdcef2dc2020-08-11 14:47:50 +08007145 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146
7147 NotifyMotionArgs motionArgs;
7148
7149 // Two fingers down at once.
7150 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7151 processPosition(mapper, x1, y1);
7152 processId(mapper, 1);
7153 processMTSync(mapper);
7154 processPosition(mapper, x2, y2);
7155 processId(mapper, 2);
7156 processMTSync(mapper);
7157 processSync(mapper);
7158
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7160 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7161 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7162 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7165 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7166
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007168 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7172 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7175 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7178
7179 // Move.
7180 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7181 processPosition(mapper, x1, y1);
7182 processId(mapper, 1);
7183 processMTSync(mapper);
7184 processPosition(mapper, x2, y2);
7185 processId(mapper, 2);
7186 processMTSync(mapper);
7187 processSync(mapper);
7188
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7191 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7192 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7194 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7197 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7198 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7199 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7200
7201 // First finger up.
7202 x2 += 15; y2 -= 20;
7203 processPosition(mapper, x2, y2);
7204 processId(mapper, 2);
7205 processMTSync(mapper);
7206 processSync(mapper);
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007209 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7213 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7218 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7223 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7226 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7227
7228 // Move.
7229 x2 += 20; y2 -= 25;
7230 processPosition(mapper, x2, y2);
7231 processId(mapper, 2);
7232 processMTSync(mapper);
7233 processSync(mapper);
7234
7235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7238 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7241 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7242
7243 // New finger down.
7244 int32_t x3 = 700, y3 = 300;
7245 processPosition(mapper, x2, y2);
7246 processId(mapper, 2);
7247 processMTSync(mapper);
7248 processPosition(mapper, x3, y3);
7249 processId(mapper, 3);
7250 processMTSync(mapper);
7251 processSync(mapper);
7252
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007254 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7258 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7263 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7264
7265 // Second finger up.
7266 x3 += 30; y3 -= 20;
7267 processPosition(mapper, x3, y3);
7268 processId(mapper, 3);
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007273 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007274 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
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7286 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7287 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7288 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7290 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7291
7292 // Last finger up.
7293 processMTSync(mapper);
7294 processSync(mapper);
7295
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7299 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7302 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7303
7304 // Should not have sent any more keys or motions.
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7307}
7308
7309TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 addConfigurationProperty("touch.deviceType", "touchScreen");
7311 prepareDisplay(DISPLAY_ORIENTATION_0);
7312 prepareAxes(POSITION | ID | SLOT);
7313 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315
arthurhungdcef2dc2020-08-11 14:47:50 +08007316 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007317
7318 NotifyMotionArgs motionArgs;
7319
7320 // Two fingers down at once.
7321 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7322 processPosition(mapper, x1, y1);
7323 processId(mapper, 1);
7324 processSlot(mapper, 1);
7325 processPosition(mapper, x2, y2);
7326 processId(mapper, 2);
7327 processSync(mapper);
7328
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7332 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7335 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7336
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007338 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7342 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7345 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7347 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7348
7349 // Move.
7350 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7351 processSlot(mapper, 0);
7352 processPosition(mapper, x1, y1);
7353 processSlot(mapper, 1);
7354 processPosition(mapper, x2, y2);
7355 processSync(mapper);
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7365 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7368
7369 // First finger up.
7370 x2 += 15; y2 -= 20;
7371 processSlot(mapper, 0);
7372 processId(mapper, -1);
7373 processSlot(mapper, 1);
7374 processPosition(mapper, x2, y2);
7375 processSync(mapper);
7376
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007378 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7380 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7382 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7386 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7387 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7388
7389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7392 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7395 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7396
7397 // Move.
7398 x2 += 20; y2 -= 25;
7399 processPosition(mapper, x2, y2);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7404 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7405 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7408 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7409
7410 // New finger down.
7411 int32_t x3 = 700, y3 = 300;
7412 processPosition(mapper, x2, y2);
7413 processSlot(mapper, 0);
7414 processId(mapper, 3);
7415 processPosition(mapper, x3, y3);
7416 processSync(mapper);
7417
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007419 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007420 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7423 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7426 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7428 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7429
7430 // Second finger up.
7431 x3 += 30; y3 -= 20;
7432 processSlot(mapper, 1);
7433 processId(mapper, -1);
7434 processSlot(mapper, 0);
7435 processPosition(mapper, x3, y3);
7436 processSync(mapper);
7437
7438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007439 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7443 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7446 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7448 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7449
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7452 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7453 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7456 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7457
7458 // Last finger up.
7459 processId(mapper, -1);
7460 processSync(mapper);
7461
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7464 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7465 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7468 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7469
7470 // Should not have sent any more keys or motions.
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7473}
7474
7475TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476 addConfigurationProperty("touch.deviceType", "touchScreen");
7477 prepareDisplay(DISPLAY_ORIENTATION_0);
7478 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007479 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007480
7481 // These calculations are based on the input device calibration documentation.
7482 int32_t rawX = 100;
7483 int32_t rawY = 200;
7484 int32_t rawTouchMajor = 7;
7485 int32_t rawTouchMinor = 6;
7486 int32_t rawToolMajor = 9;
7487 int32_t rawToolMinor = 8;
7488 int32_t rawPressure = 11;
7489 int32_t rawDistance = 0;
7490 int32_t rawOrientation = 3;
7491 int32_t id = 5;
7492
7493 float x = toDisplayX(rawX);
7494 float y = toDisplayY(rawY);
7495 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7496 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7497 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7498 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7499 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7500 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7501 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7502 float distance = float(rawDistance);
7503
7504 processPosition(mapper, rawX, rawY);
7505 processTouchMajor(mapper, rawTouchMajor);
7506 processTouchMinor(mapper, rawTouchMinor);
7507 processToolMajor(mapper, rawToolMajor);
7508 processToolMinor(mapper, rawToolMinor);
7509 processPressure(mapper, rawPressure);
7510 processOrientation(mapper, rawOrientation);
7511 processDistance(mapper, rawDistance);
7512 processId(mapper, id);
7513 processMTSync(mapper);
7514 processSync(mapper);
7515
7516 NotifyMotionArgs args;
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7518 ASSERT_EQ(0, args.pointerProperties[0].id);
7519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7520 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7521 orientation, distance));
7522}
7523
7524TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 addConfigurationProperty("touch.deviceType", "touchScreen");
7526 prepareDisplay(DISPLAY_ORIENTATION_0);
7527 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7528 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007529 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530
7531 // These calculations are based on the input device calibration documentation.
7532 int32_t rawX = 100;
7533 int32_t rawY = 200;
7534 int32_t rawTouchMajor = 140;
7535 int32_t rawTouchMinor = 120;
7536 int32_t rawToolMajor = 180;
7537 int32_t rawToolMinor = 160;
7538
7539 float x = toDisplayX(rawX);
7540 float y = toDisplayY(rawY);
7541 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7542 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7543 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7544 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7545 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7546
7547 processPosition(mapper, rawX, rawY);
7548 processTouchMajor(mapper, rawTouchMajor);
7549 processTouchMinor(mapper, rawTouchMinor);
7550 processToolMajor(mapper, rawToolMajor);
7551 processToolMinor(mapper, rawToolMinor);
7552 processMTSync(mapper);
7553 processSync(mapper);
7554
7555 NotifyMotionArgs args;
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7558 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7559}
7560
7561TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 addConfigurationProperty("touch.deviceType", "touchScreen");
7563 prepareDisplay(DISPLAY_ORIENTATION_0);
7564 prepareAxes(POSITION | TOUCH | TOOL);
7565 addConfigurationProperty("touch.size.calibration", "diameter");
7566 addConfigurationProperty("touch.size.scale", "10");
7567 addConfigurationProperty("touch.size.bias", "160");
7568 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007569 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570
7571 // These calculations are based on the input device calibration documentation.
7572 // Note: We only provide a single common touch/tool value because the device is assumed
7573 // not to emit separate values for each pointer (isSummed = 1).
7574 int32_t rawX = 100;
7575 int32_t rawY = 200;
7576 int32_t rawX2 = 150;
7577 int32_t rawY2 = 250;
7578 int32_t rawTouchMajor = 5;
7579 int32_t rawToolMajor = 8;
7580
7581 float x = toDisplayX(rawX);
7582 float y = toDisplayY(rawY);
7583 float x2 = toDisplayX(rawX2);
7584 float y2 = toDisplayY(rawY2);
7585 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7586 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7587 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7588
7589 processPosition(mapper, rawX, rawY);
7590 processTouchMajor(mapper, rawTouchMajor);
7591 processToolMajor(mapper, rawToolMajor);
7592 processMTSync(mapper);
7593 processPosition(mapper, rawX2, rawY2);
7594 processTouchMajor(mapper, rawTouchMajor);
7595 processToolMajor(mapper, rawToolMajor);
7596 processMTSync(mapper);
7597 processSync(mapper);
7598
7599 NotifyMotionArgs args;
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7601 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7602
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007604 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 ASSERT_EQ(size_t(2), args.pointerCount);
7606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7607 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7609 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7610}
7611
7612TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 addConfigurationProperty("touch.deviceType", "touchScreen");
7614 prepareDisplay(DISPLAY_ORIENTATION_0);
7615 prepareAxes(POSITION | TOUCH | TOOL);
7616 addConfigurationProperty("touch.size.calibration", "area");
7617 addConfigurationProperty("touch.size.scale", "43");
7618 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007620
7621 // These calculations are based on the input device calibration documentation.
7622 int32_t rawX = 100;
7623 int32_t rawY = 200;
7624 int32_t rawTouchMajor = 5;
7625 int32_t rawToolMajor = 8;
7626
7627 float x = toDisplayX(rawX);
7628 float y = toDisplayY(rawY);
7629 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7630 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7631 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7632
7633 processPosition(mapper, rawX, rawY);
7634 processTouchMajor(mapper, rawTouchMajor);
7635 processToolMajor(mapper, rawToolMajor);
7636 processMTSync(mapper);
7637 processSync(mapper);
7638
7639 NotifyMotionArgs args;
7640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7642 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7643}
7644
7645TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 addConfigurationProperty("touch.deviceType", "touchScreen");
7647 prepareDisplay(DISPLAY_ORIENTATION_0);
7648 prepareAxes(POSITION | PRESSURE);
7649 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7650 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007651 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652
Michael Wrightaa449c92017-12-13 21:21:43 +00007653 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007654 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007655 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7656 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7657 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7658
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 // These calculations are based on the input device calibration documentation.
7660 int32_t rawX = 100;
7661 int32_t rawY = 200;
7662 int32_t rawPressure = 60;
7663
7664 float x = toDisplayX(rawX);
7665 float y = toDisplayY(rawY);
7666 float pressure = float(rawPressure) * 0.01f;
7667
7668 processPosition(mapper, rawX, rawY);
7669 processPressure(mapper, rawPressure);
7670 processMTSync(mapper);
7671 processSync(mapper);
7672
7673 NotifyMotionArgs args;
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7676 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7677}
7678
7679TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 addConfigurationProperty("touch.deviceType", "touchScreen");
7681 prepareDisplay(DISPLAY_ORIENTATION_0);
7682 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007683 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684
7685 NotifyMotionArgs motionArgs;
7686 NotifyKeyArgs keyArgs;
7687
7688 processId(mapper, 1);
7689 processPosition(mapper, 100, 200);
7690 processSync(mapper);
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7693 ASSERT_EQ(0, motionArgs.buttonState);
7694
7695 // press BTN_LEFT, release BTN_LEFT
7696 processKey(mapper, BTN_LEFT, 1);
7697 processSync(mapper);
7698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7700 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7701
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7704 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7705
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706 processKey(mapper, BTN_LEFT, 0);
7707 processSync(mapper);
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007711
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007714 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715
7716 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7717 processKey(mapper, BTN_RIGHT, 1);
7718 processKey(mapper, BTN_MIDDLE, 1);
7719 processSync(mapper);
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7722 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7723 motionArgs.buttonState);
7724
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7727 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7731 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7732 motionArgs.buttonState);
7733
Michael Wrightd02c5b62014-02-10 15:10:22 -08007734 processKey(mapper, BTN_RIGHT, 0);
7735 processSync(mapper);
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007739
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007742 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743
7744 processKey(mapper, BTN_MIDDLE, 0);
7745 processSync(mapper);
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007752 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753
7754 // press BTN_BACK, release BTN_BACK
7755 processKey(mapper, BTN_BACK, 1);
7756 processSync(mapper);
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7758 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7759 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007760
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007763 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7764
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7766 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7767 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768
7769 processKey(mapper, BTN_BACK, 0);
7770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007772 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007774
7775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007777 ASSERT_EQ(0, motionArgs.buttonState);
7778
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7780 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7781 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7782
7783 // press BTN_SIDE, release BTN_SIDE
7784 processKey(mapper, BTN_SIDE, 1);
7785 processSync(mapper);
7786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7787 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7788 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007789
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007792 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7793
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7795 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7796 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797
7798 processKey(mapper, BTN_SIDE, 0);
7799 processSync(mapper);
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007803
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007806 ASSERT_EQ(0, motionArgs.buttonState);
7807
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7809 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7810 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7811
7812 // press BTN_FORWARD, release BTN_FORWARD
7813 processKey(mapper, BTN_FORWARD, 1);
7814 processSync(mapper);
7815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7816 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7817 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007818
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007821 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7825 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826
7827 processKey(mapper, BTN_FORWARD, 0);
7828 processSync(mapper);
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007830 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007835 ASSERT_EQ(0, motionArgs.buttonState);
7836
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7838 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7839 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7840
7841 // press BTN_EXTRA, release BTN_EXTRA
7842 processKey(mapper, BTN_EXTRA, 1);
7843 processSync(mapper);
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7845 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7846 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007847
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007850 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7854 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855
7856 processKey(mapper, BTN_EXTRA, 0);
7857 processSync(mapper);
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007861
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007864 ASSERT_EQ(0, motionArgs.buttonState);
7865
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7867 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7868 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7869
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7871
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872 // press BTN_STYLUS, release BTN_STYLUS
7873 processKey(mapper, BTN_STYLUS, 1);
7874 processSync(mapper);
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007877 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7878
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7881 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007882
7883 processKey(mapper, BTN_STYLUS, 0);
7884 processSync(mapper);
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007888
7889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007891 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892
7893 // press BTN_STYLUS2, release BTN_STYLUS2
7894 processKey(mapper, BTN_STYLUS2, 1);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007898 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7899
7900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7902 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007903
7904 processKey(mapper, BTN_STYLUS2, 0);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007912 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913
7914 // release touch
7915 processId(mapper, -1);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7919 ASSERT_EQ(0, motionArgs.buttonState);
7920}
7921
7922TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 addConfigurationProperty("touch.deviceType", "touchScreen");
7924 prepareDisplay(DISPLAY_ORIENTATION_0);
7925 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927
7928 NotifyMotionArgs motionArgs;
7929
7930 // default tool type is finger
7931 processId(mapper, 1);
7932 processPosition(mapper, 100, 200);
7933 processSync(mapper);
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7937
7938 // eraser
7939 processKey(mapper, BTN_TOOL_RUBBER, 1);
7940 processSync(mapper);
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7944
7945 // stylus
7946 processKey(mapper, BTN_TOOL_RUBBER, 0);
7947 processKey(mapper, BTN_TOOL_PEN, 1);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7952
7953 // brush
7954 processKey(mapper, BTN_TOOL_PEN, 0);
7955 processKey(mapper, BTN_TOOL_BRUSH, 1);
7956 processSync(mapper);
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7960
7961 // pencil
7962 processKey(mapper, BTN_TOOL_BRUSH, 0);
7963 processKey(mapper, BTN_TOOL_PENCIL, 1);
7964 processSync(mapper);
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7968
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007969 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 processKey(mapper, BTN_TOOL_PENCIL, 0);
7971 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7976
7977 // mouse
7978 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7979 processKey(mapper, BTN_TOOL_MOUSE, 1);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7984
7985 // lens
7986 processKey(mapper, BTN_TOOL_MOUSE, 0);
7987 processKey(mapper, BTN_TOOL_LENS, 1);
7988 processSync(mapper);
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7992
7993 // double-tap
7994 processKey(mapper, BTN_TOOL_LENS, 0);
7995 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7996 processSync(mapper);
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8000
8001 // triple-tap
8002 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8003 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8008
8009 // quad-tap
8010 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8011 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8012 processSync(mapper);
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8016
8017 // finger
8018 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8019 processKey(mapper, BTN_TOOL_FINGER, 1);
8020 processSync(mapper);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8024
8025 // stylus trumps finger
8026 processKey(mapper, BTN_TOOL_PEN, 1);
8027 processSync(mapper);
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8031
8032 // eraser trumps stylus
8033 processKey(mapper, BTN_TOOL_RUBBER, 1);
8034 processSync(mapper);
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8038
8039 // mouse trumps eraser
8040 processKey(mapper, BTN_TOOL_MOUSE, 1);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8044 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8045
8046 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8047 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8048 processSync(mapper);
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8052
8053 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8054 processToolType(mapper, MT_TOOL_PEN);
8055 processSync(mapper);
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8059
8060 // back to default tool type
8061 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8062 processKey(mapper, BTN_TOOL_MOUSE, 0);
8063 processKey(mapper, BTN_TOOL_RUBBER, 0);
8064 processKey(mapper, BTN_TOOL_PEN, 0);
8065 processKey(mapper, BTN_TOOL_FINGER, 0);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8070}
8071
8072TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 addConfigurationProperty("touch.deviceType", "touchScreen");
8074 prepareDisplay(DISPLAY_ORIENTATION_0);
8075 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008076 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008077 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008078
8079 NotifyMotionArgs motionArgs;
8080
8081 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8082 processId(mapper, 1);
8083 processPosition(mapper, 100, 200);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8088 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8089
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8094
8095 // move a little
8096 processPosition(mapper, 150, 250);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8101 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8102
8103 // down when BTN_TOUCH is pressed, pressure defaults to 1
8104 processKey(mapper, BTN_TOUCH, 1);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8109 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8114 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8115
8116 // up when BTN_TOUCH is released, hover restored
8117 processKey(mapper, BTN_TOUCH, 0);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8122 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8127 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8128
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8132 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8133
8134 // exit hover when pointer goes away
8135 processId(mapper, -1);
8136 processSync(mapper);
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8140 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8141}
8142
8143TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 addConfigurationProperty("touch.deviceType", "touchScreen");
8145 prepareDisplay(DISPLAY_ORIENTATION_0);
8146 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008147 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148
8149 NotifyMotionArgs motionArgs;
8150
8151 // initially hovering because pressure is 0
8152 processId(mapper, 1);
8153 processPosition(mapper, 100, 200);
8154 processPressure(mapper, 0);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8157 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8159 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8160
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8164 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8165
8166 // move a little
8167 processPosition(mapper, 150, 250);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8172 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8173
8174 // down when pressure becomes non-zero
8175 processPressure(mapper, RAW_PRESSURE_MAX);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8180 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8185 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8186
8187 // up when pressure becomes 0, hover restored
8188 processPressure(mapper, 0);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8193 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8194
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8198 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8203 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8204
8205 // exit hover when pointer goes away
8206 processId(mapper, -1);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8212}
8213
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008214/**
8215 * Set the input device port <--> display port associations, and check that the
8216 * events are routed to the display that matches the display port.
8217 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8218 */
8219TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008220 const std::string usb2 = "USB2";
8221 const uint8_t hdmi1 = 0;
8222 const uint8_t hdmi2 = 1;
8223 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008224 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008225
8226 addConfigurationProperty("touch.deviceType", "touchScreen");
8227 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008228 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008229
8230 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8231 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8232
8233 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8234 // for this input device is specified, and the matching viewport is not present,
8235 // the input device should be disabled (at the mapper level).
8236
8237 // Add viewport for display 2 on hdmi2
8238 prepareSecondaryDisplay(type, hdmi2);
8239 // Send a touch event
8240 processPosition(mapper, 100, 100);
8241 processSync(mapper);
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8243
8244 // Add viewport for display 1 on hdmi1
8245 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8246 // Send a touch event again
8247 processPosition(mapper, 100, 100);
8248 processSync(mapper);
8249
8250 NotifyMotionArgs args;
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8252 ASSERT_EQ(DISPLAY_ID, args.displayId);
8253}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008255TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8256 addConfigurationProperty("touch.deviceType", "touchScreen");
8257 prepareAxes(POSITION);
8258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8259
8260 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8261
8262 prepareDisplay(DISPLAY_ORIENTATION_0);
8263 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8264
8265 // Send a touch event
8266 processPosition(mapper, 100, 100);
8267 processSync(mapper);
8268
8269 NotifyMotionArgs args;
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8271 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8272}
8273
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008274TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008275 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008276 std::shared_ptr<FakePointerController> fakePointerController =
8277 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008278 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008279 fakePointerController->setPosition(100, 200);
8280 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008281 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008282
Garfield Tan888a6a42020-01-09 11:39:16 -08008283 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008284 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008285
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008286 prepareDisplay(DISPLAY_ORIENTATION_0);
8287 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008288 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008289
8290 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008291 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008292
8293 NotifyMotionArgs motionArgs;
8294 processPosition(mapper, 100, 100);
8295 processSync(mapper);
8296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8299 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8300}
8301
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008302/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008303 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8304 */
8305TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8306 addConfigurationProperty("touch.deviceType", "touchScreen");
8307 prepareAxes(POSITION);
8308 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8309
8310 prepareDisplay(DISPLAY_ORIENTATION_0);
8311 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8312 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8313 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8314 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8315
8316 NotifyMotionArgs args;
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8318 ASSERT_EQ(26, args.readTime);
8319
8320 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8321 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8322 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8323
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8325 ASSERT_EQ(33, args.readTime);
8326}
8327
8328/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008329 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8330 * events should not be delivered to the listener.
8331 */
8332TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8333 addConfigurationProperty("touch.deviceType", "touchScreen");
8334 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8335 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8336 ViewportType::INTERNAL);
8337 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8338 prepareAxes(POSITION);
8339 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8340
8341 NotifyMotionArgs motionArgs;
8342 processPosition(mapper, 100, 100);
8343 processSync(mapper);
8344
8345 mFakeListener->assertNotifyMotionWasNotCalled();
8346}
8347
Garfield Tanc734e4f2021-01-15 20:01:39 -08008348TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8349 addConfigurationProperty("touch.deviceType", "touchScreen");
8350 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8351 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8352 ViewportType::INTERNAL);
8353 std::optional<DisplayViewport> optionalDisplayViewport =
8354 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8355 ASSERT_TRUE(optionalDisplayViewport.has_value());
8356 DisplayViewport displayViewport = *optionalDisplayViewport;
8357
8358 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8359 prepareAxes(POSITION);
8360 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8361
8362 // Finger down
8363 int32_t x = 100, y = 100;
8364 processPosition(mapper, x, y);
8365 processSync(mapper);
8366
8367 NotifyMotionArgs motionArgs;
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8370
8371 // Deactivate display viewport
8372 displayViewport.isActive = false;
8373 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8374 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8375
8376 // Finger move
8377 x += 10, y += 10;
8378 processPosition(mapper, x, y);
8379 processSync(mapper);
8380
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8383
8384 // Reactivate display viewport
8385 displayViewport.isActive = true;
8386 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8387 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8388
8389 // Finger move again
8390 x += 10, y += 10;
8391 processPosition(mapper, x, y);
8392 processSync(mapper);
8393
8394 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8395 // no pointer on the touch device.
8396 mFakeListener->assertNotifyMotionWasNotCalled();
8397}
8398
Arthur Hung7c645402019-01-25 17:45:42 +08008399TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8400 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008401 prepareAxes(POSITION | ID | SLOT);
8402 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008403 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008404
8405 // Create the second touch screen device, and enable multi fingers.
8406 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008407 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008408 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008409 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008410 std::shared_ptr<InputDevice> device2 =
8411 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8412 Flags<InputDeviceClass>(0));
8413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008414 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8415 0 /*flat*/, 0 /*fuzz*/);
8416 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8417 0 /*flat*/, 0 /*fuzz*/);
8418 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8419 0 /*flat*/, 0 /*fuzz*/);
8420 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8421 0 /*flat*/, 0 /*fuzz*/);
8422 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8423 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8424 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008425
8426 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008427 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008428 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8429 device2->reset(ARBITRARY_TIME);
8430
8431 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008432 std::shared_ptr<FakePointerController> fakePointerController =
8433 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008434 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008435
8436 // Setup policy for associated displays and show touches.
8437 const uint8_t hdmi1 = 0;
8438 const uint8_t hdmi2 = 1;
8439 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8440 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8441 mFakePolicy->setShowTouches(true);
8442
8443 // Create displays.
8444 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008445 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008446
8447 // Default device will reconfigure above, need additional reconfiguration for another device.
8448 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008450
8451 // Two fingers down at default display.
8452 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8453 processPosition(mapper, x1, y1);
8454 processId(mapper, 1);
8455 processSlot(mapper, 1);
8456 processPosition(mapper, x2, y2);
8457 processId(mapper, 2);
8458 processSync(mapper);
8459
8460 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8461 fakePointerController->getSpots().find(DISPLAY_ID);
8462 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8463 ASSERT_EQ(size_t(2), iter->second.size());
8464
8465 // Two fingers down at second display.
8466 processPosition(mapper2, x1, y1);
8467 processId(mapper2, 1);
8468 processSlot(mapper2, 1);
8469 processPosition(mapper2, x2, y2);
8470 processId(mapper2, 2);
8471 processSync(mapper2);
8472
8473 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8474 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8475 ASSERT_EQ(size_t(2), iter->second.size());
8476}
8477
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008478TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008479 prepareAxes(POSITION);
8480 addConfigurationProperty("touch.deviceType", "touchScreen");
8481 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008483
8484 NotifyMotionArgs motionArgs;
8485 // Unrotated video frame
8486 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8487 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008488 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008489 processPosition(mapper, 100, 200);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(frames, motionArgs.videoFrames);
8493
8494 // Subsequent touch events should not have any videoframes
8495 // This is implemented separately in FakeEventHub,
8496 // but that should match the behaviour of TouchVideoDevice.
8497 processPosition(mapper, 200, 200);
8498 processSync(mapper);
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8501}
8502
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008503TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008504 prepareAxes(POSITION);
8505 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008507 // Unrotated video frame
8508 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8509 NotifyMotionArgs motionArgs;
8510
8511 // Test all 4 orientations
8512 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008513 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8514 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8515 clearViewports();
8516 prepareDisplay(orientation);
8517 std::vector<TouchVideoFrame> frames{frame};
8518 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8519 processPosition(mapper, 100, 200);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(frames, motionArgs.videoFrames);
8523 }
8524}
8525
8526TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8527 prepareAxes(POSITION);
8528 addConfigurationProperty("touch.deviceType", "touchScreen");
8529 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8530 // orientation-aware are affected by display rotation.
8531 addConfigurationProperty("touch.orientationAware", "0");
8532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8533 // Unrotated video frame
8534 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8535 NotifyMotionArgs motionArgs;
8536
8537 // Test all 4 orientations
8538 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008539 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8540 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8541 clearViewports();
8542 prepareDisplay(orientation);
8543 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008544 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008545 processPosition(mapper, 100, 200);
8546 processSync(mapper);
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008548 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8549 // compared to the display. This is so that when the window transform (which contains the
8550 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8551 // window's coordinate space.
8552 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008553 ASSERT_EQ(frames, motionArgs.videoFrames);
8554 }
8555}
8556
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008557TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008558 prepareAxes(POSITION);
8559 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008561 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8562 // so mix these.
8563 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8564 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8565 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8566 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8567 NotifyMotionArgs motionArgs;
8568
8569 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008570 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008571 processPosition(mapper, 100, 200);
8572 processSync(mapper);
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008574 ASSERT_EQ(frames, motionArgs.videoFrames);
8575}
8576
8577TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8578 prepareAxes(POSITION);
8579 addConfigurationProperty("touch.deviceType", "touchScreen");
8580 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8581 // orientation-aware are affected by display rotation.
8582 addConfigurationProperty("touch.orientationAware", "0");
8583 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8584 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8585 // so mix these.
8586 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8587 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8588 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8589 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8590 NotifyMotionArgs motionArgs;
8591
8592 prepareDisplay(DISPLAY_ORIENTATION_90);
8593 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8594 processPosition(mapper, 100, 200);
8595 processSync(mapper);
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8598 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8599 // compared to the display. This is so that when the window transform (which contains the
8600 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8601 // window's coordinate space.
8602 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8603 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008604 ASSERT_EQ(frames, motionArgs.videoFrames);
8605}
8606
Arthur Hung9da14732019-09-02 16:16:58 +08008607/**
8608 * If we had defined port associations, but the viewport is not ready, the touch device would be
8609 * expected to be disabled, and it should be enabled after the viewport has found.
8610 */
8611TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008612 constexpr uint8_t hdmi2 = 1;
8613 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008614 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008615
8616 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8617
8618 addConfigurationProperty("touch.deviceType", "touchScreen");
8619 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008620 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008621
8622 ASSERT_EQ(mDevice->isEnabled(), false);
8623
8624 // Add display on hdmi2, the device should be enabled and can receive touch event.
8625 prepareSecondaryDisplay(type, hdmi2);
8626 ASSERT_EQ(mDevice->isEnabled(), true);
8627
8628 // Send a touch event.
8629 processPosition(mapper, 100, 100);
8630 processSync(mapper);
8631
8632 NotifyMotionArgs args;
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8634 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8635}
8636
Arthur Hung421eb1c2020-01-16 00:09:42 +08008637TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008638 addConfigurationProperty("touch.deviceType", "touchScreen");
8639 prepareDisplay(DISPLAY_ORIENTATION_0);
8640 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008641 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008642
8643 NotifyMotionArgs motionArgs;
8644
8645 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8646 // finger down
8647 processId(mapper, 1);
8648 processPosition(mapper, x1, y1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8653
8654 // finger move
8655 processId(mapper, 1);
8656 processPosition(mapper, x2, y2);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8661
8662 // finger up.
8663 processId(mapper, -1);
8664 processSync(mapper);
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8668
8669 // new finger down
8670 processId(mapper, 1);
8671 processPosition(mapper, x3, y3);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8676}
8677
8678/**
arthurhungcc7f9802020-04-30 17:55:40 +08008679 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8680 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008681 */
arthurhungcc7f9802020-04-30 17:55:40 +08008682TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008683 addConfigurationProperty("touch.deviceType", "touchScreen");
8684 prepareDisplay(DISPLAY_ORIENTATION_0);
8685 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008687
8688 NotifyMotionArgs motionArgs;
8689
8690 // default tool type is finger
8691 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008692 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008693 processPosition(mapper, x1, y1);
8694 processSync(mapper);
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8698
8699 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8700 processToolType(mapper, MT_TOOL_PALM);
8701 processSync(mapper);
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8704
8705 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008706 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008707 processPosition(mapper, x2, y2);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8710
8711 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008712 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8715
8716 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008717 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008718 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008719 processPosition(mapper, x3, y3);
8720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8722 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8724}
8725
arthurhungbf89a482020-04-17 17:37:55 +08008726/**
arthurhungcc7f9802020-04-30 17:55:40 +08008727 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8728 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008729 */
arthurhungcc7f9802020-04-30 17:55:40 +08008730TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008731 addConfigurationProperty("touch.deviceType", "touchScreen");
8732 prepareDisplay(DISPLAY_ORIENTATION_0);
8733 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8734 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8735
8736 NotifyMotionArgs motionArgs;
8737
8738 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008739 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8740 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008741 processPosition(mapper, x1, y1);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8746
8747 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008748 processSlot(mapper, SECOND_SLOT);
8749 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008750 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008751 processSync(mapper);
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008753 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8755
8756 // If the tool type of the first finger changes to MT_TOOL_PALM,
8757 // we expect to receive ACTION_POINTER_UP with cancel flag.
8758 processSlot(mapper, FIRST_SLOT);
8759 processId(mapper, FIRST_TRACKING_ID);
8760 processToolType(mapper, MT_TOOL_PALM);
8761 processSync(mapper);
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008763 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008764 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8765
8766 // The following MOVE events of second finger should be processed.
8767 processSlot(mapper, SECOND_SLOT);
8768 processId(mapper, SECOND_TRACKING_ID);
8769 processPosition(mapper, x2 + 1, y2 + 1);
8770 processSync(mapper);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8773 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8774
8775 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8776 // it. Second finger receive move.
8777 processSlot(mapper, FIRST_SLOT);
8778 processId(mapper, INVALID_TRACKING_ID);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8782 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8783
8784 // Second finger keeps moving.
8785 processSlot(mapper, SECOND_SLOT);
8786 processId(mapper, SECOND_TRACKING_ID);
8787 processPosition(mapper, x2 + 2, y2 + 2);
8788 processSync(mapper);
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8791 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8792
8793 // Second finger up.
8794 processId(mapper, INVALID_TRACKING_ID);
8795 processSync(mapper);
8796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8798 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8799}
8800
8801/**
8802 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8803 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8804 */
8805TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8806 addConfigurationProperty("touch.deviceType", "touchScreen");
8807 prepareDisplay(DISPLAY_ORIENTATION_0);
8808 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8810
8811 NotifyMotionArgs motionArgs;
8812
8813 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8814 // First finger down.
8815 processId(mapper, FIRST_TRACKING_ID);
8816 processPosition(mapper, x1, y1);
8817 processSync(mapper);
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8821
8822 // Second finger down.
8823 processSlot(mapper, SECOND_SLOT);
8824 processId(mapper, SECOND_TRACKING_ID);
8825 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008828 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8830
arthurhungcc7f9802020-04-30 17:55:40 +08008831 // If the tool type of the first finger changes to MT_TOOL_PALM,
8832 // we expect to receive ACTION_POINTER_UP with cancel flag.
8833 processSlot(mapper, FIRST_SLOT);
8834 processId(mapper, FIRST_TRACKING_ID);
8835 processToolType(mapper, MT_TOOL_PALM);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008838 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008839 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8840
8841 // Second finger keeps moving.
8842 processSlot(mapper, SECOND_SLOT);
8843 processId(mapper, SECOND_TRACKING_ID);
8844 processPosition(mapper, x2 + 1, y2 + 1);
8845 processSync(mapper);
8846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8848
8849 // second finger becomes palm, receive cancel due to only 1 finger is active.
8850 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008851 processToolType(mapper, MT_TOOL_PALM);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8854 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8855
arthurhungcc7f9802020-04-30 17:55:40 +08008856 // third finger down.
8857 processSlot(mapper, THIRD_SLOT);
8858 processId(mapper, THIRD_TRACKING_ID);
8859 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008860 processPosition(mapper, x3, y3);
8861 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008865 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8866
8867 // third finger move
8868 processId(mapper, THIRD_TRACKING_ID);
8869 processPosition(mapper, x3 + 1, y3 + 1);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8873
8874 // first finger up, third finger receive move.
8875 processSlot(mapper, FIRST_SLOT);
8876 processId(mapper, INVALID_TRACKING_ID);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8880 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8881
8882 // second finger up, third finger receive move.
8883 processSlot(mapper, SECOND_SLOT);
8884 processId(mapper, INVALID_TRACKING_ID);
8885 processSync(mapper);
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8888 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8889
8890 // third finger up.
8891 processSlot(mapper, THIRD_SLOT);
8892 processId(mapper, INVALID_TRACKING_ID);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8896 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8897}
8898
8899/**
8900 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8901 * and the active finger could still be allowed to receive the events
8902 */
8903TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8904 addConfigurationProperty("touch.deviceType", "touchScreen");
8905 prepareDisplay(DISPLAY_ORIENTATION_0);
8906 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8908
8909 NotifyMotionArgs motionArgs;
8910
8911 // default tool type is finger
8912 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8913 processId(mapper, FIRST_TRACKING_ID);
8914 processPosition(mapper, x1, y1);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8919
8920 // Second finger down.
8921 processSlot(mapper, SECOND_SLOT);
8922 processId(mapper, SECOND_TRACKING_ID);
8923 processPosition(mapper, x2, y2);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008926 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8928
8929 // If the tool type of the second finger changes to MT_TOOL_PALM,
8930 // we expect to receive ACTION_POINTER_UP with cancel flag.
8931 processId(mapper, SECOND_TRACKING_ID);
8932 processToolType(mapper, MT_TOOL_PALM);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008935 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008936 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8937
8938 // The following MOVE event should be processed.
8939 processSlot(mapper, FIRST_SLOT);
8940 processId(mapper, FIRST_TRACKING_ID);
8941 processPosition(mapper, x1 + 1, y1 + 1);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8945 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8946
8947 // second finger up.
8948 processSlot(mapper, SECOND_SLOT);
8949 processId(mapper, INVALID_TRACKING_ID);
8950 processSync(mapper);
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8953
8954 // first finger keep moving
8955 processSlot(mapper, FIRST_SLOT);
8956 processId(mapper, FIRST_TRACKING_ID);
8957 processPosition(mapper, x1 + 2, y1 + 2);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8961
8962 // first finger up.
8963 processId(mapper, INVALID_TRACKING_ID);
8964 processSync(mapper);
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8967 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008968}
8969
Arthur Hung9ad18942021-06-19 02:04:46 +00008970/**
8971 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8972 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8973 * cause slot be valid again.
8974 */
8975TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8976 addConfigurationProperty("touch.deviceType", "touchScreen");
8977 prepareDisplay(DISPLAY_ORIENTATION_0);
8978 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8980
8981 NotifyMotionArgs motionArgs;
8982
8983 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8984 // First finger down.
8985 processId(mapper, FIRST_TRACKING_ID);
8986 processPosition(mapper, x1, y1);
8987 processPressure(mapper, RAW_PRESSURE_MAX);
8988 processSync(mapper);
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8991 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8992
8993 // First finger move.
8994 processId(mapper, FIRST_TRACKING_ID);
8995 processPosition(mapper, x1 + 1, y1 + 1);
8996 processPressure(mapper, RAW_PRESSURE_MAX);
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9000 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9001
9002 // Second finger down.
9003 processSlot(mapper, SECOND_SLOT);
9004 processId(mapper, SECOND_TRACKING_ID);
9005 processPosition(mapper, x2, y2);
9006 processPressure(mapper, RAW_PRESSURE_MAX);
9007 processSync(mapper);
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009009 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009010 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9011
9012 // second finger up with some unexpected data.
9013 processSlot(mapper, SECOND_SLOT);
9014 processId(mapper, INVALID_TRACKING_ID);
9015 processPosition(mapper, x2, y2);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009018 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009019 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9020
9021 // first finger up with some unexpected data.
9022 processSlot(mapper, FIRST_SLOT);
9023 processId(mapper, INVALID_TRACKING_ID);
9024 processPosition(mapper, x2, y2);
9025 processPressure(mapper, RAW_PRESSURE_MAX);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9029 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9030}
9031
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009032// --- MultiTouchInputMapperTest_ExternalDevice ---
9033
9034class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9035protected:
Chris Yea52ade12020-08-27 16:49:20 -07009036 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009037};
9038
9039/**
9040 * Expect fallback to internal viewport if device is external and external viewport is not present.
9041 */
9042TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9043 prepareAxes(POSITION);
9044 addConfigurationProperty("touch.deviceType", "touchScreen");
9045 prepareDisplay(DISPLAY_ORIENTATION_0);
9046 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9047
9048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9049
9050 NotifyMotionArgs motionArgs;
9051
9052 // Expect the event to be sent to the internal viewport,
9053 // because an external viewport is not present.
9054 processPosition(mapper, 100, 100);
9055 processSync(mapper);
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9057 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9058
9059 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009060 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009061 processPosition(mapper, 100, 100);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9065}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009066
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009067TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9068 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9069 std::shared_ptr<FakePointerController> fakePointerController =
9070 std::make_shared<FakePointerController>();
9071 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9072 fakePointerController->setPosition(0, 0);
9073 fakePointerController->setButtonState(0);
9074
9075 // prepare device and capture
9076 prepareDisplay(DISPLAY_ORIENTATION_0);
9077 prepareAxes(POSITION | ID | SLOT);
9078 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9079 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9080 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009081 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9083
9084 // captured touchpad should be a touchpad source
9085 NotifyDeviceResetArgs resetArgs;
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9087 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9088
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009089 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009090
9091 const InputDeviceInfo::MotionRange* relRangeX =
9092 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9093 ASSERT_NE(relRangeX, nullptr);
9094 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9095 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9096 const InputDeviceInfo::MotionRange* relRangeY =
9097 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9098 ASSERT_NE(relRangeY, nullptr);
9099 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9100 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9101
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009102 // run captured pointer tests - note that this is unscaled, so input listener events should be
9103 // identical to what the hardware sends (accounting for any
9104 // calibration).
9105 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009106 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009107 processId(mapper, 1);
9108 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9109 processKey(mapper, BTN_TOUCH, 1);
9110 processSync(mapper);
9111
9112 // expect coord[0] to contain initial location of touch 0
9113 NotifyMotionArgs args;
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9116 ASSERT_EQ(1U, args.pointerCount);
9117 ASSERT_EQ(0, args.pointerProperties[0].id);
9118 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9119 ASSERT_NO_FATAL_FAILURE(
9120 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9121
9122 // FINGER 1 DOWN
9123 processSlot(mapper, 1);
9124 processId(mapper, 2);
9125 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9126 processSync(mapper);
9127
9128 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009130 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009131 ASSERT_EQ(2U, args.pointerCount);
9132 ASSERT_EQ(0, args.pointerProperties[0].id);
9133 ASSERT_EQ(1, args.pointerProperties[1].id);
9134 ASSERT_NO_FATAL_FAILURE(
9135 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9136 ASSERT_NO_FATAL_FAILURE(
9137 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9138
9139 // FINGER 1 MOVE
9140 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9141 processSync(mapper);
9142
9143 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9144 // from move
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9147 ASSERT_NO_FATAL_FAILURE(
9148 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9149 ASSERT_NO_FATAL_FAILURE(
9150 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9151
9152 // FINGER 0 MOVE
9153 processSlot(mapper, 0);
9154 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9155 processSync(mapper);
9156
9157 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9160 ASSERT_NO_FATAL_FAILURE(
9161 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9162 ASSERT_NO_FATAL_FAILURE(
9163 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9164
9165 // BUTTON DOWN
9166 processKey(mapper, BTN_LEFT, 1);
9167 processSync(mapper);
9168
9169 // touchinputmapper design sends a move before button press
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9174
9175 // BUTTON UP
9176 processKey(mapper, BTN_LEFT, 0);
9177 processSync(mapper);
9178
9179 // touchinputmapper design sends a move after button release
9180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9184
9185 // FINGER 0 UP
9186 processId(mapper, -1);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9190
9191 // FINGER 1 MOVE
9192 processSlot(mapper, 1);
9193 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9194 processSync(mapper);
9195
9196 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9199 ASSERT_EQ(1U, args.pointerCount);
9200 ASSERT_EQ(1, args.pointerProperties[0].id);
9201 ASSERT_NO_FATAL_FAILURE(
9202 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9203
9204 // FINGER 1 UP
9205 processId(mapper, -1);
9206 processKey(mapper, BTN_TOUCH, 0);
9207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9209 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9210
9211 // non captured touchpad should be a mouse source
9212 mFakePolicy->setPointerCapture(false);
9213 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9215 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9216}
9217
9218TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9219 std::shared_ptr<FakePointerController> fakePointerController =
9220 std::make_shared<FakePointerController>();
9221 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9222 fakePointerController->setPosition(0, 0);
9223 fakePointerController->setButtonState(0);
9224
9225 // prepare device and capture
9226 prepareDisplay(DISPLAY_ORIENTATION_0);
9227 prepareAxes(POSITION | ID | SLOT);
9228 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9229 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009230 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009231 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9232 // run uncaptured pointer tests - pushes out generic events
9233 // FINGER 0 DOWN
9234 processId(mapper, 3);
9235 processPosition(mapper, 100, 100);
9236 processKey(mapper, BTN_TOUCH, 1);
9237 processSync(mapper);
9238
9239 // start at (100,100), cursor should be at (0,0) * scale
9240 NotifyMotionArgs args;
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9243 ASSERT_NO_FATAL_FAILURE(
9244 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9245
9246 // FINGER 0 MOVE
9247 processPosition(mapper, 200, 200);
9248 processSync(mapper);
9249
9250 // compute scaling to help with touch position checking
9251 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9252 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9253 float scale =
9254 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9255
9256 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9260 0, 0, 0, 0, 0, 0, 0));
9261}
9262
9263TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9264 std::shared_ptr<FakePointerController> fakePointerController =
9265 std::make_shared<FakePointerController>();
9266
9267 prepareDisplay(DISPLAY_ORIENTATION_0);
9268 prepareAxes(POSITION | ID | SLOT);
9269 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009270 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009271 mFakePolicy->setPointerCapture(false);
9272 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9273
9274 // uncaptured touchpad should be a pointer device
9275 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9276
9277 // captured touchpad should be a touchpad device
9278 mFakePolicy->setPointerCapture(true);
9279 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9280 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9281}
9282
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009283// --- JoystickInputMapperTest ---
9284
9285class JoystickInputMapperTest : public InputMapperTest {
9286protected:
9287 static const int32_t RAW_X_MIN;
9288 static const int32_t RAW_X_MAX;
9289 static const int32_t RAW_Y_MIN;
9290 static const int32_t RAW_Y_MAX;
9291
9292 void SetUp() override {
9293 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
9294 }
9295 void prepareAxes() {
9296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
9297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
9298 }
9299
9300 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
9301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
9302 }
9303
9304 void processSync(JoystickInputMapper& mapper) {
9305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
9306 }
9307
9308 void prepareVirtualDisplay(int32_t orientation) {
9309 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
9310 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
9311 NO_PORT, ViewportType::VIRTUAL);
9312 }
9313};
9314
9315const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
9316const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
9317const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
9318const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
9319
9320TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
9321 prepareAxes();
9322 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
9323
9324 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9325
9326 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9327
9328 // Send an axis event
9329 processAxis(mapper, ABS_X, 100);
9330 processSync(mapper);
9331
9332 NotifyMotionArgs args;
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9334 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9335
9336 // Send another axis event
9337 processAxis(mapper, ABS_Y, 100);
9338 processSync(mapper);
9339
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9341 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9342}
9343
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009344// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009345
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009346class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009347protected:
9348 static const char* DEVICE_NAME;
9349 static const char* DEVICE_LOCATION;
9350 static const int32_t DEVICE_ID;
9351 static const int32_t DEVICE_GENERATION;
9352 static const int32_t DEVICE_CONTROLLER_NUMBER;
9353 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9354 static const int32_t EVENTHUB_ID;
9355
9356 std::shared_ptr<FakeEventHub> mFakeEventHub;
9357 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009358 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009359 std::unique_ptr<InstrumentedInputReader> mReader;
9360 std::shared_ptr<InputDevice> mDevice;
9361
9362 virtual void SetUp(Flags<InputDeviceClass> classes) {
9363 mFakeEventHub = std::make_unique<FakeEventHub>();
9364 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009365 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009366 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009367 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009368 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9369 }
9370
9371 void SetUp() override { SetUp(DEVICE_CLASSES); }
9372
9373 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009374 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009375 mFakePolicy.clear();
9376 }
9377
9378 void configureDevice(uint32_t changes) {
9379 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9380 mReader->requestRefreshConfiguration(changes);
9381 mReader->loopOnce();
9382 }
9383 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9384 }
9385
9386 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9387 const std::string& location, int32_t eventHubId,
9388 Flags<InputDeviceClass> classes) {
9389 InputDeviceIdentifier identifier;
9390 identifier.name = name;
9391 identifier.location = location;
9392 std::shared_ptr<InputDevice> device =
9393 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9394 identifier);
9395 mReader->pushNextDevice(device);
9396 mFakeEventHub->addDevice(eventHubId, name, classes);
9397 mReader->loopOnce();
9398 return device;
9399 }
9400
9401 template <class T, typename... Args>
9402 T& addControllerAndConfigure(Args... args) {
9403 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9404
9405 return controller;
9406 }
9407};
9408
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009409const char* PeripheralControllerTest::DEVICE_NAME = "device";
9410const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9411const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9412const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9413const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9414const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009415 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009416const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009417
9418// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009419class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009420protected:
9421 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009422 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009423 }
9424};
9425
9426TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009427 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009428
9429 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9430 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9431}
9432
9433TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009434 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009435
9436 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9437 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9438}
9439
9440// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009441class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009442protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009443 void SetUp() override {
9444 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9445 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009446};
9447
Chris Ye85758332021-05-16 23:05:17 -07009448TEST_F(LightControllerTest, MonoLight) {
9449 RawLightInfo infoMono = {.id = 1,
9450 .name = "Mono",
9451 .maxBrightness = 255,
9452 .flags = InputLightClass::BRIGHTNESS,
9453 .path = ""};
9454 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009455
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009456 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009457 InputDeviceInfo info;
9458 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009459 std::vector<InputDeviceLightInfo> lights = info.getLights();
9460 ASSERT_EQ(1U, lights.size());
9461 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009462
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009463 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9464 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009465}
9466
9467TEST_F(LightControllerTest, RGBLight) {
9468 RawLightInfo infoRed = {.id = 1,
9469 .name = "red",
9470 .maxBrightness = 255,
9471 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9472 .path = ""};
9473 RawLightInfo infoGreen = {.id = 2,
9474 .name = "green",
9475 .maxBrightness = 255,
9476 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9477 .path = ""};
9478 RawLightInfo infoBlue = {.id = 3,
9479 .name = "blue",
9480 .maxBrightness = 255,
9481 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9482 .path = ""};
9483 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9484 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9485 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9486
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009487 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009488 InputDeviceInfo info;
9489 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009490 std::vector<InputDeviceLightInfo> lights = info.getLights();
9491 ASSERT_EQ(1U, lights.size());
9492 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009493
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009494 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9495 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009496}
9497
9498TEST_F(LightControllerTest, MultiColorRGBLight) {
9499 RawLightInfo infoColor = {.id = 1,
9500 .name = "red",
9501 .maxBrightness = 255,
9502 .flags = InputLightClass::BRIGHTNESS |
9503 InputLightClass::MULTI_INTENSITY |
9504 InputLightClass::MULTI_INDEX,
9505 .path = ""};
9506
9507 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9508
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009509 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009510 InputDeviceInfo info;
9511 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009512 std::vector<InputDeviceLightInfo> lights = info.getLights();
9513 ASSERT_EQ(1U, lights.size());
9514 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009515
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009516 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9517 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009518}
9519
9520TEST_F(LightControllerTest, PlayerIdLight) {
9521 RawLightInfo info1 = {.id = 1,
9522 .name = "player1",
9523 .maxBrightness = 255,
9524 .flags = InputLightClass::BRIGHTNESS,
9525 .path = ""};
9526 RawLightInfo info2 = {.id = 2,
9527 .name = "player2",
9528 .maxBrightness = 255,
9529 .flags = InputLightClass::BRIGHTNESS,
9530 .path = ""};
9531 RawLightInfo info3 = {.id = 3,
9532 .name = "player3",
9533 .maxBrightness = 255,
9534 .flags = InputLightClass::BRIGHTNESS,
9535 .path = ""};
9536 RawLightInfo info4 = {.id = 4,
9537 .name = "player4",
9538 .maxBrightness = 255,
9539 .flags = InputLightClass::BRIGHTNESS,
9540 .path = ""};
9541 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9542 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9543 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9544 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9545
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009546 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009547 InputDeviceInfo info;
9548 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009549 std::vector<InputDeviceLightInfo> lights = info.getLights();
9550 ASSERT_EQ(1U, lights.size());
9551 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009552
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009553 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9554 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9555 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009556}
9557
Michael Wrightd02c5b62014-02-10 15:10:22 -08009558} // namespace android