blob: 2c5b32140d6e7ca42d4fe79994702aa5ed612eca [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>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070025#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <SingleTouchInputMapper.h>
28#include <SwitchInputMapper.h>
29#include <TestInputListener.h>
30#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080031#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000032#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070033#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050035#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080036#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <math.h>
38
Michael Wright17db18e2020-06-26 20:51:44 +010039#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080040#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070047using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048
49// Timeout for waiting for an expected event
50static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
54static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
56// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080057static constexpr int32_t DISPLAY_ID = 0;
58static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
59static constexpr int32_t DISPLAY_WIDTH = 480;
60static constexpr int32_t DISPLAY_HEIGHT = 800;
61static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
62static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
63static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070064static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070065static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080066
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t FIRST_SLOT = 0;
68static constexpr int32_t SECOND_SLOT = 1;
69static constexpr int32_t THIRD_SLOT = 2;
70static constexpr int32_t INVALID_TRACKING_ID = -1;
71static constexpr int32_t FIRST_TRACKING_ID = 0;
72static constexpr int32_t SECOND_TRACKING_ID = 1;
73static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080074static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080075static constexpr int32_t BATTERY_STATUS = 4;
76static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080077static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
78static constexpr int32_t LIGHT_COLOR = 0x7F448866;
79static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080080
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080081static constexpr int32_t ACTION_POINTER_0_DOWN =
82 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
83static constexpr int32_t ACTION_POINTER_0_UP =
84 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
85static constexpr int32_t ACTION_POINTER_1_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_1_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89
Michael Wrightd02c5b62014-02-10 15:10:22 -080090// Error tolerance for floating point assertions.
91static const float EPSILON = 0.001f;
92
93template<typename T>
94static inline T min(T a, T b) {
95 return a < b ? a : b;
96}
97
98static inline float avg(float x, float y) {
99 return (x + y) / 2;
100}
101
Chris Ye3fdbfef2021-01-06 18:45:18 -0800102// Mapping for light color name and the light color
103const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
104 {"green", LightColor::GREEN},
105 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700107static int32_t getInverseRotation(int32_t orientation) {
108 switch (orientation) {
109 case DISPLAY_ORIENTATION_90:
110 return DISPLAY_ORIENTATION_270;
111 case DISPLAY_ORIENTATION_270:
112 return DISPLAY_ORIENTATION_90;
113 default:
114 return orientation;
115 }
116}
117
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800118static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
119 InputDeviceInfo info;
120 mapper.populateDeviceInfo(&info);
121
122 const InputDeviceInfo::MotionRange* motionRange =
123 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
124 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
125}
126
127static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_EQ(nullptr, motionRange);
134}
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136// --- FakePointerController ---
137
138class FakePointerController : public PointerControllerInterface {
139 bool mHaveBounds;
140 float mMinX, mMinY, mMaxX, mMaxY;
141 float mX, mY;
142 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800143 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145public:
146 FakePointerController() :
147 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800148 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 }
150
Michael Wright17db18e2020-06-26 20:51:44 +0100151 virtual ~FakePointerController() {}
152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 void setBounds(float minX, float minY, float maxX, float maxY) {
154 mHaveBounds = true;
155 mMinX = minX;
156 mMinY = minY;
157 mMaxX = maxX;
158 mMaxY = maxY;
159 }
160
Chris Yea52ade12020-08-27 16:49:20 -0700161 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 mX = x;
163 mY = y;
164 }
165
Chris Yea52ade12020-08-27 16:49:20 -0700166 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Chris Yea52ade12020-08-27 16:49:20 -0700168 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Chris Yea52ade12020-08-27 16:49:20 -0700170 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 *outX = mX;
172 *outY = mY;
173 }
174
Chris Yea52ade12020-08-27 16:49:20 -0700175 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800178 mDisplayId = viewport.displayId;
179 }
180
Arthur Hung7c645402019-01-25 17:45:42 +0800181 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
182 return mSpotsByDisplay;
183 }
184
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185private:
Chris Yea52ade12020-08-27 16:49:20 -0700186 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 *outMinX = mMinX;
188 *outMinY = mMinY;
189 *outMaxX = mMaxX;
190 *outMaxY = mMaxY;
191 return mHaveBounds;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 mX += deltaX;
196 if (mX < mMinX) mX = mMinX;
197 if (mX > mMaxX) mX = mMaxX;
198 mY += deltaY;
199 if (mY < mMinY) mY = mMinY;
200 if (mY > mMaxY) mY = mMaxY;
201 }
202
Chris Yea52ade12020-08-27 16:49:20 -0700203 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
Chris Yea52ade12020-08-27 16:49:20 -0700205 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206
Chris Yea52ade12020-08-27 16:49:20 -0700207 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208
Chris Yea52ade12020-08-27 16:49:20 -0700209 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
210 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800211 std::vector<int32_t> newSpots;
212 // Add spots for fingers that are down.
213 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
214 uint32_t id = idBits.clearFirstMarkedBit();
215 newSpots.push_back(id);
216 }
217
218 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 }
220
Chris Yea52ade12020-08-27 16:49:20 -0700221 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800222
223 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224};
225
226
227// --- FakeInputReaderPolicy ---
228
229class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700230 std::mutex mLock;
231 std::condition_variable mDevicesChangedCondition;
232
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000234 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
236 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100237 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700238 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239
240protected:
Chris Yea52ade12020-08-27 16:49:20 -0700241 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
243public:
244 FakeInputReaderPolicy() {
245 }
246
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700247 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800248 waitForInputDevices([](bool devicesChanged) {
249 if (!devicesChanged) {
250 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
251 }
252 });
253 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800255 void assertInputDevicesNotChanged() {
256 waitForInputDevices([](bool devicesChanged) {
257 if (devicesChanged) {
258 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
259 }
260 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700261 }
262
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700263 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100264 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700266 }
267
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700268 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
269 return mConfig.getDisplayViewportByUniqueId(uniqueId);
270 }
271 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
272 return mConfig.getDisplayViewportByType(type);
273 }
274
275 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
276 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700277 }
278
Prabir Pradhan5632d622021-09-06 07:57:20 -0700279 void addDisplayViewport(DisplayViewport viewport) {
280 mViewports.push_back(std::move(viewport));
281 mConfig.setDisplayViewports(mViewports);
282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000285 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700286 std::optional<uint8_t> physicalPort, ViewportType type) {
287 const bool isRotated =
288 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
289 DisplayViewport v;
290 v.displayId = displayId;
291 v.orientation = orientation;
292 v.logicalLeft = 0;
293 v.logicalTop = 0;
294 v.logicalRight = isRotated ? height : width;
295 v.logicalBottom = isRotated ? width : height;
296 v.physicalLeft = 0;
297 v.physicalTop = 0;
298 v.physicalRight = isRotated ? height : width;
299 v.physicalBottom = isRotated ? width : height;
300 v.deviceWidth = isRotated ? height : width;
301 v.deviceHeight = isRotated ? width : height;
302 v.isActive = isActive;
303 v.uniqueId = uniqueId;
304 v.physicalPort = physicalPort;
305 v.type = type;
306
307 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 }
309
Arthur Hung6cd19a42019-08-30 19:04:12 +0800310 bool updateViewport(const DisplayViewport& viewport) {
311 size_t count = mViewports.size();
312 for (size_t i = 0; i < count; i++) {
313 const DisplayViewport& currentViewport = mViewports[i];
314 if (currentViewport.displayId == viewport.displayId) {
315 mViewports[i] = viewport;
316 mConfig.setDisplayViewports(mViewports);
317 return true;
318 }
319 }
320 // no viewport found.
321 return false;
322 }
323
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100324 void addExcludedDeviceName(const std::string& deviceName) {
325 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 }
327
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700328 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
329 mConfig.portAssociations.insert({inputPort, displayPort});
330 }
331
Christine Franks1ba71cc2021-04-07 14:37:42 -0700332 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
333 const std::string& displayUniqueId) {
334 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
335 }
336
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000337 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700338
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000339 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700340
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000341 void setPointerController(std::shared_ptr<FakePointerController> controller) {
342 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 }
344
345 const InputReaderConfiguration* getReaderConfiguration() const {
346 return &mConfig;
347 }
348
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800349 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 return mInputDevices;
351 }
352
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100353 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700354 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700355 return transform;
356 }
357
358 void setTouchAffineTransformation(const TouchAffineTransformation t) {
359 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800360 }
361
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 PointerCaptureRequest setPointerCapture(bool enabled) {
363 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
364 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800365 }
366
Arthur Hung7c645402019-01-25 17:45:42 +0800367 void setShowTouches(bool enabled) {
368 mConfig.showTouches = enabled;
369 }
370
Garfield Tan888a6a42020-01-09 11:39:16 -0800371 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
372 mConfig.defaultPointerDisplayId = pointerDisplayId;
373 }
374
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800375 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000378 uint32_t mNextPointerCaptureSequenceNumber = 0;
379
Chris Yea52ade12020-08-27 16:49:20 -0700380 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 *outConfig = mConfig;
382 }
383
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000384 std::shared_ptr<PointerControllerInterface> obtainPointerController(
385 int32_t /*deviceId*/) override {
386 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 }
388
Chris Yea52ade12020-08-27 16:49:20 -0700389 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700390 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 mInputDevicesChanged = true;
393 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 }
395
Chris Yea52ade12020-08-27 16:49:20 -0700396 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
397 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700398 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 }
400
Chris Yea52ade12020-08-27 16:49:20 -0700401 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800402
403 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
404 std::unique_lock<std::mutex> lock(mLock);
405 base::ScopedLockAssertion assumeLocked(mLock);
406
407 const bool devicesChanged =
408 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
409 return mInputDevicesChanged;
410 });
411 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
412 mInputDevicesChanged = false;
413 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414};
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416// --- FakeEventHub ---
417
418class FakeEventHub : public EventHubInterface {
419 struct KeyInfo {
420 int32_t keyCode;
421 uint32_t flags;
422 };
423
Chris Yef59a2f42020-10-16 12:55:26 -0700424 struct SensorInfo {
425 InputDeviceSensorType sensorType;
426 int32_t sensorDataIndex;
427 };
428
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 struct Device {
430 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700431 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 PropertyMap configuration;
433 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
434 KeyedVector<int, bool> relativeAxes;
435 KeyedVector<int32_t, int32_t> keyCodeStates;
436 KeyedVector<int32_t, int32_t> scanCodeStates;
437 KeyedVector<int32_t, int32_t> switchStates;
438 KeyedVector<int32_t, int32_t> absoluteAxisValue;
439 KeyedVector<int32_t, KeyInfo> keysByScanCode;
440 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
441 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700442 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
443 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800444 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700445 bool enabled;
446
447 status_t enable() {
448 enabled = true;
449 return OK;
450 }
451
452 status_t disable() {
453 enabled = false;
454 return OK;
455 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
Chris Ye1b0c7342020-07-28 21:57:03 -0700457 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 };
459
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700460 std::mutex mLock;
461 std::condition_variable mEventsCondition;
462
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100464 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000465 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600466 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000467 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800468 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
469 // Simulates a device light brightness, from light id to light brightness.
470 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
471 // Simulates a device light intensities, from light id to light intensities map.
472 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
473 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700475public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476 virtual ~FakeEventHub() {
477 for (size_t i = 0; i < mDevices.size(); i++) {
478 delete mDevices.valueAt(i);
479 }
480 }
481
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482 FakeEventHub() { }
483
Chris Ye1b0c7342020-07-28 21:57:03 -0700484 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 Device* device = new Device(classes);
486 device->identifier.name = name;
487 mDevices.add(deviceId, device);
488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000489 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 }
491
492 void removeDevice(int32_t deviceId) {
493 delete mDevices.valueFor(deviceId);
494 mDevices.removeItem(deviceId);
495
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000496 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497 }
498
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700499 bool isDeviceEnabled(int32_t deviceId) {
500 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700501 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700502 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
503 return false;
504 }
505 return device->enabled;
506 }
507
508 status_t enableDevice(int32_t deviceId) {
509 status_t result;
510 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700511 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
513 return BAD_VALUE;
514 }
515 if (device->enabled) {
516 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
517 return OK;
518 }
519 result = device->enable();
520 return result;
521 }
522
523 status_t disableDevice(int32_t deviceId) {
524 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700525 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700526 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
527 return BAD_VALUE;
528 }
529 if (!device->enabled) {
530 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
531 return OK;
532 }
533 return device->disable();
534 }
535
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000537 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 }
539
540 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
541 Device* device = getDevice(deviceId);
542 device->configuration.addProperty(key, value);
543 }
544
545 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
546 Device* device = getDevice(deviceId);
547 device->configuration.addAll(configuration);
548 }
549
550 void addAbsoluteAxis(int32_t deviceId, int axis,
551 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
552 Device* device = getDevice(deviceId);
553
554 RawAbsoluteAxisInfo info;
555 info.valid = true;
556 info.minValue = minValue;
557 info.maxValue = maxValue;
558 info.flat = flat;
559 info.fuzz = fuzz;
560 info.resolution = resolution;
561 device->absoluteAxes.add(axis, info);
562 }
563
564 void addRelativeAxis(int32_t deviceId, int32_t axis) {
565 Device* device = getDevice(deviceId);
566 device->relativeAxes.add(axis, true);
567 }
568
569 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
570 Device* device = getDevice(deviceId);
571 device->keyCodeStates.replaceValueFor(keyCode, state);
572 }
573
574 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
575 Device* device = getDevice(deviceId);
576 device->scanCodeStates.replaceValueFor(scanCode, state);
577 }
578
579 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
580 Device* device = getDevice(deviceId);
581 device->switchStates.replaceValueFor(switchCode, state);
582 }
583
584 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
585 Device* device = getDevice(deviceId);
586 device->absoluteAxisValue.replaceValueFor(axis, value);
587 }
588
589 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
590 int32_t keyCode, uint32_t flags) {
591 Device* device = getDevice(deviceId);
592 KeyInfo info;
593 info.keyCode = keyCode;
594 info.flags = flags;
595 if (scanCode) {
596 device->keysByScanCode.add(scanCode, info);
597 }
598 if (usageCode) {
599 device->keysByUsageCode.add(usageCode, info);
600 }
601 }
602
603 void addLed(int32_t deviceId, int32_t led, bool initialState) {
604 Device* device = getDevice(deviceId);
605 device->leds.add(led, initialState);
606 }
607
Chris Yef59a2f42020-10-16 12:55:26 -0700608 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
609 int32_t sensorDataIndex) {
610 Device* device = getDevice(deviceId);
611 SensorInfo info;
612 info.sensorType = sensorType;
613 info.sensorDataIndex = sensorDataIndex;
614 device->sensorsByAbsCode.emplace(absCode, info);
615 }
616
617 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
618 Device* device = getDevice(deviceId);
619 typename BitArray<MSC_MAX>::Buffer buffer;
620 buffer[mscEvent / 32] = 1 << mscEvent % 32;
621 device->mscBitmask.loadFromBuffer(buffer);
622 }
623
Chris Ye3fdbfef2021-01-06 18:45:18 -0800624 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
625 mRawLightInfos.emplace(rawId, std::move(info));
626 }
627
628 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
629 mLightBrightness.emplace(rawId, brightness);
630 }
631
632 void fakeLightIntensities(int32_t rawId,
633 const std::unordered_map<LightColor, int32_t> intensities) {
634 mLightIntensities.emplace(rawId, std::move(intensities));
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 bool getLedState(int32_t deviceId, int32_t led) {
638 Device* device = getDevice(deviceId);
639 return device->leds.valueFor(led);
640 }
641
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100642 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 return mExcludedDevices;
644 }
645
646 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
647 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800648 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 }
650
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000651 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
652 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700653 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 RawEvent event;
655 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000656 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 event.deviceId = deviceId;
658 event.type = type;
659 event.code = code;
660 event.value = value;
661 mEvents.push_back(event);
662
663 if (type == EV_ABS) {
664 setAbsoluteAxisValue(deviceId, code, value);
665 }
666 }
667
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600668 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
669 std::vector<TouchVideoFrame>> videoFrames) {
670 mVideoFrames = std::move(videoFrames);
671 }
672
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700674 std::unique_lock<std::mutex> lock(mLock);
675 base::ScopedLockAssertion assumeLocked(mLock);
676 const bool queueIsEmpty =
677 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
678 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
679 if (!queueIsEmpty) {
680 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
681 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 }
683
684private:
685 Device* getDevice(int32_t deviceId) const {
686 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100687 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 }
689
Chris Yea52ade12020-08-27 16:49:20 -0700690 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700692 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800693 }
694
Chris Yea52ade12020-08-27 16:49:20 -0700695 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 Device* device = getDevice(deviceId);
697 return device ? device->identifier : InputDeviceIdentifier();
698 }
699
Chris Yea52ade12020-08-27 16:49:20 -0700700 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701
Chris Yea52ade12020-08-27 16:49:20 -0700702 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 Device* device = getDevice(deviceId);
704 if (device) {
705 *outConfiguration = device->configuration;
706 }
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
710 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800712 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 ssize_t index = device->absoluteAxes.indexOfKey(axis);
714 if (index >= 0) {
715 *outAxisInfo = device->absoluteAxes.valueAt(index);
716 return OK;
717 }
718 }
719 outAxisInfo->clear();
720 return -1;
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 Device* device = getDevice(deviceId);
725 if (device) {
726 return device->relativeAxes.indexOfKey(axis) >= 0;
727 }
728 return false;
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Yef59a2f42020-10-16 12:55:26 -0700733 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
734 Device* device = getDevice(deviceId);
735 if (device) {
736 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
737 }
738 return false;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
742 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 Device* device = getDevice(deviceId);
744 if (device) {
745 const KeyInfo* key = getKey(device, scanCode, usageCode);
746 if (key) {
747 if (outKeycode) {
748 *outKeycode = key->keyCode;
749 }
750 if (outFlags) {
751 *outFlags = key->flags;
752 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700753 if (outMetaState) {
754 *outMetaState = metaState;
755 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 return OK;
757 }
758 }
759 return NAME_NOT_FOUND;
760 }
761
762 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
763 if (usageCode) {
764 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
765 if (index >= 0) {
766 return &device->keysByUsageCode.valueAt(index);
767 }
768 }
769 if (scanCode) {
770 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
771 if (index >= 0) {
772 return &device->keysByScanCode.valueAt(index);
773 }
774 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700775 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 }
777
Chris Yea52ade12020-08-27 16:49:20 -0700778 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
Chris Yef59a2f42020-10-16 12:55:26 -0700780 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
781 int32_t absCode) {
782 Device* device = getDevice(deviceId);
783 if (!device) {
784 return Errorf("Sensor device not found.");
785 }
786 auto it = device->sensorsByAbsCode.find(absCode);
787 if (it == device->sensorsByAbsCode.end()) {
788 return Errorf("Sensor map not found.");
789 }
790 const SensorInfo& info = it->second;
791 return std::make_pair(info.sensorType, info.sensorDataIndex);
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 mExcludedDevices = devices;
796 }
797
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000798 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
799 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000801 const size_t filledSize = std::min(mEvents.size(), bufferSize);
802 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
803
804 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700805 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000806 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600810 auto it = mVideoFrames.find(deviceId);
811 if (it != mVideoFrames.end()) {
812 std::vector<TouchVideoFrame> frames = std::move(it->second);
813 mVideoFrames.erase(deviceId);
814 return frames;
815 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800816 return {};
817 }
818
Chris Yea52ade12020-08-27 16:49:20 -0700819 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 Device* device = getDevice(deviceId);
821 if (device) {
822 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
823 if (index >= 0) {
824 return device->scanCodeStates.valueAt(index);
825 }
826 }
827 return AKEY_STATE_UNKNOWN;
828 }
829
Chris Yea52ade12020-08-27 16:49:20 -0700830 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 Device* device = getDevice(deviceId);
832 if (device) {
833 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
834 if (index >= 0) {
835 return device->keyCodeStates.valueAt(index);
836 }
837 }
838 return AKEY_STATE_UNKNOWN;
839 }
840
Chris Yea52ade12020-08-27 16:49:20 -0700841 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 Device* device = getDevice(deviceId);
843 if (device) {
844 ssize_t index = device->switchStates.indexOfKey(sw);
845 if (index >= 0) {
846 return device->switchStates.valueAt(index);
847 }
848 }
849 return AKEY_STATE_UNKNOWN;
850 }
851
Chris Yea52ade12020-08-27 16:49:20 -0700852 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
853 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
857 if (index >= 0) {
858 *outValue = device->absoluteAxisValue.valueAt(index);
859 return OK;
860 }
861 }
862 *outValue = 0;
863 return -1;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 // Return true if the device has non-empty key layout.
867 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
868 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 bool result = false;
870 Device* device = getDevice(deviceId);
871 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700872 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 for (size_t i = 0; i < numCodes; i++) {
874 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
875 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
876 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 }
878 }
879 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
880 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
881 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 }
883 }
884 }
885 }
886 return result;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 Device* device = getDevice(deviceId);
891 if (device) {
892 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
893 return index >= 0;
894 }
895 return false;
896 }
897
Arthur Hungcb40a002021-08-03 14:31:01 +0000898 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
899 Device* device = getDevice(deviceId);
900 if (!device) {
901 return false;
902 }
903 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
904 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
905 return true;
906 }
907 }
908 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
909 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
910 return true;
911 }
912 }
913 return false;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 return device && device->leds.indexOfKey(led) >= 0;
919 }
920
Chris Yea52ade12020-08-27 16:49:20 -0700921 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 Device* device = getDevice(deviceId);
923 if (device) {
924 ssize_t index = device->leds.indexOfKey(led);
925 if (index >= 0) {
926 device->leds.replaceValueAt(led, on);
927 } else {
928 ADD_FAILURE()
929 << "Attempted to set the state of an LED that the EventHub declared "
930 "was not present. led=" << led;
931 }
932 }
933 }
934
Chris Yea52ade12020-08-27 16:49:20 -0700935 void getVirtualKeyDefinitions(
936 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 outVirtualKeys.clear();
938
939 Device* device = getDevice(deviceId);
940 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800941 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
943 }
944
Chris Yea52ade12020-08-27 16:49:20 -0700945 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700946 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
948
Chris Yea52ade12020-08-27 16:49:20 -0700949 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 return false;
951 }
952
Chris Yea52ade12020-08-27 16:49:20 -0700953 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954
Chris Yea52ade12020-08-27 16:49:20 -0700955 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956
Chris Ye87143712020-11-10 05:05:58 +0000957 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
958
Chris Yee2b1e5c2021-03-10 22:45:12 -0800959 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
960 return BATTERY_CAPACITY;
961 }
Kim Low03ea0352020-11-06 12:45:07 -0800962
Chris Yee2b1e5c2021-03-10 22:45:12 -0800963 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
964 return BATTERY_STATUS;
965 }
966
967 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
968
969 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
970 return std::nullopt;
971 }
Kim Low03ea0352020-11-06 12:45:07 -0800972
Chris Ye3fdbfef2021-01-06 18:45:18 -0800973 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
974 std::vector<int32_t> ids;
975 for (const auto& [rawId, info] : mRawLightInfos) {
976 ids.push_back(rawId);
977 }
978 return ids;
979 }
980
981 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
982 auto it = mRawLightInfos.find(lightId);
983 if (it == mRawLightInfos.end()) {
984 return std::nullopt;
985 }
986 return it->second;
987 }
988
989 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
990 mLightBrightness.emplace(lightId, brightness);
991 }
992
993 void setLightIntensities(int32_t deviceId, int32_t lightId,
994 std::unordered_map<LightColor, int32_t> intensities) override {
995 mLightIntensities.emplace(lightId, intensities);
996 };
997
998 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
999 auto lightIt = mLightBrightness.find(lightId);
1000 if (lightIt == mLightBrightness.end()) {
1001 return std::nullopt;
1002 }
1003 return lightIt->second;
1004 }
1005
1006 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1007 int32_t deviceId, int32_t lightId) override {
1008 auto lightIt = mLightIntensities.find(lightId);
1009 if (lightIt == mLightIntensities.end()) {
1010 return std::nullopt;
1011 }
1012 return lightIt->second;
1013 };
1014
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001015 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 return false;
1017 }
1018
Chris Yea52ade12020-08-27 16:49:20 -07001019 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020
Chris Yea52ade12020-08-27 16:49:20 -07001021 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022
Chris Yea52ade12020-08-27 16:49:20 -07001023 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024
Chris Yea52ade12020-08-27 16:49:20 -07001025 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026};
1027
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028// --- FakeInputMapper ---
1029
1030class FakeInputMapper : public InputMapper {
1031 uint32_t mSources;
1032 int32_t mKeyboardType;
1033 int32_t mMetaState;
1034 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1035 KeyedVector<int32_t, int32_t> mScanCodeStates;
1036 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001037 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001039 std::mutex mLock;
1040 std::condition_variable mStateChangedCondition;
1041 bool mConfigureWasCalled GUARDED_BY(mLock);
1042 bool mResetWasCalled GUARDED_BY(mLock);
1043 bool mProcessWasCalled GUARDED_BY(mLock);
1044 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001048 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1049 : InputMapper(deviceContext),
1050 mSources(sources),
1051 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001053 mConfigureWasCalled(false),
1054 mResetWasCalled(false),
1055 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056
Chris Yea52ade12020-08-27 16:49:20 -07001057 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
1059 void setKeyboardType(int32_t keyboardType) {
1060 mKeyboardType = keyboardType;
1061 }
1062
1063 void setMetaState(int32_t metaState) {
1064 mMetaState = metaState;
1065 }
1066
1067 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::unique_lock<std::mutex> lock(mLock);
1069 base::ScopedLockAssertion assumeLocked(mLock);
1070 const bool configureCalled =
1071 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1072 return mConfigureWasCalled;
1073 });
1074 if (!configureCalled) {
1075 FAIL() << "Expected configure() to have been called.";
1076 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mConfigureWasCalled = false;
1078 }
1079
1080 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 std::unique_lock<std::mutex> lock(mLock);
1082 base::ScopedLockAssertion assumeLocked(mLock);
1083 const bool resetCalled =
1084 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1085 return mResetWasCalled;
1086 });
1087 if (!resetCalled) {
1088 FAIL() << "Expected reset() to have been called.";
1089 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 mResetWasCalled = false;
1091 }
1092
Yi Kong9b14ac62018-07-17 13:48:38 -07001093 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::unique_lock<std::mutex> lock(mLock);
1095 base::ScopedLockAssertion assumeLocked(mLock);
1096 const bool processCalled =
1097 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1098 return mProcessWasCalled;
1099 });
1100 if (!processCalled) {
1101 FAIL() << "Expected process() to have been called.";
1102 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 if (outLastEvent) {
1104 *outLastEvent = mLastEvent;
1105 }
1106 mProcessWasCalled = false;
1107 }
1108
1109 void setKeyCodeState(int32_t keyCode, int32_t state) {
1110 mKeyCodeStates.replaceValueFor(keyCode, state);
1111 }
1112
1113 void setScanCodeState(int32_t scanCode, int32_t state) {
1114 mScanCodeStates.replaceValueFor(scanCode, state);
1115 }
1116
1117 void setSwitchState(int32_t switchCode, int32_t state) {
1118 mSwitchStates.replaceValueFor(switchCode, state);
1119 }
1120
1121 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001122 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 }
1124
1125private:
Chris Yea52ade12020-08-27 16:49:20 -07001126 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Chris Yea52ade12020-08-27 16:49:20 -07001128 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 InputMapper::populateDeviceInfo(deviceInfo);
1130
1131 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1132 deviceInfo->setKeyboardType(mKeyboardType);
1133 }
1134 }
1135
Chris Yea52ade12020-08-27 16:49:20 -07001136 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001137 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001139
1140 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001141 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001142 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1143 mViewport = config->getDisplayViewportByPort(*displayPort);
1144 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001145
1146 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 }
1148
Chris Yea52ade12020-08-27 16:49:20 -07001149 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001152 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 }
1154
Chris Yea52ade12020-08-27 16:49:20 -07001155 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001156 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 mLastEvent = *rawEvent;
1158 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001159 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 }
1161
Chris Yea52ade12020-08-27 16:49:20 -07001162 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1164 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1165 }
1166
Chris Yea52ade12020-08-27 16:49:20 -07001167 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1169 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1170 }
1171
Chris Yea52ade12020-08-27 16:49:20 -07001172 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1174 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1175 }
1176
Chris Yea52ade12020-08-27 16:49:20 -07001177 // Return true if the device has non-empty key layout.
1178 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1179 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 for (size_t i = 0; i < numCodes; i++) {
1181 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1182 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1183 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 }
1185 }
1186 }
Chris Yea52ade12020-08-27 16:49:20 -07001187 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 return result;
1189 }
1190
1191 virtual int32_t getMetaState() {
1192 return mMetaState;
1193 }
1194
1195 virtual void fadePointer() {
1196 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 virtual std::optional<int32_t> getAssociatedDisplay() {
1199 if (mViewport) {
1200 return std::make_optional(mViewport->displayId);
1201 }
1202 return std::nullopt;
1203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204};
1205
1206
1207// --- InstrumentedInputReader ---
1208
1209class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001210 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211
1212public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001213 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1214 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001215 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001216 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001218 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001220 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001222 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001223 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 InputDeviceIdentifier identifier;
1225 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001228 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 }
1230
Prabir Pradhan28efc192019-11-05 01:10:04 +00001231 // Make the protected loopOnce method accessible to tests.
1232 using InputReader::loopOnce;
1233
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001235 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1236 const InputDeviceIdentifier& identifier)
1237 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001238 if (!mNextDevices.empty()) {
1239 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1240 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 return device;
1242 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001243 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 }
1245
arthurhungdcef2dc2020-08-11 14:47:50 +08001246 // --- FakeInputReaderContext ---
1247 class FakeInputReaderContext : public ContextImpl {
1248 int32_t mGlobalMetaState;
1249 bool mUpdateGlobalMetaStateWasCalled;
1250 int32_t mGeneration;
1251
1252 public:
1253 FakeInputReaderContext(InputReader* reader)
1254 : ContextImpl(reader),
1255 mGlobalMetaState(0),
1256 mUpdateGlobalMetaStateWasCalled(false),
1257 mGeneration(1) {}
1258
1259 virtual ~FakeInputReaderContext() {}
1260
1261 void assertUpdateGlobalMetaStateWasCalled() {
1262 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1263 << "Expected updateGlobalMetaState() to have been called.";
1264 mUpdateGlobalMetaStateWasCalled = false;
1265 }
1266
1267 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1268
1269 uint32_t getGeneration() { return mGeneration; }
1270
1271 void updateGlobalMetaState() override {
1272 mUpdateGlobalMetaStateWasCalled = true;
1273 ContextImpl::updateGlobalMetaState();
1274 }
1275
1276 int32_t getGlobalMetaState() override {
1277 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1278 }
1279
1280 int32_t bumpGeneration() override {
1281 mGeneration = ContextImpl::bumpGeneration();
1282 return mGeneration;
1283 }
1284 } mFakeContext;
1285
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001287
1288public:
1289 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290};
1291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292// --- InputReaderPolicyTest ---
1293class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001294protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295 sp<FakeInputReaderPolicy> mFakePolicy;
1296
Chris Yea52ade12020-08-27 16:49:20 -07001297 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1298 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299};
1300
1301/**
1302 * Check that empty set of viewports is an acceptable configuration.
1303 * Also try to get internal viewport two different ways - by type and by uniqueId.
1304 *
1305 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1306 * Such configuration is not currently allowed.
1307 */
1308TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001309 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310
1311 // We didn't add any viewports yet, so there shouldn't be any.
1312 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001313 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_FALSE(internalViewport);
1315
1316 // Add an internal viewport, then clear it
1317 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001318 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001319 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320
1321 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001322 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001324 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325
1326 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001327 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001329 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330
1331 mFakePolicy->clearViewports();
1332 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001333 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001335 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 ASSERT_FALSE(internalViewport);
1337}
1338
1339TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1340 const std::string internalUniqueId = "local:0";
1341 const std::string externalUniqueId = "local:1";
1342 const std::string virtualUniqueId1 = "virtual:2";
1343 const std::string virtualUniqueId2 = "virtual:3";
1344 constexpr int32_t virtualDisplayId1 = 2;
1345 constexpr int32_t virtualDisplayId2 = 3;
1346
1347 // Add an internal viewport
1348 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001349 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1350 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 // Add an external viewport
1352 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001353 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1354 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 // Add an virtual viewport
1356 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001357 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1358 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359 // Add another virtual viewport
1360 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001361 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1362 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by type for internal
1365 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001366 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001367 ASSERT_TRUE(internalViewport);
1368 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1369
1370 // Check matching by type for external
1371 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001372 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373 ASSERT_TRUE(externalViewport);
1374 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1375
1376 // Check matching by uniqueId for virtual viewport #1
1377 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001378 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1382 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1383
1384 // Check matching by uniqueId for virtual viewport #2
1385 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001386 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001388 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1390 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1391}
1392
1393
1394/**
1395 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1396 * that lookup works by checking display id.
1397 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1398 */
1399TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1400 const std::string uniqueId1 = "uniqueId1";
1401 const std::string uniqueId2 = "uniqueId2";
1402 constexpr int32_t displayId1 = 2;
1403 constexpr int32_t displayId2 = 3;
1404
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001405 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1406 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 for (const ViewportType& type : types) {
1408 mFakePolicy->clearViewports();
1409 // Add a viewport
1410 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1412 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another viewport
1414 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1416 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001419 std::optional<DisplayViewport> viewport1 =
1420 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(viewport1);
1422 ASSERT_EQ(displayId1, viewport1->displayId);
1423 ASSERT_EQ(type, viewport1->type);
1424
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001425 std::optional<DisplayViewport> viewport2 =
1426 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(viewport2);
1428 ASSERT_EQ(displayId2, viewport2->displayId);
1429 ASSERT_EQ(type, viewport2->type);
1430
1431 // When there are multiple viewports of the same kind, and uniqueId is not specified
1432 // in the call to getDisplayViewport, then that situation is not supported.
1433 // The viewports can be stored in any order, so we cannot rely on the order, since that
1434 // is just implementation detail.
1435 // However, we can check that it still returns *a* viewport, we just cannot assert
1436 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001437 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001438 ASSERT_TRUE(someViewport);
1439 }
1440}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001443 * When we have multiple internal displays make sure we always return the default display when
1444 * querying by type.
1445 */
1446TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1447 const std::string uniqueId1 = "uniqueId1";
1448 const std::string uniqueId2 = "uniqueId2";
1449 constexpr int32_t nonDefaultDisplayId = 2;
1450 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1451 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1452
1453 // Add the default display first and ensure it gets returned.
1454 mFakePolicy->clearViewports();
1455 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001456 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001457 ViewportType::INTERNAL);
1458 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001459 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001460 ViewportType::INTERNAL);
1461
1462 std::optional<DisplayViewport> viewport =
1463 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1464 ASSERT_TRUE(viewport);
1465 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1466 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1467
1468 // Add the default display second to make sure order doesn't matter.
1469 mFakePolicy->clearViewports();
1470 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001471 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001472 ViewportType::INTERNAL);
1473 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001475 ViewportType::INTERNAL);
1476
1477 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1478 ASSERT_TRUE(viewport);
1479 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1480 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1481}
1482
1483/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001484 * Check getDisplayViewportByPort
1485 */
1486TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001487 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001488 const std::string uniqueId1 = "uniqueId1";
1489 const std::string uniqueId2 = "uniqueId2";
1490 constexpr int32_t displayId1 = 1;
1491 constexpr int32_t displayId2 = 2;
1492 const uint8_t hdmi1 = 0;
1493 const uint8_t hdmi2 = 1;
1494 const uint8_t hdmi3 = 2;
1495
1496 mFakePolicy->clearViewports();
1497 // Add a viewport that's associated with some display port that's not of interest.
1498 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001499 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1500 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001501 // Add another viewport, connected to HDMI1 port
1502 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001503 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1504 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001505
1506 // Check that correct display viewport was returned by comparing the display ports.
1507 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1508 ASSERT_TRUE(hdmi1Viewport);
1509 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1510 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1511
1512 // Check that we can still get the same viewport using the uniqueId
1513 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1514 ASSERT_TRUE(hdmi1Viewport);
1515 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1516 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1517 ASSERT_EQ(type, hdmi1Viewport->type);
1518
1519 // Check that we cannot find a port with "HDMI2", because we never added one
1520 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1521 ASSERT_FALSE(hdmi2Viewport);
1522}
1523
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524// --- InputReaderTest ---
1525
1526class InputReaderTest : public testing::Test {
1527protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001528 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001530 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001531 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532
Chris Yea52ade12020-08-27 16:49:20 -07001533 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001534 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001536 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537
Prabir Pradhan28efc192019-11-05 01:10:04 +00001538 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001539 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540 }
1541
Chris Yea52ade12020-08-27 16:49:20 -07001542 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001543 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545 }
1546
Chris Ye1b0c7342020-07-28 21:57:03 -07001547 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 const PropertyMap* configuration) {
1549 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550
1551 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 }
1554 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001555 mReader->loopOnce();
1556 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001557 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1558 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 }
1560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001561 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001562 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001563 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001564 }
1565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001566 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001567 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001568 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001569 }
1570
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001572 const std::string& name,
1573 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001574 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1576 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001577 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 return mapper;
1580 }
1581};
1582
Chris Ye98d3f532020-10-01 21:48:59 -07001583TEST_F(InputReaderTest, PolicyGetInputDevices) {
1584 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1585 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1586 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001589 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001592 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1594 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001595 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596}
1597
Chris Yee7310032020-09-22 15:36:28 -07001598TEST_F(InputReaderTest, GetMergedInputDevices) {
1599 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1600 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1601 // Add two subdevices to device
1602 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1603 // Must add at least one mapper or the device will be ignored!
1604 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1605 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1606
1607 // Push same device instance for next device to be added, so they'll have same identifier.
1608 mReader->pushNextDevice(device);
1609 mReader->pushNextDevice(device);
1610 ASSERT_NO_FATAL_FAILURE(
1611 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1612 ASSERT_NO_FATAL_FAILURE(
1613 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1614
1615 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001616 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001617}
1618
Chris Yee14523a2020-12-19 13:46:00 -08001619TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1620 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1621 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1622 // Add two subdevices to device
1623 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1624 // Must add at least one mapper or the device will be ignored!
1625 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1626 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1627
1628 // Push same device instance for next device to be added, so they'll have same identifier.
1629 mReader->pushNextDevice(device);
1630 mReader->pushNextDevice(device);
1631 // Sensor device is initially disabled
1632 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1633 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1634 nullptr));
1635 // Device is disabled because the only sub device is a sensor device and disabled initially.
1636 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1637 ASSERT_FALSE(device->isEnabled());
1638 ASSERT_NO_FATAL_FAILURE(
1639 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1640 // The merged device is enabled if any sub device is enabled
1641 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1642 ASSERT_TRUE(device->isEnabled());
1643}
1644
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001645TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001647 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t eventHubId = 1;
1649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001650 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001652 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001654
Yi Kong9b14ac62018-07-17 13:48:38 -07001655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001656
1657 NotifyDeviceResetArgs resetArgs;
1658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001659 ASSERT_EQ(deviceId, resetArgs.deviceId);
1660
1661 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001663 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001664
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001666 ASSERT_EQ(deviceId, resetArgs.deviceId);
1667 ASSERT_EQ(device->isEnabled(), false);
1668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001669 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001670 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001673 ASSERT_EQ(device->isEnabled(), false);
1674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001675 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001678 ASSERT_EQ(deviceId, resetArgs.deviceId);
1679 ASSERT_EQ(device->isEnabled(), true);
1680}
1681
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001684 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001688 AINPUT_SOURCE_KEYBOARD, nullptr);
1689 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690
1691 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1692 AINPUT_SOURCE_ANY, AKEYCODE_A))
1693 << "Should return unknown when the device id is >= 0 but unknown.";
1694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1696 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1697 << "Should return unknown when the device id is valid but the sources are not "
1698 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001700 ASSERT_EQ(AKEY_STATE_DOWN,
1701 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1702 AKEYCODE_A))
1703 << "Should return value provided by mapper when device id is valid and the device "
1704 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705
1706 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1707 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1708 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1709
1710 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1711 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1712 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1713}
1714
1715TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001716 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001717 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001719 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 AINPUT_SOURCE_KEYBOARD, nullptr);
1722 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723
1724 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1725 AINPUT_SOURCE_ANY, KEY_A))
1726 << "Should return unknown when the device id is >= 0 but unknown.";
1727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1729 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1730 << "Should return unknown when the device id is valid but the sources are not "
1731 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 ASSERT_EQ(AKEY_STATE_DOWN,
1734 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1735 KEY_A))
1736 << "Should return value provided by mapper when device id is valid and the device "
1737 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
1739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1740 AINPUT_SOURCE_TRACKBALL, KEY_A))
1741 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1742
1743 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1744 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1745 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1746}
1747
1748TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001750 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001753 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001754 AINPUT_SOURCE_KEYBOARD, nullptr);
1755 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756
1757 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1758 AINPUT_SOURCE_ANY, SW_LID))
1759 << "Should return unknown when the device id is >= 0 but unknown.";
1760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1762 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1763 << "Should return unknown when the device id is valid but the sources are not "
1764 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001766 ASSERT_EQ(AKEY_STATE_DOWN,
1767 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1768 SW_LID))
1769 << "Should return value provided by mapper when device id is valid and the device "
1770 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001771
1772 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1773 AINPUT_SOURCE_TRACKBALL, SW_LID))
1774 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1775
1776 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1777 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1778 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1779}
1780
1781TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001783 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 mapper.addSupportedKeyCode(AKEYCODE_A);
1790 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
1792 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1793 uint8_t flags[4] = { 0, 0, 0, 1 };
1794
1795 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1796 << "Should return false when device id is >= 0 but unknown.";
1797 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1798
1799 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1801 << "Should return false when device id is valid but the sources are not supported by "
1802 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1804
1805 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1807 keyCodes, flags))
1808 << "Should return value provided by mapper when device id is valid and the device "
1809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1811
1812 flags[3] = 1;
1813 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1814 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1815 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1816
1817 flags[3] = 1;
1818 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1819 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1820 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1821}
1822
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001823TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001824 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001825 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 NotifyConfigurationChangedArgs args;
1828
1829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1830 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1831}
1832
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001833TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001835 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001836 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001838 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001839 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001840 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001841 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001843 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001844 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1846
1847 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001849 ASSERT_EQ(when, event.when);
1850 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 ASSERT_EQ(EV_KEY, event.type);
1853 ASSERT_EQ(KEY_A, event.code);
1854 ASSERT_EQ(1, event.value);
1855}
1856
Garfield Tan1c7bc862020-01-28 13:24:04 -08001857TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001858 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001859 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 constexpr int32_t eventHubId = 1;
1861 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001862 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001864 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001866
1867 NotifyDeviceResetArgs resetArgs;
1868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001869 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001872 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001874 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001875 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001878 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001880 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001881 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001884 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001886 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001887 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001888}
1889
Garfield Tan1c7bc862020-01-28 13:24:04 -08001890TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1891 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001892 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001893 constexpr int32_t eventHubId = 1;
1894 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1895 // Must add at least one mapper or the device will be ignored!
1896 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001897 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001898 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1899
1900 NotifyDeviceResetArgs resetArgs;
1901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1902 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1903}
1904
Arthur Hungc23540e2018-11-29 20:42:11 +08001905TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001907 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001908 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001909 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1911 FakeInputMapper& mapper =
1912 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001913 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001914
1915 const uint8_t hdmi1 = 1;
1916
1917 // Associated touch screen with second display.
1918 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1919
1920 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001921 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001922 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001923 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001924 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001925 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001926 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001927 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001928 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001929 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001930
1931 // Add the device, and make sure all of the callbacks are triggered.
1932 // The device is added after the input port associations are processed since
1933 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001938
Arthur Hung2c9a3342019-07-23 14:18:59 +08001939 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001940 ASSERT_EQ(deviceId, device->getId());
1941 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1942 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001943
1944 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001946 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001947 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001948}
1949
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001950TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1951 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1952 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1953 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1954 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1955 // Must add at least one mapper or the device will be ignored!
1956 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1957 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1958 mReader->pushNextDevice(device);
1959 mReader->pushNextDevice(device);
1960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1962
1963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1964
1965 NotifyDeviceResetArgs resetArgs;
1966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1967 ASSERT_EQ(deviceId, resetArgs.deviceId);
1968 ASSERT_TRUE(device->isEnabled());
1969 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1970 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1971
1972 disableDevice(deviceId);
1973 mReader->loopOnce();
1974
1975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1976 ASSERT_EQ(deviceId, resetArgs.deviceId);
1977 ASSERT_FALSE(device->isEnabled());
1978 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1979 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1980
1981 enableDevice(deviceId);
1982 mReader->loopOnce();
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1985 ASSERT_EQ(deviceId, resetArgs.deviceId);
1986 ASSERT_TRUE(device->isEnabled());
1987 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1988 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1989}
1990
1991TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1993 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1994 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1995 // Add two subdevices to device
1996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1997 FakeInputMapper& mapperDevice1 =
1998 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1999 FakeInputMapper& mapperDevice2 =
2000 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2001 mReader->pushNextDevice(device);
2002 mReader->pushNextDevice(device);
2003 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2004 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2005
2006 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2007 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2008
2009 ASSERT_EQ(AKEY_STATE_DOWN,
2010 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2011 ASSERT_EQ(AKEY_STATE_DOWN,
2012 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2013 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2014 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2015}
2016
Prabir Pradhan7e186182020-11-10 13:56:45 -08002017TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2018 NotifyPointerCaptureChangedArgs args;
2019
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002020 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002021 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2022 mReader->loopOnce();
2023 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002024 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2025 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002026
2027 mFakePolicy->setPointerCapture(false);
2028 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2029 mReader->loopOnce();
2030 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002031 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002032
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002033 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002034 // does not change.
2035 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2036 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002037 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002038}
2039
Chris Ye87143712020-11-10 05:05:58 +00002040class FakeVibratorInputMapper : public FakeInputMapper {
2041public:
2042 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2043 : FakeInputMapper(deviceContext, sources) {}
2044
2045 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2046};
2047
2048TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2049 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2050 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2051 constexpr int32_t eventHubId = 1;
2052 const char* DEVICE_LOCATION = "BLUETOOTH";
2053 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2054 FakeVibratorInputMapper& mapper =
2055 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2056 mReader->pushNextDevice(device);
2057
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2059 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2060
2061 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2062 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2063}
2064
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002065// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002066
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002067class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002068public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002069 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002070
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002071 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002072
2073 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2074
2075 void dump(std::string& dump) override {}
2076
2077 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2078 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002079 }
2080
Chris Yee2b1e5c2021-03-10 22:45:12 -08002081 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2082 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002083 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002084
2085 bool setLightColor(int32_t lightId, int32_t color) override {
2086 getDeviceContext().setLightBrightness(lightId, color >> 24);
2087 return true;
2088 }
2089
2090 std::optional<int32_t> getLightColor(int32_t lightId) override {
2091 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2092 if (!result.has_value()) {
2093 return std::nullopt;
2094 }
2095 return result.value() << 24;
2096 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002097
2098 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2099
2100 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2101
2102private:
2103 InputDeviceContext& mDeviceContext;
2104 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2105 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002106};
2107
Chris Yee2b1e5c2021-03-10 22:45:12 -08002108TEST_F(InputReaderTest, BatteryGetCapacity) {
2109 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2110 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2111 constexpr int32_t eventHubId = 1;
2112 const char* DEVICE_LOCATION = "BLUETOOTH";
2113 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002114 FakePeripheralController& controller =
2115 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002116 mReader->pushNextDevice(device);
2117
2118 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2119
2120 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2121 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2122}
2123
2124TEST_F(InputReaderTest, BatteryGetStatus) {
2125 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2126 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2127 constexpr int32_t eventHubId = 1;
2128 const char* DEVICE_LOCATION = "BLUETOOTH";
2129 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002130 FakePeripheralController& controller =
2131 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002132 mReader->pushNextDevice(device);
2133
2134 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2135
2136 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2137 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2138}
2139
Chris Ye3fdbfef2021-01-06 18:45:18 -08002140TEST_F(InputReaderTest, LightGetColor) {
2141 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2142 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2143 constexpr int32_t eventHubId = 1;
2144 const char* DEVICE_LOCATION = "BLUETOOTH";
2145 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002146 FakePeripheralController& controller =
2147 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002148 mReader->pushNextDevice(device);
2149 RawLightInfo info = {.id = 1,
2150 .name = "Mono",
2151 .maxBrightness = 255,
2152 .flags = InputLightClass::BRIGHTNESS,
2153 .path = ""};
2154 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2155 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2156
2157 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2160 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002161 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2162 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2163}
2164
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002165// --- InputReaderIntegrationTest ---
2166
2167// These tests create and interact with the InputReader only through its interface.
2168// The InputReader is started during SetUp(), which starts its processing in its own
2169// thread. The tests use linux uinput to emulate input devices.
2170// NOTE: Interacting with the physical device while these tests are running may cause
2171// the tests to fail.
2172class InputReaderIntegrationTest : public testing::Test {
2173protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002174 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002175 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002176 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002177
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002178 std::shared_ptr<FakePointerController> mFakePointerController;
2179
Chris Yea52ade12020-08-27 16:49:20 -07002180 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002181 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002182 mFakePointerController = std::make_shared<FakePointerController>();
2183 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002184 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2185 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002186
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002187 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2188 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002189 ASSERT_EQ(mReader->start(), OK);
2190
2191 // Since this test is run on a real device, all the input devices connected
2192 // to the test device will show up in mReader. We wait for those input devices to
2193 // show up before beginning the tests.
2194 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2196 }
2197
Chris Yea52ade12020-08-27 16:49:20 -07002198 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002199 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002200 mReader.reset();
2201 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002202 mFakePolicy.clear();
2203 }
2204};
2205
2206TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2207 // An invalid input device that is only used for this test.
2208 class InvalidUinputDevice : public UinputDevice {
2209 public:
2210 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2211
2212 private:
2213 void configureDevice(int fd, uinput_user_dev* device) override {}
2214 };
2215
2216 const size_t numDevices = mFakePolicy->getInputDevices().size();
2217
2218 // UinputDevice does not set any event or key bits, so InputReader should not
2219 // consider it as a valid device.
2220 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2221 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2223 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2224
2225 invalidDevice.reset();
2226 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2228 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2229}
2230
2231TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2232 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2233
2234 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2235 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2236 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2237 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2238
2239 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002241 const auto& it =
2242 std::find_if(inputDevices.begin(), inputDevices.end(),
2243 [&keyboard](const InputDeviceInfo& info) {
2244 return info.getIdentifier().name == keyboard->getName();
2245 });
2246
2247 ASSERT_NE(it, inputDevices.end());
2248 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2249 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2250 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251
2252 keyboard.reset();
2253 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2255 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2256}
2257
2258TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2259 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2260 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2261
2262 NotifyConfigurationChangedArgs configChangedArgs;
2263 ASSERT_NO_FATAL_FAILURE(
2264 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002265 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2267
2268 NotifyKeyArgs keyArgs;
2269 keyboard->pressAndReleaseHomeKey();
2270 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2271 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002272 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002273 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002274 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002275 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276 prevTimestamp = keyArgs.eventTime;
2277
2278 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002280 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002282 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002285/**
2286 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2287 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2288 * are passed to the listener.
2289 */
2290static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2291TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2292 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2294 NotifyKeyArgs keyArgs;
2295
2296 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2297 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2299 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2300
2301 controller->pressAndReleaseKey(BTN_GEAR_UP);
2302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2304 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2305}
2306
Arthur Hungaab25622020-01-16 11:22:11 +08002307// --- TouchProcessTest ---
2308class TouchIntegrationTest : public InputReaderIntegrationTest {
2309protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002310 const std::string UNIQUE_ID = "local:0";
2311
Chris Yea52ade12020-08-27 16:49:20 -07002312 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002313 InputReaderIntegrationTest::SetUp();
2314 // At least add an internal display.
2315 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2316 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002317 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002318
2319 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2320 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2322 }
2323
2324 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2325 int32_t orientation, const std::string& uniqueId,
2326 std::optional<uint8_t> physicalPort,
2327 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002328 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2329 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002330 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2331 }
2332
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002333 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2334 NotifyMotionArgs args;
2335 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2336 EXPECT_EQ(action, args.action);
2337 ASSERT_EQ(points.size(), args.pointerCount);
2338 for (size_t i = 0; i < args.pointerCount; i++) {
2339 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2340 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2341 }
2342 }
2343
Arthur Hungaab25622020-01-16 11:22:11 +08002344 std::unique_ptr<UinputTouchScreen> mDevice;
2345};
2346
2347TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2348 NotifyMotionArgs args;
2349 const Point centerPoint = mDevice->getCenterPoint();
2350
2351 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002352 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002353 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002354 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2357
2358 // ACTION_MOVE
2359 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002360 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2363
2364 // ACTION_UP
2365 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002366 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2368 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2369}
2370
2371TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2372 NotifyMotionArgs args;
2373 const Point centerPoint = mDevice->getCenterPoint();
2374
2375 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002376 mDevice->sendSlot(FIRST_SLOT);
2377 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002378 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002379 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2382
2383 // ACTION_POINTER_DOWN (Second slot)
2384 const Point secondPoint = centerPoint + Point(100, 100);
2385 mDevice->sendSlot(SECOND_SLOT);
2386 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002387 mDevice->sendDown(secondPoint);
2388 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002390 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002391
2392 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002393 mDevice->sendMove(secondPoint + Point(1, 1));
2394 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002395 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2397
2398 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002399 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002400 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002402 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002403
2404 // ACTION_UP
2405 mDevice->sendSlot(FIRST_SLOT);
2406 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002407 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2410}
2411
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002412/**
2413 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2414 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2415 * data?
2416 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2417 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2418 * for Pointer 0 only is generated after.
2419 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2420 * events, we will not miss any information.
2421 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2422 * event generated afterwards that contains the newest movement of pointer 0.
2423 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2424 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2425 * losing information about non-palm pointers.
2426 */
2427TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2428 NotifyMotionArgs args;
2429 const Point centerPoint = mDevice->getCenterPoint();
2430
2431 // ACTION_DOWN
2432 mDevice->sendSlot(FIRST_SLOT);
2433 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2434 mDevice->sendDown(centerPoint);
2435 mDevice->sendSync();
2436 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2437
2438 // ACTION_POINTER_DOWN (Second slot)
2439 const Point secondPoint = centerPoint + Point(100, 100);
2440 mDevice->sendSlot(SECOND_SLOT);
2441 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2442 mDevice->sendDown(secondPoint);
2443 mDevice->sendSync();
2444 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2445
2446 // ACTION_MOVE (First slot)
2447 mDevice->sendSlot(FIRST_SLOT);
2448 mDevice->sendMove(centerPoint + Point(5, 5));
2449 // ACTION_POINTER_UP (Second slot)
2450 mDevice->sendSlot(SECOND_SLOT);
2451 mDevice->sendPointerUp();
2452 // Send a single sync for the above 2 pointer updates
2453 mDevice->sendSync();
2454
2455 // First, we should get POINTER_UP for the second pointer
2456 assertReceivedMotion(ACTION_POINTER_1_UP,
2457 {/*first pointer */ centerPoint + Point(5, 5),
2458 /*second pointer*/ secondPoint});
2459
2460 // Next, the MOVE event for the first pointer
2461 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2462}
2463
2464/**
2465 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2466 * move, and then it will go up, all in the same frame.
2467 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2468 * gets sent to the listener.
2469 */
2470TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2471 NotifyMotionArgs args;
2472 const Point centerPoint = mDevice->getCenterPoint();
2473
2474 // ACTION_DOWN
2475 mDevice->sendSlot(FIRST_SLOT);
2476 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2477 mDevice->sendDown(centerPoint);
2478 mDevice->sendSync();
2479 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2480
2481 // ACTION_POINTER_DOWN (Second slot)
2482 const Point secondPoint = centerPoint + Point(100, 100);
2483 mDevice->sendSlot(SECOND_SLOT);
2484 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2485 mDevice->sendDown(secondPoint);
2486 mDevice->sendSync();
2487 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2488
2489 // ACTION_MOVE (First slot)
2490 mDevice->sendSlot(FIRST_SLOT);
2491 mDevice->sendMove(centerPoint + Point(5, 5));
2492 // ACTION_POINTER_UP (Second slot)
2493 mDevice->sendSlot(SECOND_SLOT);
2494 mDevice->sendMove(secondPoint + Point(6, 6));
2495 mDevice->sendPointerUp();
2496 // Send a single sync for the above 2 pointer updates
2497 mDevice->sendSync();
2498
2499 // First, we should get POINTER_UP for the second pointer
2500 // The movement of the second pointer during the liftoff frame is ignored.
2501 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2502 assertReceivedMotion(ACTION_POINTER_1_UP,
2503 {/*first pointer */ centerPoint + Point(5, 5),
2504 /*second pointer*/ secondPoint});
2505
2506 // Next, the MOVE event for the first pointer
2507 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2508}
2509
Arthur Hungaab25622020-01-16 11:22:11 +08002510TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2511 NotifyMotionArgs args;
2512 const Point centerPoint = mDevice->getCenterPoint();
2513
2514 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002515 mDevice->sendSlot(FIRST_SLOT);
2516 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002517 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002518 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002519 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2521
arthurhungcc7f9802020-04-30 17:55:40 +08002522 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002523 const Point secondPoint = centerPoint + Point(100, 100);
2524 mDevice->sendSlot(SECOND_SLOT);
2525 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2526 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002527 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002528 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002529 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002530
arthurhungcc7f9802020-04-30 17:55:40 +08002531 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002532 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002533 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002534 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2536
arthurhungcc7f9802020-04-30 17:55:40 +08002537 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2538 // a palm event.
2539 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002540 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002543 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002544 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002545
arthurhungcc7f9802020-04-30 17:55:40 +08002546 // Send up to second slot, expect first slot send moving.
2547 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002548 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002549 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002551
arthurhungcc7f9802020-04-30 17:55:40 +08002552 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002553 mDevice->sendSlot(FIRST_SLOT);
2554 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002555 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002556
arthurhungcc7f9802020-04-30 17:55:40 +08002557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002559}
2560
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562class InputDeviceTest : public testing::Test {
2563protected:
2564 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002565 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566 static const int32_t DEVICE_ID;
2567 static const int32_t DEVICE_GENERATION;
2568 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002569 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002570 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002572 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002573 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002574 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002575 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002576 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577
Chris Yea52ade12020-08-27 16:49:20 -07002578 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002579 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002581 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002582 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002583 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002584 InputDeviceIdentifier identifier;
2585 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002586 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002587 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002588 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002589 mReader->pushNextDevice(mDevice);
2590 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2591 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002592 }
2593
Chris Yea52ade12020-08-27 16:49:20 -07002594 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002595 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002596 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 }
2598};
2599
2600const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002601const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002602const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2604const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002605const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2606 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002607const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002608
2609TEST_F(InputDeviceTest, ImmutableProperties) {
2610 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002611 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002612 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002613}
2614
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002615TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2616 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002617}
2618
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2620 // Configuration.
2621 InputReaderConfiguration config;
2622 mDevice->configure(ARBITRARY_TIME, &config, 0);
2623
2624 // Reset.
2625 mDevice->reset(ARBITRARY_TIME);
2626
2627 NotifyDeviceResetArgs resetArgs;
2628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2629 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2630 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2631
2632 // Metadata.
2633 ASSERT_TRUE(mDevice->isIgnored());
2634 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2635
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002636 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002638 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2640 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2641
2642 // State queries.
2643 ASSERT_EQ(0, mDevice->getMetaState());
2644
2645 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2646 << "Ignored device should return unknown key code state.";
2647 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2648 << "Ignored device should return unknown scan code state.";
2649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2650 << "Ignored device should return unknown switch state.";
2651
2652 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2653 uint8_t flags[2] = { 0, 1 };
2654 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2655 << "Ignored device should never mark any key codes.";
2656 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2657 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2658}
2659
2660TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2661 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002662 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002664 FakeInputMapper& mapper1 =
2665 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002666 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2667 mapper1.setMetaState(AMETA_ALT_ON);
2668 mapper1.addSupportedKeyCode(AKEYCODE_A);
2669 mapper1.addSupportedKeyCode(AKEYCODE_B);
2670 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2671 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2672 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2673 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2674 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002675
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002676 FakeInputMapper& mapper2 =
2677 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002678 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679
2680 InputReaderConfiguration config;
2681 mDevice->configure(ARBITRARY_TIME, &config, 0);
2682
2683 String8 propertyValue;
2684 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2685 << "Device should have read configuration during configuration phase.";
2686 ASSERT_STREQ("value", propertyValue.string());
2687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002688 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2689 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690
2691 // Reset
2692 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002693 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2694 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695
2696 NotifyDeviceResetArgs resetArgs;
2697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2698 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2699 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2700
2701 // Metadata.
2702 ASSERT_FALSE(mDevice->isIgnored());
2703 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2704
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002705 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002707 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2709 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2710
2711 // State queries.
2712 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2713 << "Should query mappers and combine meta states.";
2714
2715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2716 << "Should return unknown key code state when source not supported.";
2717 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2718 << "Should return unknown scan code state when source not supported.";
2719 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2720 << "Should return unknown switch state when source not supported.";
2721
2722 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2723 << "Should query mapper when source is supported.";
2724 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2725 << "Should query mapper when source is supported.";
2726 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2727 << "Should query mapper when source is supported.";
2728
2729 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2730 uint8_t flags[4] = { 0, 0, 0, 1 };
2731 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2732 << "Should do nothing when source is unsupported.";
2733 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2734 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2735 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2736 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2737
2738 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2739 << "Should query mapper when source is supported.";
2740 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2741 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2742 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2743 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2744
2745 // Event handling.
2746 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002747 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748 mDevice->process(&event, 1);
2749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002750 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2751 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002752}
2753
Arthur Hung2c9a3342019-07-23 14:18:59 +08002754// A single input device is associated with a specific display. Check that:
2755// 1. Device is disabled if the viewport corresponding to the associated display is not found
2756// 2. Device is disabled when setEnabled API is called
2757TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002758 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002759
2760 // First Configuration.
2761 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2762
2763 // Device should be enabled by default.
2764 ASSERT_TRUE(mDevice->isEnabled());
2765
2766 // Prepare associated info.
2767 constexpr uint8_t hdmi = 1;
2768 const std::string UNIQUE_ID = "local:1";
2769
2770 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2771 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2772 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2773 // Device should be disabled because it is associated with a specific display via
2774 // input port <-> display port association, but the corresponding display is not found
2775 ASSERT_FALSE(mDevice->isEnabled());
2776
2777 // Prepare displays.
2778 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002779 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2780 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002781 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2782 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2783 ASSERT_TRUE(mDevice->isEnabled());
2784
2785 // Device should be disabled after set disable.
2786 mFakePolicy->addDisabledDevice(mDevice->getId());
2787 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2788 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2789 ASSERT_FALSE(mDevice->isEnabled());
2790
2791 // Device should still be disabled even found the associated display.
2792 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2793 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2794 ASSERT_FALSE(mDevice->isEnabled());
2795}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002796
Christine Franks1ba71cc2021-04-07 14:37:42 -07002797TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2798 // Device should be enabled by default.
2799 mFakePolicy->clearViewports();
2800 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2801 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2802 ASSERT_TRUE(mDevice->isEnabled());
2803
2804 // Device should be disabled because it is associated with a specific display, but the
2805 // corresponding display is not found.
2806 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2807 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2808 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2809 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2810 ASSERT_FALSE(mDevice->isEnabled());
2811
2812 // Device should be enabled when a display is found.
2813 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2814 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2815 NO_PORT, ViewportType::INTERNAL);
2816 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2817 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2818 ASSERT_TRUE(mDevice->isEnabled());
2819
2820 // Device should be disabled after set disable.
2821 mFakePolicy->addDisabledDevice(mDevice->getId());
2822 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2823 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2824 ASSERT_FALSE(mDevice->isEnabled());
2825
2826 // Device should still be disabled even found the associated display.
2827 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2828 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2829 ASSERT_FALSE(mDevice->isEnabled());
2830}
2831
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832// --- InputMapperTest ---
2833
2834class InputMapperTest : public testing::Test {
2835protected:
2836 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002837 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 static const int32_t DEVICE_ID;
2839 static const int32_t DEVICE_GENERATION;
2840 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002841 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002842 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002844 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002846 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002847 std::unique_ptr<InstrumentedInputReader> mReader;
2848 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849
Chris Ye1b0c7342020-07-28 21:57:03 -07002850 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002851 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002852 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002853 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002854 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002855 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002856 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 }
2858
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002859 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002860 SetUp(DEVICE_CLASSES);
2861 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002862
Chris Yea52ade12020-08-27 16:49:20 -07002863 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002864 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002866 }
2867
2868 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002869 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002870 }
2871
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002872 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002873 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002874 mReader->requestRefreshConfiguration(changes);
2875 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002876 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002877 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2878 }
2879
arthurhungdcef2dc2020-08-11 14:47:50 +08002880 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2881 const std::string& location, int32_t eventHubId,
2882 Flags<InputDeviceClass> classes) {
2883 InputDeviceIdentifier identifier;
2884 identifier.name = name;
2885 identifier.location = location;
2886 std::shared_ptr<InputDevice> device =
2887 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2888 identifier);
2889 mReader->pushNextDevice(device);
2890 mFakeEventHub->addDevice(eventHubId, name, classes);
2891 mReader->loopOnce();
2892 return device;
2893 }
2894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002895 template <class T, typename... Args>
2896 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002897 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002898 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002899 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002900 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002901 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 }
2903
2904 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002905 int32_t orientation, const std::string& uniqueId,
2906 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002907 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2908 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002909 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2910 }
2911
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002912 void clearViewports() {
2913 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002914 }
2915
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002916 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2917 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 RawEvent event;
2919 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002920 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002921 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922 event.type = type;
2923 event.code = code;
2924 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002925 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002926 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002927 }
2928
2929 static void assertMotionRange(const InputDeviceInfo& info,
2930 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2931 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002932 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2934 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2935 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2936 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2937 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2938 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2939 }
2940
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002941 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2942 float size, float touchMajor, float touchMinor, float toolMajor,
2943 float toolMinor, float orientation, float distance,
2944 float scaledAxisEpsilon = 1.f) {
2945 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2946 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002947 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2948 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002949 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2950 scaledAxisEpsilon);
2951 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2952 scaledAxisEpsilon);
2953 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2954 scaledAxisEpsilon);
2955 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2956 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2958 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2959 }
2960
Michael Wright17db18e2020-06-26 20:51:44 +01002961 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002963 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964 ASSERT_NEAR(x, actualX, 1);
2965 ASSERT_NEAR(y, actualY, 1);
2966 }
2967};
2968
2969const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002970const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002971const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2973const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002974const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2975 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002976const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
2978// --- SwitchInputMapperTest ---
2979
2980class SwitchInputMapperTest : public InputMapperTest {
2981protected:
2982};
2983
2984TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002985 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002987 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988}
2989
2990TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002991 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002993 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002994 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002996 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002997 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
3000TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007
3008 NotifySwitchArgs args;
3009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3010 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003011 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3012 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003013 args.switchMask);
3014 ASSERT_EQ(uint32_t(0), args.policyFlags);
3015}
3016
Chris Ye87143712020-11-10 05:05:58 +00003017// --- VibratorInputMapperTest ---
3018class VibratorInputMapperTest : public InputMapperTest {
3019protected:
3020 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3021};
3022
3023TEST_F(VibratorInputMapperTest, GetSources) {
3024 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3025
3026 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3027}
3028
3029TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3030 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3031
3032 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3033}
3034
3035TEST_F(VibratorInputMapperTest, Vibrate) {
3036 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003037 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003038 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3039
3040 VibrationElement pattern(2);
3041 VibrationSequence sequence(2);
3042 pattern.duration = std::chrono::milliseconds(200);
3043 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3044 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3045 sequence.addElement(pattern);
3046 pattern.duration = std::chrono::milliseconds(500);
3047 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3048 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3049 sequence.addElement(pattern);
3050
3051 std::vector<int64_t> timings = {0, 1};
3052 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3053
3054 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003055 // Start vibrating
3056 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003057 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003058 // Verify vibrator state listener was notified.
3059 mReader->loopOnce();
3060 NotifyVibratorStateArgs args;
3061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3062 ASSERT_EQ(DEVICE_ID, args.deviceId);
3063 ASSERT_TRUE(args.isOn);
3064 // Stop vibrating
3065 mapper.cancelVibrate(VIBRATION_TOKEN);
3066 ASSERT_FALSE(mapper.isVibrating());
3067 // Verify vibrator state listener was notified.
3068 mReader->loopOnce();
3069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3070 ASSERT_EQ(DEVICE_ID, args.deviceId);
3071 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003072}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Chris Yef59a2f42020-10-16 12:55:26 -07003074// --- SensorInputMapperTest ---
3075
3076class SensorInputMapperTest : public InputMapperTest {
3077protected:
3078 static const int32_t ACCEL_RAW_MIN;
3079 static const int32_t ACCEL_RAW_MAX;
3080 static const int32_t ACCEL_RAW_FUZZ;
3081 static const int32_t ACCEL_RAW_FLAT;
3082 static const int32_t ACCEL_RAW_RESOLUTION;
3083
3084 static const int32_t GYRO_RAW_MIN;
3085 static const int32_t GYRO_RAW_MAX;
3086 static const int32_t GYRO_RAW_FUZZ;
3087 static const int32_t GYRO_RAW_FLAT;
3088 static const int32_t GYRO_RAW_RESOLUTION;
3089
3090 static const float GRAVITY_MS2_UNIT;
3091 static const float DEGREE_RADIAN_UNIT;
3092
3093 void prepareAccelAxes();
3094 void prepareGyroAxes();
3095 void setAccelProperties();
3096 void setGyroProperties();
3097 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3098};
3099
3100const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3101const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3102const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3103const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3104const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3105
3106const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3107const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3108const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3109const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3110const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3111
3112const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3113const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3114
3115void SensorInputMapperTest::prepareAccelAxes() {
3116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3117 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3119 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3121 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3122}
3123
3124void SensorInputMapperTest::prepareGyroAxes() {
3125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3126 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3127 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3128 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3130 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3131}
3132
3133void SensorInputMapperTest::setAccelProperties() {
3134 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3135 /* sensorDataIndex */ 0);
3136 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3137 /* sensorDataIndex */ 1);
3138 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3139 /* sensorDataIndex */ 2);
3140 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3141 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3142 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3143 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3144 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3145}
3146
3147void SensorInputMapperTest::setGyroProperties() {
3148 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3149 /* sensorDataIndex */ 0);
3150 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3151 /* sensorDataIndex */ 1);
3152 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3153 /* sensorDataIndex */ 2);
3154 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3155 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3156 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3157 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3158 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3159}
3160
3161TEST_F(SensorInputMapperTest, GetSources) {
3162 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3163
3164 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3165}
3166
3167TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3168 setAccelProperties();
3169 prepareAccelAxes();
3170 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3171
3172 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3173 std::chrono::microseconds(10000),
3174 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003175 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003181
3182 NotifySensorArgs args;
3183 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3184 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3185 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3186
3187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3188 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3189 ASSERT_EQ(args.deviceId, DEVICE_ID);
3190 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3191 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3192 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3193 ASSERT_EQ(args.values, values);
3194 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3195}
3196
3197TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3198 setGyroProperties();
3199 prepareGyroAxes();
3200 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3201
3202 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3203 std::chrono::microseconds(10000),
3204 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003205 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003211
3212 NotifySensorArgs args;
3213 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3214 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3215 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3216
3217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3218 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3219 ASSERT_EQ(args.deviceId, DEVICE_ID);
3220 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3221 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3222 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3223 ASSERT_EQ(args.values, values);
3224 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3225}
3226
Michael Wrightd02c5b62014-02-10 15:10:22 -08003227// --- KeyboardInputMapperTest ---
3228
3229class KeyboardInputMapperTest : public InputMapperTest {
3230protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003231 const std::string UNIQUE_ID = "local:0";
3232
3233 void prepareDisplay(int32_t orientation);
3234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003235 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003236 int32_t originalKeyCode, int32_t rotatedKeyCode,
3237 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238};
3239
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003240/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3241 * orientation.
3242 */
3243void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003244 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3245 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003246}
3247
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003248void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003249 int32_t originalScanCode, int32_t originalKeyCode,
3250 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251 NotifyKeyArgs args;
3252
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3255 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3256 ASSERT_EQ(originalScanCode, args.scanCode);
3257 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003258 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3263 ASSERT_EQ(originalScanCode, args.scanCode);
3264 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003265 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266}
3267
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 KeyboardInputMapper& mapper =
3270 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3271 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003273 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274}
3275
3276TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3277 const int32_t USAGE_A = 0x070004;
3278 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003279 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3280 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003281 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3282 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3283 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003285 KeyboardInputMapper& mapper =
3286 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3287 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003288 // Initial metastate to AMETA_NONE.
3289 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3290 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
3292 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 NotifyKeyArgs args;
3295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3296 ASSERT_EQ(DEVICE_ID, args.deviceId);
3297 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3298 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3299 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3300 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3301 ASSERT_EQ(KEY_HOME, args.scanCode);
3302 ASSERT_EQ(AMETA_NONE, args.metaState);
3303 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3304 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3305 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3306
3307 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003308 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3310 ASSERT_EQ(DEVICE_ID, args.deviceId);
3311 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3312 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3313 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3314 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3315 ASSERT_EQ(KEY_HOME, args.scanCode);
3316 ASSERT_EQ(AMETA_NONE, args.metaState);
3317 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3318 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3319 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3320
3321 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3325 ASSERT_EQ(DEVICE_ID, args.deviceId);
3326 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3327 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3329 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3330 ASSERT_EQ(0, args.scanCode);
3331 ASSERT_EQ(AMETA_NONE, args.metaState);
3332 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3333 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3334 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3335
3336 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3338 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3340 ASSERT_EQ(DEVICE_ID, args.deviceId);
3341 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3342 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3344 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3345 ASSERT_EQ(0, args.scanCode);
3346 ASSERT_EQ(AMETA_NONE, args.metaState);
3347 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3348 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3349 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3350
3351 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3355 ASSERT_EQ(DEVICE_ID, args.deviceId);
3356 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3357 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3358 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3359 ASSERT_EQ(0, args.keyCode);
3360 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3361 ASSERT_EQ(AMETA_NONE, args.metaState);
3362 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3363 ASSERT_EQ(0U, args.policyFlags);
3364 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3365
3366 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3368 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 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 + 1, args.eventTime);
3373 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3374 ASSERT_EQ(0, args.keyCode);
3375 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3376 ASSERT_EQ(AMETA_NONE, args.metaState);
3377 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3378 ASSERT_EQ(0U, args.policyFlags);
3379 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3380}
3381
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382/**
3383 * Ensure that the readTime is set to the time when the EV_KEY is received.
3384 */
3385TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3387
3388 KeyboardInputMapper& mapper =
3389 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3391 NotifyKeyArgs args;
3392
3393 // Key down
3394 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(12, args.readTime);
3397
3398 // Key up
3399 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(15, args.readTime);
3402}
3403
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3406 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003407 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3408 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3409 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
3412 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
arthurhungc903df12020-08-11 15:08:42 +08003415 // Initial metastate to AMETA_NONE.
3416 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3417 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418
3419 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 NotifyKeyArgs args;
3422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003425 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003426
3427 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003428 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432
3433 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003434 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
3439 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3442 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003444 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445}
3446
3447TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003448 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3449 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3450 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 KeyboardInputMapper& mapper =
3454 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3455 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003457 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3459 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3460 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3461 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3462 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3463 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3464 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3465 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3466}
3467
3468TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 KeyboardInputMapper& mapper =
3476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003479 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003480 ASSERT_NO_FATAL_FAILURE(
3481 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3482 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3483 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3484 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3485 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3486 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3487 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003489 clearViewports();
3490 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003491 ASSERT_NO_FATAL_FAILURE(
3492 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3493 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3494 AKEYCODE_DPAD_UP, DISPLAY_ID));
3495 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3496 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3497 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3498 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003500 clearViewports();
3501 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003502 ASSERT_NO_FATAL_FAILURE(
3503 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3504 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3505 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3506 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3507 AKEYCODE_DPAD_UP, DISPLAY_ID));
3508 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3509 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003511 clearViewports();
3512 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003513 ASSERT_NO_FATAL_FAILURE(
3514 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3515 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3516 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3517 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3518 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3519 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3520 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
3522 // Special case: if orientation changes while key is down, we still emit the same keycode
3523 // in the key up as we did in the key down.
3524 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003525 clearViewports();
3526 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3530 ASSERT_EQ(KEY_UP, args.scanCode);
3531 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3532
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003533 clearViewports();
3534 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3537 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3538 ASSERT_EQ(KEY_UP, args.scanCode);
3539 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3540}
3541
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003542TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3543 // If the keyboard is not orientation aware,
3544 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003547 KeyboardInputMapper& mapper =
3548 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3549 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003550 NotifyKeyArgs args;
3551
3552 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3557 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3558
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003559 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3564 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3565}
3566
3567TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3568 // If the keyboard is orientation aware,
3569 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003571
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003572 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003573 KeyboardInputMapper& mapper =
3574 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3575 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003576 NotifyKeyArgs args;
3577
3578 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3579 // ^--- already checked by the previous test
3580
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003581 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003582 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3587 ASSERT_EQ(DISPLAY_ID, args.displayId);
3588
3589 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003590 clearViewports();
3591 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003592 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3597 ASSERT_EQ(newDisplayId, args.displayId);
3598}
3599
Michael Wrightd02c5b62014-02-10 15:10:22 -08003600TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 KeyboardInputMapper& mapper =
3602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003605 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003606 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003607
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003608 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610}
3611
3612TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003613 KeyboardInputMapper& mapper =
3614 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3615 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003617 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003618 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003620 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003621 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622}
3623
3624TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 KeyboardInputMapper& mapper =
3626 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3627 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003629 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630
3631 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3632 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_TRUE(flags[0]);
3635 ASSERT_FALSE(flags[1]);
3636}
3637
3638TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003639 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3640 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3641 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3642 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 KeyboardInputMapper& mapper =
3647 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003649 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003650 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3651 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652
3653 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3655 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3656 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657
3658 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003661 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3662 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3663 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
3666 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3670 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3671 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673
3674 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003677 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3678 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3679 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003680 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681
3682 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003685 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3686 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3687 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003689
3690 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003693 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3694 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3695 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697
3698 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705}
3706
Chris Yea52ade12020-08-27 16:49:20 -07003707TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3708 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3709 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3710 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3711 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3712
3713 KeyboardInputMapper& mapper =
3714 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3715 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3716
3717 // Initial metastate should be AMETA_NONE as no meta keys added.
3718 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3719 // Meta state should be AMETA_NONE after reset
3720 mapper.reset(ARBITRARY_TIME);
3721 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3722 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3723 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3724 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3725
3726 NotifyKeyArgs args;
3727 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3730 ASSERT_EQ(AMETA_NONE, args.metaState);
3731 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3732 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3733 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3734
3735 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(AMETA_NONE, args.metaState);
3739 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3740 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3741 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3742}
3743
Arthur Hung2c9a3342019-07-23 14:18:59 +08003744TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3745 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003750
3751 // keyboard 2.
3752 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003753 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003754 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003756 std::shared_ptr<InputDevice> device2 =
3757 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3758 Flags<InputDeviceClass>(0));
3759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003760 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3761 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3762 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3763 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003764
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003765 KeyboardInputMapper& mapper =
3766 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3767 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003772 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3773 device2->reset(ARBITRARY_TIME);
3774
3775 // Prepared displays and associated info.
3776 constexpr uint8_t hdmi1 = 0;
3777 constexpr uint8_t hdmi2 = 1;
3778 const std::string SECONDARY_UNIQUE_ID = "local:1";
3779
3780 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3781 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3782
3783 // No associated display viewport found, should disable the device.
3784 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3785 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3786 ASSERT_FALSE(device2->isEnabled());
3787
3788 // Prepare second display.
3789 constexpr int32_t newDisplayId = 2;
3790 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003791 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003792 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003793 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003794 // Default device will reconfigure above, need additional reconfiguration for another device.
3795 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3796 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3797
3798 // Device should be enabled after the associated display is found.
3799 ASSERT_TRUE(mDevice->isEnabled());
3800 ASSERT_TRUE(device2->isEnabled());
3801
3802 // Test pad key events
3803 ASSERT_NO_FATAL_FAILURE(
3804 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3805 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3806 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3807 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3808 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3809 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3810 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3811
3812 ASSERT_NO_FATAL_FAILURE(
3813 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3814 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3815 AKEYCODE_DPAD_RIGHT, newDisplayId));
3816 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3817 AKEYCODE_DPAD_DOWN, newDisplayId));
3818 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3819 AKEYCODE_DPAD_LEFT, newDisplayId));
3820}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
arthurhungc903df12020-08-11 15:08:42 +08003822TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3823 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3824 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3825 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3826 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3829
3830 KeyboardInputMapper& mapper =
3831 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3832 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3833 // Initial metastate to AMETA_NONE.
3834 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3835 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3836
3837 // Initialization should have turned all of the lights off.
3838 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3839 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3840 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3841
3842 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003845 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3846 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3847
3848 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003851 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3852 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3853
3854 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003857 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3858 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3859
3860 mFakeEventHub->removeDevice(EVENTHUB_ID);
3861 mReader->loopOnce();
3862
3863 // keyboard 2 should default toggle keys.
3864 const std::string USB2 = "USB2";
3865 const std::string DEVICE_NAME2 = "KEYBOARD2";
3866 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3867 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3868 std::shared_ptr<InputDevice> device2 =
3869 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3870 Flags<InputDeviceClass>(0));
3871 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3872 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3873 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3874 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3875 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3876 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3877
arthurhung6fe95782020-10-05 22:41:16 +08003878 KeyboardInputMapper& mapper2 =
3879 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3880 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003881 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3882 device2->reset(ARBITRARY_TIME);
3883
3884 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3885 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3886 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003887 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3888 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003889}
3890
Arthur Hungcb40a002021-08-03 14:31:01 +00003891TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3893 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3894 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3895
3896 // Suppose we have two mappers. (DPAD + KEYBOARD)
3897 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3898 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3899 KeyboardInputMapper& mapper =
3900 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3901 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3902 // Initialize metastate to AMETA_NUM_LOCK_ON.
3903 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3904 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3905
3906 mReader->toggleCapsLockState(DEVICE_ID);
3907 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3908}
3909
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003910// --- KeyboardInputMapperTest_ExternalDevice ---
3911
3912class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3913protected:
Chris Yea52ade12020-08-27 16:49:20 -07003914 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003915};
3916
3917TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003918 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3919 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003920
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003921 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3922 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3923 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3924 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003926 KeyboardInputMapper& mapper =
3927 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3928 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003929
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003931 NotifyKeyArgs args;
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3933 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3934
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3937 ASSERT_EQ(uint32_t(0), args.policyFlags);
3938
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3941 ASSERT_EQ(uint32_t(0), args.policyFlags);
3942
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3945 ASSERT_EQ(uint32_t(0), args.policyFlags);
3946
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3949 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3950
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3953 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3954}
3955
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003956TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003957 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003958
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003962
Powei Fengd041c5d2019-05-03 17:11:33 -07003963 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003964 KeyboardInputMapper& mapper =
3965 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003967
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003969 NotifyKeyArgs args;
3970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3971 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3972
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003973 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3975 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3976
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3979 ASSERT_EQ(uint32_t(0), args.policyFlags);
3980
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3983 ASSERT_EQ(uint32_t(0), args.policyFlags);
3984
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003985 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3987 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3988
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3991 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3992}
3993
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994// --- CursorInputMapperTest ---
3995
3996class CursorInputMapperTest : public InputMapperTest {
3997protected:
3998 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3999
Michael Wright17db18e2020-06-26 20:51:44 +01004000 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001
Chris Yea52ade12020-08-27 16:49:20 -07004002 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 InputMapperTest::SetUp();
4004
Michael Wright17db18e2020-06-26 20:51:44 +01004005 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004006 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007 }
4008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004009 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4010 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004011
4012 void prepareDisplay(int32_t orientation) {
4013 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004014 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004015 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4016 orientation, uniqueId, NO_PORT, viewportType);
4017 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004018
4019 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4020 float pressure) {
4021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4022 0.0f, 0.0f, 0.0f, EPSILON));
4023 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024};
4025
4026const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004028void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4029 int32_t originalY, int32_t rotatedX,
4030 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 NotifyMotionArgs args;
4032
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004038 ASSERT_NO_FATAL_FAILURE(
4039 assertCursorPointerCoords(args.pointerCoords[0],
4040 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4041 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042}
4043
4044TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049}
4050
4051TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004055 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056}
4057
4058TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061
4062 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
4065 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004066 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4067 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4069 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4070
4071 // When the bounds are set, then there should be a valid motion range.
4072 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4073
4074 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004075 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076
4077 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4078 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4079 1, 800 - 1, 0.0f, 0.0f));
4080 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4081 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4082 2, 480 - 1, 0.0f, 0.0f));
4083 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4084 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4085 0.0f, 1.0f, 0.0f, 0.0f));
4086}
4087
4088TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004090 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
4092 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004093 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
4095 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4096 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4097 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4098 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4099 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4100 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4101 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4102 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4103 0.0f, 1.0f, 0.0f, 0.0f));
4104}
4105
4106TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004108 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
arthurhungdcef2dc2020-08-11 14:47:50 +08004110 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
4112 NotifyMotionArgs args;
4113
4114 // Button press.
4115 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4119 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4120 ASSERT_EQ(DEVICE_ID, args.deviceId);
4121 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4122 ASSERT_EQ(uint32_t(0), args.policyFlags);
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4124 ASSERT_EQ(0, args.flags);
4125 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4127 ASSERT_EQ(0, args.edgeFlags);
4128 ASSERT_EQ(uint32_t(1), args.pointerCount);
4129 ASSERT_EQ(0, args.pointerProperties[0].id);
4130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004131 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4133 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4134 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4135
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4137 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4138 ASSERT_EQ(DEVICE_ID, args.deviceId);
4139 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4140 ASSERT_EQ(uint32_t(0), args.policyFlags);
4141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4142 ASSERT_EQ(0, args.flags);
4143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4144 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4145 ASSERT_EQ(0, args.edgeFlags);
4146 ASSERT_EQ(uint32_t(1), args.pointerCount);
4147 ASSERT_EQ(0, args.pointerProperties[0].id);
4148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004149 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004150 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4151 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4152 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4153
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4156 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4158 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4159 ASSERT_EQ(DEVICE_ID, args.deviceId);
4160 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4161 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4163 ASSERT_EQ(0, args.flags);
4164 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4165 ASSERT_EQ(0, args.buttonState);
4166 ASSERT_EQ(0, args.edgeFlags);
4167 ASSERT_EQ(uint32_t(1), args.pointerCount);
4168 ASSERT_EQ(0, args.pointerProperties[0].id);
4169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004170 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004171 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4172 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4173 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4174
4175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4176 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4177 ASSERT_EQ(DEVICE_ID, args.deviceId);
4178 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4179 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4181 ASSERT_EQ(0, args.flags);
4182 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4183 ASSERT_EQ(0, args.buttonState);
4184 ASSERT_EQ(0, args.edgeFlags);
4185 ASSERT_EQ(uint32_t(1), args.pointerCount);
4186 ASSERT_EQ(0, args.pointerProperties[0].id);
4187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4190 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4191 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4192}
4193
4194TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004196 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
4198 NotifyMotionArgs args;
4199
4200 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004205 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4206 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4207 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208
4209 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004214 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4215 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216}
4217
4218TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 NotifyMotionArgs args;
4223
4224 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004233 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004240 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004244 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245}
4246
4247TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004249 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250
4251 NotifyMotionArgs args;
4252
4253 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004260 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4261 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4262 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004266 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4267 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4268 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004269
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4277 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4278 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004289 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290}
4291
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004292TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004294 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4295 // need to be rotated.
4296 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004297 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004299 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4302 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4303 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4304 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4305 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4306 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4308}
4309
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004310TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004312 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4313 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004314 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004315
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004316 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4324 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4325
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004326 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004327 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4328 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4329 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4330 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4331 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4332 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4333 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4334 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004336 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4338 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4339 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4340 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4341 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4342 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4343 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4344 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4345
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004346 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004347 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4348 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4349 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4350 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4351 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4352 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4353 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4354 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355}
4356
4357TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004359 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360
4361 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4362 mFakePointerController->setPosition(100, 200);
4363 mFakePointerController->setButtonState(0);
4364
4365 NotifyMotionArgs motionArgs;
4366 NotifyKeyArgs keyArgs;
4367
4368 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4374 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004375 ASSERT_NO_FATAL_FAILURE(
4376 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4380 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4381 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(0, motionArgs.buttonState);
4390 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(
4392 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393
4394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396 ASSERT_EQ(0, motionArgs.buttonState);
4397 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004398 ASSERT_NO_FATAL_FAILURE(
4399 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004400
4401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403 ASSERT_EQ(0, motionArgs.buttonState);
4404 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004405 ASSERT_NO_FATAL_FAILURE(
4406 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
4408 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4413 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4414 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4415 motionArgs.buttonState);
4416 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4417 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004418 ASSERT_NO_FATAL_FAILURE(
4419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4423 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4424 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4425 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004426 ASSERT_NO_FATAL_FAILURE(
4427 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004428
4429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4430 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4431 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4432 motionArgs.buttonState);
4433 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4434 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004435 ASSERT_NO_FATAL_FAILURE(
4436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4443 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004444 ASSERT_NO_FATAL_FAILURE(
4445 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446
4447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4450 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004451 ASSERT_NO_FATAL_FAILURE(
4452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4458 ASSERT_EQ(0, motionArgs.buttonState);
4459 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004460 ASSERT_NO_FATAL_FAILURE(
4461 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_EQ(0, motionArgs.buttonState);
4467 ASSERT_EQ(0, mFakePointerController->getButtonState());
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(
4470 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4473 ASSERT_EQ(0, motionArgs.buttonState);
4474 ASSERT_EQ(0, mFakePointerController->getButtonState());
4475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004476 ASSERT_NO_FATAL_FAILURE(
4477 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
4479 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4484 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4489 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(
4491 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4495 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4496 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004497 ASSERT_NO_FATAL_FAILURE(
4498 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_EQ(0, motionArgs.buttonState);
4505 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004506 ASSERT_NO_FATAL_FAILURE(
4507 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511 ASSERT_EQ(0, motionArgs.buttonState);
4512 ASSERT_EQ(0, mFakePointerController->getButtonState());
4513
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004514 ASSERT_NO_FATAL_FAILURE(
4515 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4518 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4519
4520 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4524 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4525 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004526
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_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4530 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004533
4534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4535 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4536 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4537 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004538 ASSERT_NO_FATAL_FAILURE(
4539 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.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_SIDE, 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);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004549
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004554 ASSERT_NO_FATAL_FAILURE(
4555 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4558 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4559 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4560
4561 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4566 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004567
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4571 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(
4573 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4577 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4578 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004579 ASSERT_NO_FATAL_FAILURE(
4580 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_EQ(0, motionArgs.buttonState);
4587 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004588 ASSERT_NO_FATAL_FAILURE(
4589 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4593 ASSERT_EQ(0, motionArgs.buttonState);
4594 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004595 ASSERT_NO_FATAL_FAILURE(
4596 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4601
4602 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004608
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004610 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004613 ASSERT_NO_FATAL_FAILURE(
4614 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4619 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620 ASSERT_NO_FATAL_FAILURE(
4621 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004626 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627 ASSERT_EQ(0, motionArgs.buttonState);
4628 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004629 ASSERT_NO_FATAL_FAILURE(
4630 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004631
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4633 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4634 ASSERT_EQ(0, motionArgs.buttonState);
4635 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004636 ASSERT_NO_FATAL_FAILURE(
4637 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4641 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4642}
4643
4644TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004646 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647
4648 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4649 mFakePointerController->setPosition(100, 200);
4650 mFakePointerController->setButtonState(0);
4651
4652 NotifyMotionArgs args;
4653
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004658 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4661 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 +01004662 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004663}
4664
4665TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004666 addConfigurationProperty("cursor.mode", "pointer");
4667 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004668 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004669
4670 NotifyDeviceResetArgs resetArgs;
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4672 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4673 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4674
4675 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4676 mFakePointerController->setPosition(100, 200);
4677 mFakePointerController->setButtonState(0);
4678
4679 NotifyMotionArgs args;
4680
4681 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4686 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4689 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 +01004690 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004691
4692 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4696 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4699 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4701 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4704 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4705
4706 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004707 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4708 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4710 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4711 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4713 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4715 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4718 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4719
4720 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4725 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4728 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 +01004729 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004730
4731 // Disable pointer capture and check that the device generation got bumped
4732 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004733 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004734 mFakePolicy->setPointerCapture(false);
4735 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004736 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4739 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4740 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4741
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4746 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4749 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 +01004750 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751}
4752
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004753TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004754 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004755
Garfield Tan888a6a42020-01-09 11:39:16 -08004756 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004757 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004758 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4759 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004760 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4761 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004762 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4763 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4764
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004765 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4766 mFakePointerController->setPosition(100, 200);
4767 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004768
4769 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4774 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4775 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4777 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 +01004778 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004779 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4780}
4781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782// --- TouchInputMapperTest ---
4783
4784class TouchInputMapperTest : public InputMapperTest {
4785protected:
4786 static const int32_t RAW_X_MIN;
4787 static const int32_t RAW_X_MAX;
4788 static const int32_t RAW_Y_MIN;
4789 static const int32_t RAW_Y_MAX;
4790 static const int32_t RAW_TOUCH_MIN;
4791 static const int32_t RAW_TOUCH_MAX;
4792 static const int32_t RAW_TOOL_MIN;
4793 static const int32_t RAW_TOOL_MAX;
4794 static const int32_t RAW_PRESSURE_MIN;
4795 static const int32_t RAW_PRESSURE_MAX;
4796 static const int32_t RAW_ORIENTATION_MIN;
4797 static const int32_t RAW_ORIENTATION_MAX;
4798 static const int32_t RAW_DISTANCE_MIN;
4799 static const int32_t RAW_DISTANCE_MAX;
4800 static const int32_t RAW_TILT_MIN;
4801 static const int32_t RAW_TILT_MAX;
4802 static const int32_t RAW_ID_MIN;
4803 static const int32_t RAW_ID_MAX;
4804 static const int32_t RAW_SLOT_MIN;
4805 static const int32_t RAW_SLOT_MAX;
4806 static const float X_PRECISION;
4807 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004808 static const float X_PRECISION_VIRTUAL;
4809 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810
4811 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004812 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813
4814 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4815
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004816 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004817 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004818
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 enum Axes {
4820 POSITION = 1 << 0,
4821 TOUCH = 1 << 1,
4822 TOOL = 1 << 2,
4823 PRESSURE = 1 << 3,
4824 ORIENTATION = 1 << 4,
4825 MINOR = 1 << 5,
4826 ID = 1 << 6,
4827 DISTANCE = 1 << 7,
4828 TILT = 1 << 8,
4829 SLOT = 1 << 9,
4830 TOOL_TYPE = 1 << 10,
4831 };
4832
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004833 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4834 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004835 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004837 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838 int32_t toRawX(float displayX);
4839 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004840 int32_t toRotatedRawX(float displayX);
4841 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004842 float toCookedX(float rawX, float rawY);
4843 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004845 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004847 float toDisplayY(int32_t rawY, int32_t displayHeight);
4848
Michael Wrightd02c5b62014-02-10 15:10:22 -08004849};
4850
4851const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4852const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4853const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4854const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4855const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4856const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4857const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4858const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004859const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4860const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4862const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4863const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4864const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4865const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4866const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4867const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4868const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4869const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4870const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4871const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4872const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004873const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4874 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4875const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4876 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004877const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4878 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879
4880const float TouchInputMapperTest::GEOMETRIC_SCALE =
4881 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4882 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4883
4884const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4885 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4886 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4887};
4888
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004889void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004890 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4891 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004892}
4893
4894void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4895 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4896 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
Santos Cordonfa5cf462017-04-05 10:37:00 -07004899void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004900 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4901 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4902 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004903}
4904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004906 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4907 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4908 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4909 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910}
4911
Jason Gerecke489fda82012-09-07 17:19:40 -07004912void TouchInputMapperTest::prepareLocationCalibration() {
4913 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4914}
4915
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916int32_t TouchInputMapperTest::toRawX(float displayX) {
4917 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4918}
4919
4920int32_t TouchInputMapperTest::toRawY(float displayY) {
4921 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4922}
4923
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004924int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4925 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4926}
4927
4928int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4929 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4930}
4931
Jason Gerecke489fda82012-09-07 17:19:40 -07004932float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4933 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4934 return rawX;
4935}
4936
4937float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4938 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4939 return rawY;
4940}
4941
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004943 return toDisplayX(rawX, DISPLAY_WIDTH);
4944}
4945
4946float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4947 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948}
4949
4950float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004951 return toDisplayY(rawY, DISPLAY_HEIGHT);
4952}
4953
4954float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4955 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956}
4957
4958
4959// --- SingleTouchInputMapperTest ---
4960
4961class SingleTouchInputMapperTest : public TouchInputMapperTest {
4962protected:
4963 void prepareButtons();
4964 void prepareAxes(int axes);
4965
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4967 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4968 void processUp(SingleTouchInputMapper& mappery);
4969 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4970 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4971 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4972 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4973 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4974 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975};
4976
4977void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004978 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979}
4980
4981void SingleTouchInputMapperTest::prepareAxes(int axes) {
4982 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985 }
4986 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4988 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 }
4990 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004991 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4992 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993 }
4994 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004995 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4996 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 }
4998 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004999 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 }
5002}
5003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005004void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021}
5022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005023void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5024 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005028void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030}
5031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005032void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5033 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036}
5037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5039 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041}
5042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005043void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045}
5046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 prepareButtons();
5049 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005050 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005052 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053}
5054
5055TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005056 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5057 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 prepareButtons();
5059 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005062 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063}
5064
5065TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 prepareButtons();
5067 prepareAxes(POSITION);
5068 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005069 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005071 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072}
5073
5074TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 prepareButtons();
5076 prepareAxes(POSITION);
5077 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005078 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005080 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
5083TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 addConfigurationProperty("touch.deviceType", "touchScreen");
5085 prepareDisplay(DISPLAY_ORIENTATION_0);
5086 prepareButtons();
5087 prepareAxes(POSITION);
5088 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005092 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093
5094 // Virtual key is down.
5095 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5096 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5097 processDown(mapper, x, y);
5098 processSync(mapper);
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102
5103 // Virtual key is up.
5104 processUp(mapper);
5105 processSync(mapper);
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005108 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109}
5110
5111TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 addConfigurationProperty("touch.deviceType", "touchScreen");
5113 prepareDisplay(DISPLAY_ORIENTATION_0);
5114 prepareButtons();
5115 prepareAxes(POSITION);
5116 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118
5119 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121
5122 // Virtual key is down.
5123 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5124 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5125 processDown(mapper, x, y);
5126 processSync(mapper);
5127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
5131 // Virtual key is up.
5132 processUp(mapper);
5133 processSync(mapper);
5134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005136 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137}
5138
5139TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005140 addConfigurationProperty("touch.deviceType", "touchScreen");
5141 prepareDisplay(DISPLAY_ORIENTATION_0);
5142 prepareButtons();
5143 prepareAxes(POSITION);
5144 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146
5147 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5148 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 ASSERT_TRUE(flags[0]);
5151 ASSERT_FALSE(flags[1]);
5152}
5153
5154TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155 addConfigurationProperty("touch.deviceType", "touchScreen");
5156 prepareDisplay(DISPLAY_ORIENTATION_0);
5157 prepareButtons();
5158 prepareAxes(POSITION);
5159 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005160 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161
arthurhungdcef2dc2020-08-11 14:47:50 +08005162 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163
5164 NotifyKeyArgs args;
5165
5166 // Press virtual key.
5167 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5168 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5169 processDown(mapper, x, y);
5170 processSync(mapper);
5171
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5173 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5174 ASSERT_EQ(DEVICE_ID, args.deviceId);
5175 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5176 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5178 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5179 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5180 ASSERT_EQ(KEY_HOME, args.scanCode);
5181 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5182 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5183
5184 // Release virtual key.
5185 processUp(mapper);
5186 processSync(mapper);
5187
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5189 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5190 ASSERT_EQ(DEVICE_ID, args.deviceId);
5191 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5192 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5194 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5195 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5196 ASSERT_EQ(KEY_HOME, args.scanCode);
5197 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5198 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5199
5200 // Should not have sent any motions.
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5202}
5203
5204TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 addConfigurationProperty("touch.deviceType", "touchScreen");
5206 prepareDisplay(DISPLAY_ORIENTATION_0);
5207 prepareButtons();
5208 prepareAxes(POSITION);
5209 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005210 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211
arthurhungdcef2dc2020-08-11 14:47:50 +08005212 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213
5214 NotifyKeyArgs keyArgs;
5215
5216 // Press virtual key.
5217 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5218 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5219 processDown(mapper, x, y);
5220 processSync(mapper);
5221
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5223 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5224 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5225 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5226 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5228 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5229 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5230 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5231 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5232 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5233
5234 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5235 // into the display area.
5236 y -= 100;
5237 processMove(mapper, x, y);
5238 processSync(mapper);
5239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5241 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5242 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5243 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5244 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5245 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5246 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5247 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5248 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5249 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5251 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5252
5253 NotifyMotionArgs motionArgs;
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5256 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5258 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5259 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5260 ASSERT_EQ(0, motionArgs.flags);
5261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5262 ASSERT_EQ(0, motionArgs.buttonState);
5263 ASSERT_EQ(0, motionArgs.edgeFlags);
5264 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5265 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5268 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5269 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5270 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5272
5273 // Keep moving out of bounds. Should generate a pointer move.
5274 y -= 50;
5275 processMove(mapper, x, y);
5276 processSync(mapper);
5277
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5280 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5281 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5282 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5284 ASSERT_EQ(0, motionArgs.flags);
5285 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5286 ASSERT_EQ(0, motionArgs.buttonState);
5287 ASSERT_EQ(0, motionArgs.edgeFlags);
5288 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5289 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5292 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5293 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5294 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5295 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5296
5297 // Release out of bounds. Should generate a pointer up.
5298 processUp(mapper);
5299 processSync(mapper);
5300
5301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5307 ASSERT_EQ(0, motionArgs.flags);
5308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5309 ASSERT_EQ(0, motionArgs.buttonState);
5310 ASSERT_EQ(0, motionArgs.edgeFlags);
5311 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5315 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5316 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5317 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5319
5320 // Should not have sent any more keys or motions.
5321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5323}
5324
5325TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 addConfigurationProperty("touch.deviceType", "touchScreen");
5327 prepareDisplay(DISPLAY_ORIENTATION_0);
5328 prepareButtons();
5329 prepareAxes(POSITION);
5330 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005331 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332
arthurhungdcef2dc2020-08-11 14:47:50 +08005333 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334
5335 NotifyMotionArgs motionArgs;
5336
5337 // Initially go down out of bounds.
5338 int32_t x = -10;
5339 int32_t y = -10;
5340 processDown(mapper, x, y);
5341 processSync(mapper);
5342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5344
5345 // Move into the display area. Should generate a pointer down.
5346 x = 50;
5347 y = 75;
5348 processMove(mapper, x, y);
5349 processSync(mapper);
5350
5351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5353 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5354 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5355 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5357 ASSERT_EQ(0, motionArgs.flags);
5358 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5359 ASSERT_EQ(0, motionArgs.buttonState);
5360 ASSERT_EQ(0, motionArgs.edgeFlags);
5361 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5362 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5365 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5369
5370 // Release. Should generate a pointer up.
5371 processUp(mapper);
5372 processSync(mapper);
5373
5374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5379 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5380 ASSERT_EQ(0, motionArgs.flags);
5381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5382 ASSERT_EQ(0, motionArgs.buttonState);
5383 ASSERT_EQ(0, motionArgs.edgeFlags);
5384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5388 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5392
5393 // Should not have sent any more keys or motions.
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5396}
5397
Santos Cordonfa5cf462017-04-05 10:37:00 -07005398TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005399 addConfigurationProperty("touch.deviceType", "touchScreen");
5400 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5401
5402 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5403 prepareButtons();
5404 prepareAxes(POSITION);
5405 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005407
arthurhungdcef2dc2020-08-11 14:47:50 +08005408 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005409
5410 NotifyMotionArgs motionArgs;
5411
5412 // Down.
5413 int32_t x = 100;
5414 int32_t y = 125;
5415 processDown(mapper, x, y);
5416 processSync(mapper);
5417
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5420 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5421 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5424 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5425 ASSERT_EQ(0, motionArgs.flags);
5426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5427 ASSERT_EQ(0, motionArgs.buttonState);
5428 ASSERT_EQ(0, motionArgs.edgeFlags);
5429 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5433 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5434 1, 0, 0, 0, 0, 0, 0, 0));
5435 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5436 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5437 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5438
5439 // Move.
5440 x += 50;
5441 y += 75;
5442 processMove(mapper, x, y);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5447 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5448 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5449 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5450 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5452 ASSERT_EQ(0, motionArgs.flags);
5453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5454 ASSERT_EQ(0, motionArgs.buttonState);
5455 ASSERT_EQ(0, motionArgs.edgeFlags);
5456 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5457 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5460 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5461 1, 0, 0, 0, 0, 0, 0, 0));
5462 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5463 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5465
5466 // Up.
5467 processUp(mapper);
5468 processSync(mapper);
5469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5472 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5473 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5477 ASSERT_EQ(0, motionArgs.flags);
5478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5479 ASSERT_EQ(0, motionArgs.buttonState);
5480 ASSERT_EQ(0, motionArgs.edgeFlags);
5481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5485 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5486 1, 0, 0, 0, 0, 0, 0, 0));
5487 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5488 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5490
5491 // Should not have sent any more keys or motions.
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5494}
5495
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497 addConfigurationProperty("touch.deviceType", "touchScreen");
5498 prepareDisplay(DISPLAY_ORIENTATION_0);
5499 prepareButtons();
5500 prepareAxes(POSITION);
5501 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005502 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503
arthurhungdcef2dc2020-08-11 14:47:50 +08005504 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505
5506 NotifyMotionArgs motionArgs;
5507
5508 // Down.
5509 int32_t x = 100;
5510 int32_t y = 125;
5511 processDown(mapper, x, y);
5512 processSync(mapper);
5513
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5516 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5517 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5518 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5520 ASSERT_EQ(0, motionArgs.flags);
5521 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5522 ASSERT_EQ(0, motionArgs.buttonState);
5523 ASSERT_EQ(0, motionArgs.edgeFlags);
5524 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5525 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5528 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5529 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5530 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5532
5533 // Move.
5534 x += 50;
5535 y += 75;
5536 processMove(mapper, x, y);
5537 processSync(mapper);
5538
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5541 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5543 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5545 ASSERT_EQ(0, motionArgs.flags);
5546 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5547 ASSERT_EQ(0, motionArgs.buttonState);
5548 ASSERT_EQ(0, motionArgs.edgeFlags);
5549 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5553 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5554 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5555 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5557
5558 // Up.
5559 processUp(mapper);
5560 processSync(mapper);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5568 ASSERT_EQ(0, motionArgs.flags);
5569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5570 ASSERT_EQ(0, motionArgs.buttonState);
5571 ASSERT_EQ(0, motionArgs.edgeFlags);
5572 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5576 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5580
5581 // Should not have sent any more keys or motions.
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5584}
5585
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005586TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 addConfigurationProperty("touch.deviceType", "touchScreen");
5588 prepareButtons();
5589 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005590 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5591 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005592 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005593
5594 NotifyMotionArgs args;
5595
5596 // Rotation 90.
5597 prepareDisplay(DISPLAY_ORIENTATION_90);
5598 processDown(mapper, toRawX(50), toRawY(75));
5599 processSync(mapper);
5600
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5602 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5603 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5604
5605 processUp(mapper);
5606 processSync(mapper);
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5608}
5609
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005610TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 addConfigurationProperty("touch.deviceType", "touchScreen");
5612 prepareButtons();
5613 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005614 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5615 // orientation-aware are affected by display rotation.
5616 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618
5619 NotifyMotionArgs args;
5620
5621 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005622 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 prepareDisplay(DISPLAY_ORIENTATION_0);
5624 processDown(mapper, toRawX(50), toRawY(75));
5625 processSync(mapper);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5628 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5629 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5630
5631 processUp(mapper);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5634
5635 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005636 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005637 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005638 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 processSync(mapper);
5640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5642 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5643 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5644
5645 processUp(mapper);
5646 processSync(mapper);
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5648
5649 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005650 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 prepareDisplay(DISPLAY_ORIENTATION_180);
5652 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5653 processSync(mapper);
5654
5655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5656 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5657 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5658
5659 processUp(mapper);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5662
5663 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005664 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005666 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 processSync(mapper);
5668
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5670 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5671 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5672
5673 processUp(mapper);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5676}
5677
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005678TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5679 addConfigurationProperty("touch.deviceType", "touchScreen");
5680 prepareButtons();
5681 prepareAxes(POSITION);
5682 addConfigurationProperty("touch.orientationAware", "1");
5683 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5684 clearViewports();
5685 prepareDisplay(DISPLAY_ORIENTATION_0);
5686 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5687 NotifyMotionArgs args;
5688
5689 // Orientation 0.
5690 processDown(mapper, toRawX(50), toRawY(75));
5691 processSync(mapper);
5692
5693 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5694 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5695 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5696
5697 processUp(mapper);
5698 processSync(mapper);
5699 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5700}
5701
5702TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5703 addConfigurationProperty("touch.deviceType", "touchScreen");
5704 prepareButtons();
5705 prepareAxes(POSITION);
5706 addConfigurationProperty("touch.orientationAware", "1");
5707 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5708 clearViewports();
5709 prepareDisplay(DISPLAY_ORIENTATION_0);
5710 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5711 NotifyMotionArgs args;
5712
5713 // Orientation 90.
5714 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5715 processSync(mapper);
5716
5717 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5718 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5719 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5720
5721 processUp(mapper);
5722 processSync(mapper);
5723 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5724}
5725
5726TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5727 addConfigurationProperty("touch.deviceType", "touchScreen");
5728 prepareButtons();
5729 prepareAxes(POSITION);
5730 addConfigurationProperty("touch.orientationAware", "1");
5731 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5732 clearViewports();
5733 prepareDisplay(DISPLAY_ORIENTATION_0);
5734 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5735 NotifyMotionArgs args;
5736
5737 // Orientation 180.
5738 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5739 processSync(mapper);
5740
5741 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5742 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5743 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5744
5745 processUp(mapper);
5746 processSync(mapper);
5747 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5748}
5749
5750TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5751 addConfigurationProperty("touch.deviceType", "touchScreen");
5752 prepareButtons();
5753 prepareAxes(POSITION);
5754 addConfigurationProperty("touch.orientationAware", "1");
5755 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5756 clearViewports();
5757 prepareDisplay(DISPLAY_ORIENTATION_0);
5758 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5759 NotifyMotionArgs args;
5760
5761 // Orientation 270.
5762 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5763 processSync(mapper);
5764
5765 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5766 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5767 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5768
5769 processUp(mapper);
5770 processSync(mapper);
5771 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5772}
5773
5774TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5775 addConfigurationProperty("touch.deviceType", "touchScreen");
5776 prepareButtons();
5777 prepareAxes(POSITION);
5778 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5779 // orientation-aware are affected by display rotation.
5780 addConfigurationProperty("touch.orientationAware", "0");
5781 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5782 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5783
5784 NotifyMotionArgs args;
5785
5786 // Orientation 90, Rotation 0.
5787 clearViewports();
5788 prepareDisplay(DISPLAY_ORIENTATION_0);
5789 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5790 processSync(mapper);
5791
5792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5793 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5794 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5795
5796 processUp(mapper);
5797 processSync(mapper);
5798 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5799
5800 // Orientation 90, Rotation 90.
5801 clearViewports();
5802 prepareDisplay(DISPLAY_ORIENTATION_90);
5803 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5804 processSync(mapper);
5805
5806 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5807 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5808 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5809
5810 processUp(mapper);
5811 processSync(mapper);
5812 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5813
5814 // Orientation 90, Rotation 180.
5815 clearViewports();
5816 prepareDisplay(DISPLAY_ORIENTATION_180);
5817 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5818 processSync(mapper);
5819
5820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5821 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5822 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5823
5824 processUp(mapper);
5825 processSync(mapper);
5826 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5827
5828 // Orientation 90, Rotation 270.
5829 clearViewports();
5830 prepareDisplay(DISPLAY_ORIENTATION_270);
5831 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5832 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5833 processSync(mapper);
5834
5835 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5836 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5837 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5838
5839 processUp(mapper);
5840 processSync(mapper);
5841 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5842}
5843
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 addConfigurationProperty("touch.deviceType", "touchScreen");
5846 prepareDisplay(DISPLAY_ORIENTATION_0);
5847 prepareButtons();
5848 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005849 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850
5851 // These calculations are based on the input device calibration documentation.
5852 int32_t rawX = 100;
5853 int32_t rawY = 200;
5854 int32_t rawPressure = 10;
5855 int32_t rawToolMajor = 12;
5856 int32_t rawDistance = 2;
5857 int32_t rawTiltX = 30;
5858 int32_t rawTiltY = 110;
5859
5860 float x = toDisplayX(rawX);
5861 float y = toDisplayY(rawY);
5862 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5863 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5864 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5865 float distance = float(rawDistance);
5866
5867 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5868 float tiltScale = M_PI / 180;
5869 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5870 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5871 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5872 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5873
5874 processDown(mapper, rawX, rawY);
5875 processPressure(mapper, rawPressure);
5876 processToolMajor(mapper, rawToolMajor);
5877 processDistance(mapper, rawDistance);
5878 processTilt(mapper, rawTiltX, rawTiltY);
5879 processSync(mapper);
5880
5881 NotifyMotionArgs args;
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5884 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5885 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5886}
5887
Jason Gerecke489fda82012-09-07 17:19:40 -07005888TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005889 addConfigurationProperty("touch.deviceType", "touchScreen");
5890 prepareDisplay(DISPLAY_ORIENTATION_0);
5891 prepareLocationCalibration();
5892 prepareButtons();
5893 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005894 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005895
5896 int32_t rawX = 100;
5897 int32_t rawY = 200;
5898
5899 float x = toDisplayX(toCookedX(rawX, rawY));
5900 float y = toDisplayY(toCookedY(rawX, rawY));
5901
5902 processDown(mapper, rawX, rawY);
5903 processSync(mapper);
5904
5905 NotifyMotionArgs args;
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5908 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5909}
5910
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 addConfigurationProperty("touch.deviceType", "touchScreen");
5913 prepareDisplay(DISPLAY_ORIENTATION_0);
5914 prepareButtons();
5915 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005916 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917
5918 NotifyMotionArgs motionArgs;
5919 NotifyKeyArgs keyArgs;
5920
5921 processDown(mapper, 100, 200);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5925 ASSERT_EQ(0, motionArgs.buttonState);
5926
5927 // press BTN_LEFT, release BTN_LEFT
5928 processKey(mapper, BTN_LEFT, 1);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5932 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5933
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5936 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5937
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 processKey(mapper, BTN_LEFT, 0);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5949 processKey(mapper, BTN_RIGHT, 1);
5950 processKey(mapper, BTN_MIDDLE, 1);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5954 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5955 motionArgs.buttonState);
5956
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5959 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5963 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5964 motionArgs.buttonState);
5965
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 processKey(mapper, BTN_RIGHT, 0);
5967 processSync(mapper);
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005974 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975
5976 processKey(mapper, BTN_MIDDLE, 0);
5977 processSync(mapper);
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005981
5982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005984 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 // press BTN_BACK, release BTN_BACK
5987 processKey(mapper, BTN_BACK, 1);
5988 processSync(mapper);
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5990 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5991 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5996
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5999 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
6001 processKey(mapper, BTN_BACK, 0);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(0, motionArgs.buttonState);
6010
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6012 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6013 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6014
6015 // press BTN_SIDE, release BTN_SIDE
6016 processKey(mapper, BTN_SIDE, 1);
6017 processSync(mapper);
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6019 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6020 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006024 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6028 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029
6030 processKey(mapper, BTN_SIDE, 0);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035
6036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038 ASSERT_EQ(0, motionArgs.buttonState);
6039
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6043
6044 // press BTN_FORWARD, release BTN_FORWARD
6045 processKey(mapper, BTN_FORWARD, 1);
6046 processSync(mapper);
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6048 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6049 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006053 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6054
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6057 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058
6059 processKey(mapper, BTN_FORWARD, 0);
6060 processSync(mapper);
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006067 ASSERT_EQ(0, motionArgs.buttonState);
6068
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6070 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6072
6073 // press BTN_EXTRA, release BTN_EXTRA
6074 processKey(mapper, BTN_EXTRA, 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_FORWARD, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087
6088 processKey(mapper, BTN_EXTRA, 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_FORWARD, keyArgs.keyCode);
6101
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6103
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104 // press BTN_STYLUS, release BTN_STYLUS
6105 processKey(mapper, BTN_STYLUS, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6110
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6113 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114
6115 processKey(mapper, BTN_STYLUS, 0);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006119 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006120
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006123 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006124
6125 // press BTN_STYLUS2, release BTN_STYLUS2
6126 processKey(mapper, BTN_STYLUS2, 1);
6127 processSync(mapper);
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6134 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006135
6136 processKey(mapper, BTN_STYLUS2, 0);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006141
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145
6146 // release touch
6147 processUp(mapper);
6148 processSync(mapper);
6149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6151 ASSERT_EQ(0, motionArgs.buttonState);
6152}
6153
6154TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 addConfigurationProperty("touch.deviceType", "touchScreen");
6156 prepareDisplay(DISPLAY_ORIENTATION_0);
6157 prepareButtons();
6158 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006159 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006160
6161 NotifyMotionArgs motionArgs;
6162
6163 // default tool type is finger
6164 processDown(mapper, 100, 200);
6165 processSync(mapper);
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6169
6170 // eraser
6171 processKey(mapper, BTN_TOOL_RUBBER, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6176
6177 // stylus
6178 processKey(mapper, BTN_TOOL_RUBBER, 0);
6179 processKey(mapper, BTN_TOOL_PEN, 1);
6180 processSync(mapper);
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6184
6185 // brush
6186 processKey(mapper, BTN_TOOL_PEN, 0);
6187 processKey(mapper, BTN_TOOL_BRUSH, 1);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6192
6193 // pencil
6194 processKey(mapper, BTN_TOOL_BRUSH, 0);
6195 processKey(mapper, BTN_TOOL_PENCIL, 1);
6196 processSync(mapper);
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6200
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006201 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 processKey(mapper, BTN_TOOL_PENCIL, 0);
6203 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6208
6209 // mouse
6210 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6211 processKey(mapper, BTN_TOOL_MOUSE, 1);
6212 processSync(mapper);
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6216
6217 // lens
6218 processKey(mapper, BTN_TOOL_MOUSE, 0);
6219 processKey(mapper, BTN_TOOL_LENS, 1);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6224
6225 // double-tap
6226 processKey(mapper, BTN_TOOL_LENS, 0);
6227 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6228 processSync(mapper);
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6232
6233 // triple-tap
6234 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6235 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240
6241 // quad-tap
6242 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6243 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6248
6249 // finger
6250 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6251 processKey(mapper, BTN_TOOL_FINGER, 1);
6252 processSync(mapper);
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6256
6257 // stylus trumps finger
6258 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6263
6264 // eraser trumps stylus
6265 processKey(mapper, BTN_TOOL_RUBBER, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6270
6271 // mouse trumps eraser
6272 processKey(mapper, BTN_TOOL_MOUSE, 1);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6277
6278 // back to default tool type
6279 processKey(mapper, BTN_TOOL_MOUSE, 0);
6280 processKey(mapper, BTN_TOOL_RUBBER, 0);
6281 processKey(mapper, BTN_TOOL_PEN, 0);
6282 processKey(mapper, BTN_TOOL_FINGER, 0);
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_FINGER, motionArgs.pointerProperties[0].toolType);
6287}
6288
6289TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 addConfigurationProperty("touch.deviceType", "touchScreen");
6291 prepareDisplay(DISPLAY_ORIENTATION_0);
6292 prepareButtons();
6293 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006294 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006295 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006296
6297 NotifyMotionArgs motionArgs;
6298
6299 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6300 processKey(mapper, BTN_TOOL_FINGER, 1);
6301 processMove(mapper, 100, 200);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6306 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6307
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6311 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6312
6313 // move a little
6314 processMove(mapper, 150, 250);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6319 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6320
6321 // down when BTN_TOUCH is pressed, pressure defaults to 1
6322 processKey(mapper, BTN_TOUCH, 1);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6327 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6332 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6333
6334 // up when BTN_TOUCH is released, hover restored
6335 processKey(mapper, BTN_TOUCH, 0);
6336 processSync(mapper);
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6340 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6341
6342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6346
6347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6348 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6350 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6351
6352 // exit hover when pointer goes away
6353 processKey(mapper, BTN_TOOL_FINGER, 0);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6358 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6359}
6360
6361TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 addConfigurationProperty("touch.deviceType", "touchScreen");
6363 prepareDisplay(DISPLAY_ORIENTATION_0);
6364 prepareButtons();
6365 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006366 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 NotifyMotionArgs motionArgs;
6369
6370 // initially hovering because pressure is 0
6371 processDown(mapper, 100, 200);
6372 processPressure(mapper, 0);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6377 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6382 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6383
6384 // move a little
6385 processMove(mapper, 150, 250);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6390 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6391
6392 // down when pressure is non-zero
6393 processPressure(mapper, RAW_PRESSURE_MAX);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6404
6405 // up when pressure becomes 0, hover restored
6406 processPressure(mapper, 0);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6422
6423 // exit hover when pointer goes away
6424 processUp(mapper);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6429 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6430}
6431
Prabir Pradhan5632d622021-09-06 07:57:20 -07006432// --- TouchDisplayProjectionTest ---
6433
6434class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6435public:
6436 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6437 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6438 // rotated equivalent of the given un-rotated physical display bounds.
6439 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6440 uint32_t inverseRotationFlags;
6441 auto width = DISPLAY_WIDTH;
6442 auto height = DISPLAY_HEIGHT;
6443 switch (orientation) {
6444 case DISPLAY_ORIENTATION_90:
6445 inverseRotationFlags = ui::Transform::ROT_270;
6446 std::swap(width, height);
6447 break;
6448 case DISPLAY_ORIENTATION_180:
6449 inverseRotationFlags = ui::Transform::ROT_180;
6450 break;
6451 case DISPLAY_ORIENTATION_270:
6452 inverseRotationFlags = ui::Transform::ROT_90;
6453 std::swap(width, height);
6454 break;
6455 case DISPLAY_ORIENTATION_0:
6456 inverseRotationFlags = ui::Transform::ROT_0;
6457 break;
6458 default:
6459 FAIL() << "Invalid orientation: " << orientation;
6460 }
6461
6462 const ui::Transform rotation(inverseRotationFlags, width, height);
6463 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6464
6465 std::optional<DisplayViewport> internalViewport =
6466 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6467 DisplayViewport& v = *internalViewport;
6468 v.displayId = DISPLAY_ID;
6469 v.orientation = orientation;
6470
6471 v.logicalLeft = 0;
6472 v.logicalTop = 0;
6473 v.logicalRight = 100;
6474 v.logicalBottom = 100;
6475
6476 v.physicalLeft = rotatedPhysicalDisplay.left;
6477 v.physicalTop = rotatedPhysicalDisplay.top;
6478 v.physicalRight = rotatedPhysicalDisplay.right;
6479 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6480
6481 v.deviceWidth = width;
6482 v.deviceHeight = height;
6483
6484 v.isActive = true;
6485 v.uniqueId = UNIQUE_ID;
6486 v.type = ViewportType::INTERNAL;
6487 mFakePolicy->updateViewport(v);
6488 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6489 }
6490
6491 void assertReceivedMove(const Point& point) {
6492 NotifyMotionArgs motionArgs;
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6495 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6497 1, 0, 0, 0, 0, 0, 0, 0));
6498 }
6499};
6500
6501TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6502 addConfigurationProperty("touch.deviceType", "touchScreen");
6503 prepareDisplay(DISPLAY_ORIENTATION_0);
6504
6505 prepareButtons();
6506 prepareAxes(POSITION);
6507 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6508
6509 NotifyMotionArgs motionArgs;
6510
6511 // Configure the DisplayViewport such that the logical display maps to a subsection of
6512 // the display panel called the physical display. Here, the physical display is bounded by the
6513 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6514 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6515 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6516 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6517
6518 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6519 DISPLAY_ORIENTATION_270}) {
6520 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6521
6522 // Touches outside the physical display should be ignored, and should not generate any
6523 // events. Ensure touches at the following points that lie outside of the physical display
6524 // area do not generate any events.
6525 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6526 processDown(mapper, toRawX(point.x), toRawY(point.y));
6527 processSync(mapper);
6528 processUp(mapper);
6529 processSync(mapper);
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6531 << "Unexpected event generated for touch outside physical display at point: "
6532 << point.x << ", " << point.y;
6533 }
6534 }
6535}
6536
6537TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6538 addConfigurationProperty("touch.deviceType", "touchScreen");
6539 prepareDisplay(DISPLAY_ORIENTATION_0);
6540
6541 prepareButtons();
6542 prepareAxes(POSITION);
6543 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6544
6545 NotifyMotionArgs motionArgs;
6546
6547 // Configure the DisplayViewport such that the logical display maps to a subsection of
6548 // the display panel called the physical display. Here, the physical display is bounded by the
6549 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6550 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6551
6552 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6553 DISPLAY_ORIENTATION_270}) {
6554 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6555
6556 // Touches that start outside the physical display should be ignored until it enters the
6557 // physical display bounds, at which point it should generate a down event. Start a touch at
6558 // the point (5, 100), which is outside the physical display bounds.
6559 static const Point kOutsidePoint{5, 100};
6560 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6561 processSync(mapper);
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6563
6564 // Move the touch into the physical display area. This should generate a pointer down.
6565 processMove(mapper, toRawX(11), toRawY(21));
6566 processSync(mapper);
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6569 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6570 ASSERT_NO_FATAL_FAILURE(
6571 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6572
6573 // Move the touch inside the physical display area. This should generate a pointer move.
6574 processMove(mapper, toRawX(69), toRawY(159));
6575 processSync(mapper);
6576 assertReceivedMove({69, 159});
6577
6578 // Move outside the physical display area. Since the pointer is already down, this should
6579 // now continue generating events.
6580 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6581 processSync(mapper);
6582 assertReceivedMove(kOutsidePoint);
6583
6584 // Release. This should generate a pointer up.
6585 processUp(mapper);
6586 processSync(mapper);
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6588 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6590 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6591
6592 // Ensure no more events were generated.
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6595 }
6596}
6597
Michael Wrightd02c5b62014-02-10 15:10:22 -08006598// --- MultiTouchInputMapperTest ---
6599
6600class MultiTouchInputMapperTest : public TouchInputMapperTest {
6601protected:
6602 void prepareAxes(int axes);
6603
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006604 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6605 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6606 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6607 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6608 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6609 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6610 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6611 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6612 void processId(MultiTouchInputMapper& mapper, int32_t id);
6613 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6614 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6615 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6616 void processMTSync(MultiTouchInputMapper& mapper);
6617 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006618};
6619
6620void MultiTouchInputMapperTest::prepareAxes(int axes) {
6621 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006622 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6623 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006624 }
6625 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006626 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6627 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006628 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006629 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6630 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006631 }
6632 }
6633 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006634 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6635 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006636 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006637 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006638 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639 }
6640 }
6641 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006642 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6643 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006644 }
6645 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006646 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6647 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648 }
6649 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6651 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652 }
6653 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6655 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006656 }
6657 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006658 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6659 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006660 }
6661 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006662 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006663 }
6664}
6665
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006666void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6667 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006670}
6671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006672void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6673 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006675}
6676
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006677void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6678 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006679 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680}
6681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006682void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006684}
6685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006686void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688}
6689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006690void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6691 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006693}
6694
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006695void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006697}
6698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006699void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006701}
6702
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006703void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705}
6706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006707void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709}
6710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006711void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713}
6714
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006715void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6716 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718}
6719
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006720void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006722}
6723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006724void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006726}
6727
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006729 addConfigurationProperty("touch.deviceType", "touchScreen");
6730 prepareDisplay(DISPLAY_ORIENTATION_0);
6731 prepareAxes(POSITION);
6732 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734
arthurhungdcef2dc2020-08-11 14:47:50 +08006735 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006736
6737 NotifyMotionArgs motionArgs;
6738
6739 // Two fingers down at once.
6740 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6741 processPosition(mapper, x1, y1);
6742 processMTSync(mapper);
6743 processPosition(mapper, x2, y2);
6744 processMTSync(mapper);
6745 processSync(mapper);
6746
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6749 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6750 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6751 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6753 ASSERT_EQ(0, motionArgs.flags);
6754 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6755 ASSERT_EQ(0, motionArgs.buttonState);
6756 ASSERT_EQ(0, motionArgs.edgeFlags);
6757 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6758 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6759 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6761 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6762 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6763 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6765
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6770 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006771 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772 ASSERT_EQ(0, motionArgs.flags);
6773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6774 ASSERT_EQ(0, motionArgs.buttonState);
6775 ASSERT_EQ(0, motionArgs.edgeFlags);
6776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6782 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6785 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6786 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6787 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6788
6789 // Move.
6790 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6791 processPosition(mapper, x1, y1);
6792 processMTSync(mapper);
6793 processPosition(mapper, x2, y2);
6794 processMTSync(mapper);
6795 processSync(mapper);
6796
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6799 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6800 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6801 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6803 ASSERT_EQ(0, motionArgs.flags);
6804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6805 ASSERT_EQ(0, motionArgs.buttonState);
6806 ASSERT_EQ(0, motionArgs.edgeFlags);
6807 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6810 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6813 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6815 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6816 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6817 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6818 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6819
6820 // First finger up.
6821 x2 += 15; y2 -= 20;
6822 processPosition(mapper, x2, y2);
6823 processMTSync(mapper);
6824 processSync(mapper);
6825
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6828 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6829 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6830 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006831 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006832 ASSERT_EQ(0, motionArgs.flags);
6833 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6834 ASSERT_EQ(0, motionArgs.buttonState);
6835 ASSERT_EQ(0, motionArgs.edgeFlags);
6836 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6837 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6839 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6842 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6844 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6845 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6846 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6848
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6851 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6852 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6853 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6855 ASSERT_EQ(0, motionArgs.flags);
6856 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6857 ASSERT_EQ(0, motionArgs.buttonState);
6858 ASSERT_EQ(0, motionArgs.edgeFlags);
6859 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6860 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6863 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6864 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6865 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6867
6868 // Move.
6869 x2 += 20; y2 -= 25;
6870 processPosition(mapper, x2, y2);
6871 processMTSync(mapper);
6872 processSync(mapper);
6873
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6875 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6876 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6877 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6878 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6880 ASSERT_EQ(0, motionArgs.flags);
6881 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6882 ASSERT_EQ(0, motionArgs.buttonState);
6883 ASSERT_EQ(0, motionArgs.edgeFlags);
6884 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6885 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6888 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6889 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6890 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6891 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6892
6893 // New finger down.
6894 int32_t x3 = 700, y3 = 300;
6895 processPosition(mapper, x2, y2);
6896 processMTSync(mapper);
6897 processPosition(mapper, x3, y3);
6898 processMTSync(mapper);
6899 processSync(mapper);
6900
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6902 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6903 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6904 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6905 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006906 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 ASSERT_EQ(0, motionArgs.flags);
6908 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6909 ASSERT_EQ(0, motionArgs.buttonState);
6910 ASSERT_EQ(0, motionArgs.edgeFlags);
6911 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6912 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6913 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6914 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6917 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6918 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6919 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6920 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6921 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6922 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6923
6924 // Second finger up.
6925 x3 += 30; y3 -= 20;
6926 processPosition(mapper, x3, y3);
6927 processMTSync(mapper);
6928 processSync(mapper);
6929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6932 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6933 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6934 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006935 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 ASSERT_EQ(0, motionArgs.flags);
6937 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6938 ASSERT_EQ(0, motionArgs.buttonState);
6939 ASSERT_EQ(0, motionArgs.edgeFlags);
6940 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6941 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6943 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6946 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6948 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6949 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6950 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6952
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6955 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6956 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6957 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6959 ASSERT_EQ(0, motionArgs.flags);
6960 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6961 ASSERT_EQ(0, motionArgs.buttonState);
6962 ASSERT_EQ(0, motionArgs.edgeFlags);
6963 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6964 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6967 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6968 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6969 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6970 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6971
6972 // Last finger up.
6973 processMTSync(mapper);
6974 processSync(mapper);
6975
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6978 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6982 ASSERT_EQ(0, motionArgs.flags);
6983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6984 ASSERT_EQ(0, motionArgs.buttonState);
6985 ASSERT_EQ(0, motionArgs.edgeFlags);
6986 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6990 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6991 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6992 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6994
6995 // Should not have sent any more keys or motions.
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6998}
6999
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007000TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7001 addConfigurationProperty("touch.deviceType", "touchScreen");
7002 prepareDisplay(DISPLAY_ORIENTATION_0);
7003
7004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7005 /*fuzz*/ 0, /*resolution*/ 10);
7006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7007 /*fuzz*/ 0, /*resolution*/ 11);
7008 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7009 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7010 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7011 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7013 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7015 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7016
7017 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7018
7019 // X and Y axes
7020 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7021 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7022 // Touch major and minor
7023 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7024 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7025 // Tool major and minor
7026 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7027 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7028}
7029
7030TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7031 addConfigurationProperty("touch.deviceType", "touchScreen");
7032 prepareDisplay(DISPLAY_ORIENTATION_0);
7033
7034 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7035 /*fuzz*/ 0, /*resolution*/ 10);
7036 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7037 /*fuzz*/ 0, /*resolution*/ 11);
7038
7039 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7040
7041 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7042
7043 // Touch major and minor
7044 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7045 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7046 // Tool major and minor
7047 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7048 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7049}
7050
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052 addConfigurationProperty("touch.deviceType", "touchScreen");
7053 prepareDisplay(DISPLAY_ORIENTATION_0);
7054 prepareAxes(POSITION | ID);
7055 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007056 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057
arthurhungdcef2dc2020-08-11 14:47:50 +08007058 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059
7060 NotifyMotionArgs motionArgs;
7061
7062 // Two fingers down at once.
7063 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7064 processPosition(mapper, x1, y1);
7065 processId(mapper, 1);
7066 processMTSync(mapper);
7067 processPosition(mapper, x2, y2);
7068 processId(mapper, 2);
7069 processMTSync(mapper);
7070 processSync(mapper);
7071
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7073 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7074 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7078 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7079
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007081 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7083 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7085 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7088 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7090 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7091
7092 // Move.
7093 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7094 processPosition(mapper, x1, y1);
7095 processId(mapper, 1);
7096 processMTSync(mapper);
7097 processPosition(mapper, x2, y2);
7098 processId(mapper, 2);
7099 processMTSync(mapper);
7100 processSync(mapper);
7101
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7104 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7105 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7106 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7107 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7110 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7113
7114 // First finger up.
7115 x2 += 15; y2 -= 20;
7116 processPosition(mapper, x2, y2);
7117 processId(mapper, 2);
7118 processMTSync(mapper);
7119 processSync(mapper);
7120
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007122 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7124 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7126 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7129 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7131 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7132
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7135 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7136 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7139 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7140
7141 // Move.
7142 x2 += 20; y2 -= 25;
7143 processPosition(mapper, x2, y2);
7144 processId(mapper, 2);
7145 processMTSync(mapper);
7146 processSync(mapper);
7147
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7150 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7151 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7155
7156 // New finger down.
7157 int32_t x3 = 700, y3 = 300;
7158 processPosition(mapper, x2, y2);
7159 processId(mapper, 2);
7160 processMTSync(mapper);
7161 processPosition(mapper, x3, y3);
7162 processId(mapper, 3);
7163 processMTSync(mapper);
7164 processSync(mapper);
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007167 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7171 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7174 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7176 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7177
7178 // Second finger up.
7179 x3 += 30; y3 -= 20;
7180 processPosition(mapper, x3, y3);
7181 processId(mapper, 3);
7182 processMTSync(mapper);
7183 processSync(mapper);
7184
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007186 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7188 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7190 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7193 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7195 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7196
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7199 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7200 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7203 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7204
7205 // Last finger up.
7206 processMTSync(mapper);
7207 processSync(mapper);
7208
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7212 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7215 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7216
7217 // Should not have sent any more keys or motions.
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7220}
7221
7222TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 addConfigurationProperty("touch.deviceType", "touchScreen");
7224 prepareDisplay(DISPLAY_ORIENTATION_0);
7225 prepareAxes(POSITION | ID | SLOT);
7226 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007227 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228
arthurhungdcef2dc2020-08-11 14:47:50 +08007229 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230
7231 NotifyMotionArgs motionArgs;
7232
7233 // Two fingers down at once.
7234 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7235 processPosition(mapper, x1, y1);
7236 processId(mapper, 1);
7237 processSlot(mapper, 1);
7238 processPosition(mapper, x2, y2);
7239 processId(mapper, 2);
7240 processSync(mapper);
7241
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7244 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7245 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7248 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7249
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007251 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7255 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7260 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7261
7262 // Move.
7263 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7264 processSlot(mapper, 0);
7265 processPosition(mapper, x1, y1);
7266 processSlot(mapper, 1);
7267 processPosition(mapper, x2, y2);
7268 processSync(mapper);
7269
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7272 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7275 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7278 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7280 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7281
7282 // First finger up.
7283 x2 += 15; y2 -= 20;
7284 processSlot(mapper, 0);
7285 processId(mapper, -1);
7286 processSlot(mapper, 1);
7287 processPosition(mapper, x2, y2);
7288 processSync(mapper);
7289
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007291 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7293 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7295 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7298 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7300 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7304 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7305 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7309
7310 // Move.
7311 x2 += 20; y2 -= 25;
7312 processPosition(mapper, x2, y2);
7313 processSync(mapper);
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7317 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7318 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7321 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7322
7323 // New finger down.
7324 int32_t x3 = 700, y3 = 300;
7325 processPosition(mapper, x2, y2);
7326 processSlot(mapper, 0);
7327 processId(mapper, 3);
7328 processPosition(mapper, x3, y3);
7329 processSync(mapper);
7330
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007332 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7336 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7342
7343 // Second finger up.
7344 x3 += 30; y3 -= 20;
7345 processSlot(mapper, 1);
7346 processId(mapper, -1);
7347 processSlot(mapper, 0);
7348 processPosition(mapper, x3, y3);
7349 processSync(mapper);
7350
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007352 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7354 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7356 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7359 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7361 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7362
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7365 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7369 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7370
7371 // Last finger up.
7372 processId(mapper, -1);
7373 processSync(mapper);
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7377 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7378 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7381 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7382
7383 // Should not have sent any more keys or motions.
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7386}
7387
7388TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 addConfigurationProperty("touch.deviceType", "touchScreen");
7390 prepareDisplay(DISPLAY_ORIENTATION_0);
7391 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007392 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007393
7394 // These calculations are based on the input device calibration documentation.
7395 int32_t rawX = 100;
7396 int32_t rawY = 200;
7397 int32_t rawTouchMajor = 7;
7398 int32_t rawTouchMinor = 6;
7399 int32_t rawToolMajor = 9;
7400 int32_t rawToolMinor = 8;
7401 int32_t rawPressure = 11;
7402 int32_t rawDistance = 0;
7403 int32_t rawOrientation = 3;
7404 int32_t id = 5;
7405
7406 float x = toDisplayX(rawX);
7407 float y = toDisplayY(rawY);
7408 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7409 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7410 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7411 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7412 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7413 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7414 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7415 float distance = float(rawDistance);
7416
7417 processPosition(mapper, rawX, rawY);
7418 processTouchMajor(mapper, rawTouchMajor);
7419 processTouchMinor(mapper, rawTouchMinor);
7420 processToolMajor(mapper, rawToolMajor);
7421 processToolMinor(mapper, rawToolMinor);
7422 processPressure(mapper, rawPressure);
7423 processOrientation(mapper, rawOrientation);
7424 processDistance(mapper, rawDistance);
7425 processId(mapper, id);
7426 processMTSync(mapper);
7427 processSync(mapper);
7428
7429 NotifyMotionArgs args;
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7431 ASSERT_EQ(0, args.pointerProperties[0].id);
7432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7433 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7434 orientation, distance));
7435}
7436
7437TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007438 addConfigurationProperty("touch.deviceType", "touchScreen");
7439 prepareDisplay(DISPLAY_ORIENTATION_0);
7440 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7441 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007442 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443
7444 // These calculations are based on the input device calibration documentation.
7445 int32_t rawX = 100;
7446 int32_t rawY = 200;
7447 int32_t rawTouchMajor = 140;
7448 int32_t rawTouchMinor = 120;
7449 int32_t rawToolMajor = 180;
7450 int32_t rawToolMinor = 160;
7451
7452 float x = toDisplayX(rawX);
7453 float y = toDisplayY(rawY);
7454 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7455 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7456 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7457 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7458 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7459
7460 processPosition(mapper, rawX, rawY);
7461 processTouchMajor(mapper, rawTouchMajor);
7462 processTouchMinor(mapper, rawTouchMinor);
7463 processToolMajor(mapper, rawToolMajor);
7464 processToolMinor(mapper, rawToolMinor);
7465 processMTSync(mapper);
7466 processSync(mapper);
7467
7468 NotifyMotionArgs args;
7469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7471 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7472}
7473
7474TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475 addConfigurationProperty("touch.deviceType", "touchScreen");
7476 prepareDisplay(DISPLAY_ORIENTATION_0);
7477 prepareAxes(POSITION | TOUCH | TOOL);
7478 addConfigurationProperty("touch.size.calibration", "diameter");
7479 addConfigurationProperty("touch.size.scale", "10");
7480 addConfigurationProperty("touch.size.bias", "160");
7481 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483
7484 // These calculations are based on the input device calibration documentation.
7485 // Note: We only provide a single common touch/tool value because the device is assumed
7486 // not to emit separate values for each pointer (isSummed = 1).
7487 int32_t rawX = 100;
7488 int32_t rawY = 200;
7489 int32_t rawX2 = 150;
7490 int32_t rawY2 = 250;
7491 int32_t rawTouchMajor = 5;
7492 int32_t rawToolMajor = 8;
7493
7494 float x = toDisplayX(rawX);
7495 float y = toDisplayY(rawY);
7496 float x2 = toDisplayX(rawX2);
7497 float y2 = toDisplayY(rawY2);
7498 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7499 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7500 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7501
7502 processPosition(mapper, rawX, rawY);
7503 processTouchMajor(mapper, rawTouchMajor);
7504 processToolMajor(mapper, rawToolMajor);
7505 processMTSync(mapper);
7506 processPosition(mapper, rawX2, rawY2);
7507 processTouchMajor(mapper, rawTouchMajor);
7508 processToolMajor(mapper, rawToolMajor);
7509 processMTSync(mapper);
7510 processSync(mapper);
7511
7512 NotifyMotionArgs args;
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007517 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 ASSERT_EQ(size_t(2), args.pointerCount);
7519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7520 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7522 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7523}
7524
7525TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526 addConfigurationProperty("touch.deviceType", "touchScreen");
7527 prepareDisplay(DISPLAY_ORIENTATION_0);
7528 prepareAxes(POSITION | TOUCH | TOOL);
7529 addConfigurationProperty("touch.size.calibration", "area");
7530 addConfigurationProperty("touch.size.scale", "43");
7531 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533
7534 // These calculations are based on the input device calibration documentation.
7535 int32_t rawX = 100;
7536 int32_t rawY = 200;
7537 int32_t rawTouchMajor = 5;
7538 int32_t rawToolMajor = 8;
7539
7540 float x = toDisplayX(rawX);
7541 float y = toDisplayY(rawY);
7542 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7543 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7544 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7545
7546 processPosition(mapper, rawX, rawY);
7547 processTouchMajor(mapper, rawTouchMajor);
7548 processToolMajor(mapper, rawToolMajor);
7549 processMTSync(mapper);
7550 processSync(mapper);
7551
7552 NotifyMotionArgs args;
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7555 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7556}
7557
7558TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559 addConfigurationProperty("touch.deviceType", "touchScreen");
7560 prepareDisplay(DISPLAY_ORIENTATION_0);
7561 prepareAxes(POSITION | PRESSURE);
7562 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7563 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007565
Michael Wrightaa449c92017-12-13 21:21:43 +00007566 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007567 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007568 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7569 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7570 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7571
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 // These calculations are based on the input device calibration documentation.
7573 int32_t rawX = 100;
7574 int32_t rawY = 200;
7575 int32_t rawPressure = 60;
7576
7577 float x = toDisplayX(rawX);
7578 float y = toDisplayY(rawY);
7579 float pressure = float(rawPressure) * 0.01f;
7580
7581 processPosition(mapper, rawX, rawY);
7582 processPressure(mapper, rawPressure);
7583 processMTSync(mapper);
7584 processSync(mapper);
7585
7586 NotifyMotionArgs args;
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7589 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7590}
7591
7592TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007593 addConfigurationProperty("touch.deviceType", "touchScreen");
7594 prepareDisplay(DISPLAY_ORIENTATION_0);
7595 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007596 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597
7598 NotifyMotionArgs motionArgs;
7599 NotifyKeyArgs keyArgs;
7600
7601 processId(mapper, 1);
7602 processPosition(mapper, 100, 200);
7603 processSync(mapper);
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7606 ASSERT_EQ(0, motionArgs.buttonState);
7607
7608 // press BTN_LEFT, release BTN_LEFT
7609 processKey(mapper, BTN_LEFT, 1);
7610 processSync(mapper);
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7613 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7614
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7617 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7618
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 processKey(mapper, BTN_LEFT, 0);
7620 processSync(mapper);
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007624
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007627 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628
7629 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7630 processKey(mapper, BTN_RIGHT, 1);
7631 processKey(mapper, BTN_MIDDLE, 1);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7635 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7636 motionArgs.buttonState);
7637
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7640 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7644 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7645 motionArgs.buttonState);
7646
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647 processKey(mapper, BTN_RIGHT, 0);
7648 processSync(mapper);
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007652
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007655 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656
7657 processKey(mapper, BTN_MIDDLE, 0);
7658 processSync(mapper);
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007662
7663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007665 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666
7667 // press BTN_BACK, release BTN_BACK
7668 processKey(mapper, BTN_BACK, 1);
7669 processSync(mapper);
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7671 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7672 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007673
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007676 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7680 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007681
7682 processKey(mapper, BTN_BACK, 0);
7683 processSync(mapper);
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007690 ASSERT_EQ(0, motionArgs.buttonState);
7691
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7694 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7695
7696 // press BTN_SIDE, release BTN_SIDE
7697 processKey(mapper, BTN_SIDE, 1);
7698 processSync(mapper);
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7700 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007702
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007705 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7706
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7709 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710
7711 processKey(mapper, BTN_SIDE, 0);
7712 processSync(mapper);
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007719 ASSERT_EQ(0, motionArgs.buttonState);
7720
Michael Wrightd02c5b62014-02-10 15:10:22 -08007721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7722 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7723 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7724
7725 // press BTN_FORWARD, release BTN_FORWARD
7726 processKey(mapper, BTN_FORWARD, 1);
7727 processSync(mapper);
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7729 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7730 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007731
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007734 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7735
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7738 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739
7740 processKey(mapper, BTN_FORWARD, 0);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007744 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007745
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007748 ASSERT_EQ(0, motionArgs.buttonState);
7749
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7751 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7752 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7753
7754 // press BTN_EXTRA, release BTN_EXTRA
7755 processKey(mapper, BTN_EXTRA, 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_FORWARD, 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_FORWARD, 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_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768
7769 processKey(mapper, BTN_EXTRA, 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_FORWARD, keyArgs.keyCode);
7782
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7784
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785 // press BTN_STYLUS, release BTN_STYLUS
7786 processKey(mapper, BTN_STYLUS, 1);
7787 processSync(mapper);
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007790 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7791
7792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7794 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795
7796 processKey(mapper, BTN_STYLUS, 0);
7797 processSync(mapper);
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007801
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007804 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805
7806 // press BTN_STYLUS2, release BTN_STYLUS2
7807 processKey(mapper, BTN_STYLUS2, 1);
7808 processSync(mapper);
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007811 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7812
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7815 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816
7817 processKey(mapper, BTN_STYLUS2, 0);
7818 processSync(mapper);
7819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007825 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826
7827 // release touch
7828 processId(mapper, -1);
7829 processSync(mapper);
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7831 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7832 ASSERT_EQ(0, motionArgs.buttonState);
7833}
7834
7835TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 addConfigurationProperty("touch.deviceType", "touchScreen");
7837 prepareDisplay(DISPLAY_ORIENTATION_0);
7838 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840
7841 NotifyMotionArgs motionArgs;
7842
7843 // default tool type is finger
7844 processId(mapper, 1);
7845 processPosition(mapper, 100, 200);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7850
7851 // eraser
7852 processKey(mapper, BTN_TOOL_RUBBER, 1);
7853 processSync(mapper);
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7857
7858 // stylus
7859 processKey(mapper, BTN_TOOL_RUBBER, 0);
7860 processKey(mapper, BTN_TOOL_PEN, 1);
7861 processSync(mapper);
7862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7865
7866 // brush
7867 processKey(mapper, BTN_TOOL_PEN, 0);
7868 processKey(mapper, BTN_TOOL_BRUSH, 1);
7869 processSync(mapper);
7870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7873
7874 // pencil
7875 processKey(mapper, BTN_TOOL_BRUSH, 0);
7876 processKey(mapper, BTN_TOOL_PENCIL, 1);
7877 processSync(mapper);
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7881
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007882 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 processKey(mapper, BTN_TOOL_PENCIL, 0);
7884 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7885 processSync(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7889
7890 // mouse
7891 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7892 processKey(mapper, BTN_TOOL_MOUSE, 1);
7893 processSync(mapper);
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7897
7898 // lens
7899 processKey(mapper, BTN_TOOL_MOUSE, 0);
7900 processKey(mapper, BTN_TOOL_LENS, 1);
7901 processSync(mapper);
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7905
7906 // double-tap
7907 processKey(mapper, BTN_TOOL_LENS, 0);
7908 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7909 processSync(mapper);
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7913
7914 // triple-tap
7915 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7916 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7917 processSync(mapper);
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7921
7922 // quad-tap
7923 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7924 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7925 processSync(mapper);
7926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7929
7930 // finger
7931 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7932 processKey(mapper, BTN_TOOL_FINGER, 1);
7933 processSync(mapper);
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7937
7938 // stylus trumps finger
7939 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7944
7945 // eraser trumps stylus
7946 processKey(mapper, BTN_TOOL_RUBBER, 1);
7947 processSync(mapper);
7948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7951
7952 // mouse trumps eraser
7953 processKey(mapper, BTN_TOOL_MOUSE, 1);
7954 processSync(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7958
7959 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7960 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7965
7966 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7967 processToolType(mapper, MT_TOOL_PEN);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7972
7973 // back to default tool type
7974 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7975 processKey(mapper, BTN_TOOL_MOUSE, 0);
7976 processKey(mapper, BTN_TOOL_RUBBER, 0);
7977 processKey(mapper, BTN_TOOL_PEN, 0);
7978 processKey(mapper, BTN_TOOL_FINGER, 0);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7982 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7983}
7984
7985TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 addConfigurationProperty("touch.deviceType", "touchScreen");
7987 prepareDisplay(DISPLAY_ORIENTATION_0);
7988 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007989 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007990 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991
7992 NotifyMotionArgs motionArgs;
7993
7994 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7995 processId(mapper, 1);
7996 processPosition(mapper, 100, 200);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8001 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8002
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8006 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8007
8008 // move a little
8009 processPosition(mapper, 150, 250);
8010 processSync(mapper);
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8012 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8014 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8015
8016 // down when BTN_TOUCH is pressed, pressure defaults to 1
8017 processKey(mapper, BTN_TOUCH, 1);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8022 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8023
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8025 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8027 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8028
8029 // up when BTN_TOUCH is released, hover restored
8030 processKey(mapper, BTN_TOUCH, 0);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8033 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8035 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8041
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8043 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8045 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8046
8047 // exit hover when pointer goes away
8048 processId(mapper, -1);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8053 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 prepareDisplay(DISPLAY_ORIENTATION_0);
8059 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061
8062 NotifyMotionArgs motionArgs;
8063
8064 // initially hovering because pressure is 0
8065 processId(mapper, 1);
8066 processPosition(mapper, 100, 200);
8067 processPressure(mapper, 0);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8072 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8077 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8078
8079 // move a little
8080 processPosition(mapper, 150, 250);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8085 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8086
8087 // down when pressure becomes non-zero
8088 processPressure(mapper, RAW_PRESSURE_MAX);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8094
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8098 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8099
8100 // up when pressure becomes 0, hover restored
8101 processPressure(mapper, 0);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8106 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8111 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8116 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8117
8118 // exit hover when pointer goes away
8119 processId(mapper, -1);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8124 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8125}
8126
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008127/**
8128 * Set the input device port <--> display port associations, and check that the
8129 * events are routed to the display that matches the display port.
8130 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8131 */
8132TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008133 const std::string usb2 = "USB2";
8134 const uint8_t hdmi1 = 0;
8135 const uint8_t hdmi2 = 1;
8136 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008137 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008138
8139 addConfigurationProperty("touch.deviceType", "touchScreen");
8140 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008141 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008142
8143 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8144 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8145
8146 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8147 // for this input device is specified, and the matching viewport is not present,
8148 // the input device should be disabled (at the mapper level).
8149
8150 // Add viewport for display 2 on hdmi2
8151 prepareSecondaryDisplay(type, hdmi2);
8152 // Send a touch event
8153 processPosition(mapper, 100, 100);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8156
8157 // Add viewport for display 1 on hdmi1
8158 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8159 // Send a touch event again
8160 processPosition(mapper, 100, 100);
8161 processSync(mapper);
8162
8163 NotifyMotionArgs args;
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8165 ASSERT_EQ(DISPLAY_ID, args.displayId);
8166}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008168TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008169 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008170 std::shared_ptr<FakePointerController> fakePointerController =
8171 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008172 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008173 fakePointerController->setPosition(100, 200);
8174 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008175 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008176
Garfield Tan888a6a42020-01-09 11:39:16 -08008177 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008178 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008179
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008180 prepareDisplay(DISPLAY_ORIENTATION_0);
8181 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008182 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008183
8184 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008185 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008186
8187 NotifyMotionArgs motionArgs;
8188 processPosition(mapper, 100, 100);
8189 processSync(mapper);
8190
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8192 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8193 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8194}
8195
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008196/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008197 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8198 */
8199TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8200 addConfigurationProperty("touch.deviceType", "touchScreen");
8201 prepareAxes(POSITION);
8202 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8203
8204 prepareDisplay(DISPLAY_ORIENTATION_0);
8205 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8206 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8207 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8208 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8209
8210 NotifyMotionArgs args;
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8212 ASSERT_EQ(26, args.readTime);
8213
8214 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8215 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8216 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8217
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8219 ASSERT_EQ(33, args.readTime);
8220}
8221
8222/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008223 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8224 * events should not be delivered to the listener.
8225 */
8226TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8227 addConfigurationProperty("touch.deviceType", "touchScreen");
8228 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8229 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8230 ViewportType::INTERNAL);
8231 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8232 prepareAxes(POSITION);
8233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8234
8235 NotifyMotionArgs motionArgs;
8236 processPosition(mapper, 100, 100);
8237 processSync(mapper);
8238
8239 mFakeListener->assertNotifyMotionWasNotCalled();
8240}
8241
Garfield Tanc734e4f2021-01-15 20:01:39 -08008242TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8243 addConfigurationProperty("touch.deviceType", "touchScreen");
8244 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8245 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8246 ViewportType::INTERNAL);
8247 std::optional<DisplayViewport> optionalDisplayViewport =
8248 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8249 ASSERT_TRUE(optionalDisplayViewport.has_value());
8250 DisplayViewport displayViewport = *optionalDisplayViewport;
8251
8252 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8253 prepareAxes(POSITION);
8254 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8255
8256 // Finger down
8257 int32_t x = 100, y = 100;
8258 processPosition(mapper, x, y);
8259 processSync(mapper);
8260
8261 NotifyMotionArgs motionArgs;
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8264
8265 // Deactivate display viewport
8266 displayViewport.isActive = false;
8267 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8268 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8269
8270 // Finger move
8271 x += 10, y += 10;
8272 processPosition(mapper, x, y);
8273 processSync(mapper);
8274
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8276 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8277
8278 // Reactivate display viewport
8279 displayViewport.isActive = true;
8280 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8281 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8282
8283 // Finger move again
8284 x += 10, y += 10;
8285 processPosition(mapper, x, y);
8286 processSync(mapper);
8287
8288 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8289 // no pointer on the touch device.
8290 mFakeListener->assertNotifyMotionWasNotCalled();
8291}
8292
Arthur Hung7c645402019-01-25 17:45:42 +08008293TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8294 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008295 prepareAxes(POSITION | ID | SLOT);
8296 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008298
8299 // Create the second touch screen device, and enable multi fingers.
8300 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008301 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008302 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008303 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008304 std::shared_ptr<InputDevice> device2 =
8305 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8306 Flags<InputDeviceClass>(0));
8307
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008308 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8309 0 /*flat*/, 0 /*fuzz*/);
8310 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8311 0 /*flat*/, 0 /*fuzz*/);
8312 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8313 0 /*flat*/, 0 /*fuzz*/);
8314 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8315 0 /*flat*/, 0 /*fuzz*/);
8316 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8317 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8318 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008319
8320 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008321 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008322 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8323 device2->reset(ARBITRARY_TIME);
8324
8325 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008326 std::shared_ptr<FakePointerController> fakePointerController =
8327 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008328 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008329
8330 // Setup policy for associated displays and show touches.
8331 const uint8_t hdmi1 = 0;
8332 const uint8_t hdmi2 = 1;
8333 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8334 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8335 mFakePolicy->setShowTouches(true);
8336
8337 // Create displays.
8338 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008339 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008340
8341 // Default device will reconfigure above, need additional reconfiguration for another device.
8342 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008343 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008344
8345 // Two fingers down at default display.
8346 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8347 processPosition(mapper, x1, y1);
8348 processId(mapper, 1);
8349 processSlot(mapper, 1);
8350 processPosition(mapper, x2, y2);
8351 processId(mapper, 2);
8352 processSync(mapper);
8353
8354 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8355 fakePointerController->getSpots().find(DISPLAY_ID);
8356 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8357 ASSERT_EQ(size_t(2), iter->second.size());
8358
8359 // Two fingers down at second display.
8360 processPosition(mapper2, x1, y1);
8361 processId(mapper2, 1);
8362 processSlot(mapper2, 1);
8363 processPosition(mapper2, x2, y2);
8364 processId(mapper2, 2);
8365 processSync(mapper2);
8366
8367 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8368 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8369 ASSERT_EQ(size_t(2), iter->second.size());
8370}
8371
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008372TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008373 prepareAxes(POSITION);
8374 addConfigurationProperty("touch.deviceType", "touchScreen");
8375 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008376 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008377
8378 NotifyMotionArgs motionArgs;
8379 // Unrotated video frame
8380 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8381 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008382 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008383 processPosition(mapper, 100, 200);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(frames, motionArgs.videoFrames);
8387
8388 // Subsequent touch events should not have any videoframes
8389 // This is implemented separately in FakeEventHub,
8390 // but that should match the behaviour of TouchVideoDevice.
8391 processPosition(mapper, 200, 200);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8395}
8396
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008397TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008398 prepareAxes(POSITION);
8399 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008401 // Unrotated video frame
8402 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8403 NotifyMotionArgs motionArgs;
8404
8405 // Test all 4 orientations
8406 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008407 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8408 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8409 clearViewports();
8410 prepareDisplay(orientation);
8411 std::vector<TouchVideoFrame> frames{frame};
8412 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8413 processPosition(mapper, 100, 200);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(frames, motionArgs.videoFrames);
8417 }
8418}
8419
8420TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8421 prepareAxes(POSITION);
8422 addConfigurationProperty("touch.deviceType", "touchScreen");
8423 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8424 // orientation-aware are affected by display rotation.
8425 addConfigurationProperty("touch.orientationAware", "0");
8426 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8427 // Unrotated video frame
8428 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8429 NotifyMotionArgs motionArgs;
8430
8431 // Test all 4 orientations
8432 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008433 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8434 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8435 clearViewports();
8436 prepareDisplay(orientation);
8437 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008438 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008439 processPosition(mapper, 100, 200);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008442 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8443 // compared to the display. This is so that when the window transform (which contains the
8444 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8445 // window's coordinate space.
8446 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008447 ASSERT_EQ(frames, motionArgs.videoFrames);
8448 }
8449}
8450
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008451TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008452 prepareAxes(POSITION);
8453 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008455 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8456 // so mix these.
8457 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8458 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8459 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8460 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8461 NotifyMotionArgs motionArgs;
8462
8463 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008464 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008465 processPosition(mapper, 100, 200);
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008468 ASSERT_EQ(frames, motionArgs.videoFrames);
8469}
8470
8471TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8472 prepareAxes(POSITION);
8473 addConfigurationProperty("touch.deviceType", "touchScreen");
8474 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8475 // orientation-aware are affected by display rotation.
8476 addConfigurationProperty("touch.orientationAware", "0");
8477 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8478 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8479 // so mix these.
8480 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8481 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8482 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8483 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8484 NotifyMotionArgs motionArgs;
8485
8486 prepareDisplay(DISPLAY_ORIENTATION_90);
8487 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8488 processPosition(mapper, 100, 200);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8492 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8493 // compared to the display. This is so that when the window transform (which contains the
8494 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8495 // window's coordinate space.
8496 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8497 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008498 ASSERT_EQ(frames, motionArgs.videoFrames);
8499}
8500
Arthur Hung9da14732019-09-02 16:16:58 +08008501/**
8502 * If we had defined port associations, but the viewport is not ready, the touch device would be
8503 * expected to be disabled, and it should be enabled after the viewport has found.
8504 */
8505TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008506 constexpr uint8_t hdmi2 = 1;
8507 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008508 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008509
8510 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8511
8512 addConfigurationProperty("touch.deviceType", "touchScreen");
8513 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008514 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008515
8516 ASSERT_EQ(mDevice->isEnabled(), false);
8517
8518 // Add display on hdmi2, the device should be enabled and can receive touch event.
8519 prepareSecondaryDisplay(type, hdmi2);
8520 ASSERT_EQ(mDevice->isEnabled(), true);
8521
8522 // Send a touch event.
8523 processPosition(mapper, 100, 100);
8524 processSync(mapper);
8525
8526 NotifyMotionArgs args;
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8528 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8529}
8530
Arthur Hung421eb1c2020-01-16 00:09:42 +08008531TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008532 addConfigurationProperty("touch.deviceType", "touchScreen");
8533 prepareDisplay(DISPLAY_ORIENTATION_0);
8534 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008535 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008536
8537 NotifyMotionArgs motionArgs;
8538
8539 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8540 // finger down
8541 processId(mapper, 1);
8542 processPosition(mapper, x1, y1);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8547
8548 // finger move
8549 processId(mapper, 1);
8550 processPosition(mapper, x2, y2);
8551 processSync(mapper);
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8555
8556 // finger up.
8557 processId(mapper, -1);
8558 processSync(mapper);
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8562
8563 // new finger down
8564 processId(mapper, 1);
8565 processPosition(mapper, x3, y3);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8570}
8571
8572/**
arthurhungcc7f9802020-04-30 17:55:40 +08008573 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8574 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008575 */
arthurhungcc7f9802020-04-30 17:55:40 +08008576TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008577 addConfigurationProperty("touch.deviceType", "touchScreen");
8578 prepareDisplay(DISPLAY_ORIENTATION_0);
8579 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008581
8582 NotifyMotionArgs motionArgs;
8583
8584 // default tool type is finger
8585 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008586 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008587 processPosition(mapper, x1, y1);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8592
8593 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8594 processToolType(mapper, MT_TOOL_PALM);
8595 processSync(mapper);
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8598
8599 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008600 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008601 processPosition(mapper, x2, y2);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8604
8605 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008606 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8609
8610 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008611 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008612 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008613 processPosition(mapper, x3, y3);
8614 processSync(mapper);
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8618}
8619
arthurhungbf89a482020-04-17 17:37:55 +08008620/**
arthurhungcc7f9802020-04-30 17:55:40 +08008621 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8622 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008623 */
arthurhungcc7f9802020-04-30 17:55:40 +08008624TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008625 addConfigurationProperty("touch.deviceType", "touchScreen");
8626 prepareDisplay(DISPLAY_ORIENTATION_0);
8627 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8628 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8629
8630 NotifyMotionArgs motionArgs;
8631
8632 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008633 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8634 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008635 processPosition(mapper, x1, y1);
8636 processSync(mapper);
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8640
8641 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008642 processSlot(mapper, SECOND_SLOT);
8643 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008644 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008645 processSync(mapper);
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008647 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8649
8650 // If the tool type of the first finger changes to MT_TOOL_PALM,
8651 // we expect to receive ACTION_POINTER_UP with cancel flag.
8652 processSlot(mapper, FIRST_SLOT);
8653 processId(mapper, FIRST_TRACKING_ID);
8654 processToolType(mapper, MT_TOOL_PALM);
8655 processSync(mapper);
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008657 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008658 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8659
8660 // The following MOVE events of second finger should be processed.
8661 processSlot(mapper, SECOND_SLOT);
8662 processId(mapper, SECOND_TRACKING_ID);
8663 processPosition(mapper, x2 + 1, y2 + 1);
8664 processSync(mapper);
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8667 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8668
8669 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8670 // it. Second finger receive move.
8671 processSlot(mapper, FIRST_SLOT);
8672 processId(mapper, INVALID_TRACKING_ID);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8676 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8677
8678 // Second finger keeps moving.
8679 processSlot(mapper, SECOND_SLOT);
8680 processId(mapper, SECOND_TRACKING_ID);
8681 processPosition(mapper, x2 + 2, y2 + 2);
8682 processSync(mapper);
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8685 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8686
8687 // Second finger up.
8688 processId(mapper, INVALID_TRACKING_ID);
8689 processSync(mapper);
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8692 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8693}
8694
8695/**
8696 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8697 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8698 */
8699TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8700 addConfigurationProperty("touch.deviceType", "touchScreen");
8701 prepareDisplay(DISPLAY_ORIENTATION_0);
8702 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8703 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8704
8705 NotifyMotionArgs motionArgs;
8706
8707 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8708 // First finger down.
8709 processId(mapper, FIRST_TRACKING_ID);
8710 processPosition(mapper, x1, y1);
8711 processSync(mapper);
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8715
8716 // Second finger down.
8717 processSlot(mapper, SECOND_SLOT);
8718 processId(mapper, SECOND_TRACKING_ID);
8719 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008722 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8724
arthurhungcc7f9802020-04-30 17:55:40 +08008725 // If the tool type of the first finger changes to MT_TOOL_PALM,
8726 // we expect to receive ACTION_POINTER_UP with cancel flag.
8727 processSlot(mapper, FIRST_SLOT);
8728 processId(mapper, FIRST_TRACKING_ID);
8729 processToolType(mapper, MT_TOOL_PALM);
8730 processSync(mapper);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008732 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008733 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8734
8735 // Second finger keeps moving.
8736 processSlot(mapper, SECOND_SLOT);
8737 processId(mapper, SECOND_TRACKING_ID);
8738 processPosition(mapper, x2 + 1, y2 + 1);
8739 processSync(mapper);
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8742
8743 // second finger becomes palm, receive cancel due to only 1 finger is active.
8744 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008745 processToolType(mapper, MT_TOOL_PALM);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8749
arthurhungcc7f9802020-04-30 17:55:40 +08008750 // third finger down.
8751 processSlot(mapper, THIRD_SLOT);
8752 processId(mapper, THIRD_TRACKING_ID);
8753 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008754 processPosition(mapper, x3, y3);
8755 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008759 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8760
8761 // third finger move
8762 processId(mapper, THIRD_TRACKING_ID);
8763 processPosition(mapper, x3 + 1, y3 + 1);
8764 processSync(mapper);
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8767
8768 // first finger up, third finger receive move.
8769 processSlot(mapper, FIRST_SLOT);
8770 processId(mapper, INVALID_TRACKING_ID);
8771 processSync(mapper);
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8774 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8775
8776 // second finger up, third finger receive move.
8777 processSlot(mapper, SECOND_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 // third finger up.
8785 processSlot(mapper, THIRD_SLOT);
8786 processId(mapper, INVALID_TRACKING_ID);
8787 processSync(mapper);
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8789 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8790 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8791}
8792
8793/**
8794 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8795 * and the active finger could still be allowed to receive the events
8796 */
8797TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8798 addConfigurationProperty("touch.deviceType", "touchScreen");
8799 prepareDisplay(DISPLAY_ORIENTATION_0);
8800 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8802
8803 NotifyMotionArgs motionArgs;
8804
8805 // default tool type is finger
8806 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8807 processId(mapper, FIRST_TRACKING_ID);
8808 processPosition(mapper, x1, y1);
8809 processSync(mapper);
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8811 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8813
8814 // Second finger down.
8815 processSlot(mapper, SECOND_SLOT);
8816 processId(mapper, SECOND_TRACKING_ID);
8817 processPosition(mapper, x2, y2);
8818 processSync(mapper);
8819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008820 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8822
8823 // If the tool type of the second finger changes to MT_TOOL_PALM,
8824 // we expect to receive ACTION_POINTER_UP with cancel flag.
8825 processId(mapper, SECOND_TRACKING_ID);
8826 processToolType(mapper, MT_TOOL_PALM);
8827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008829 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008830 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8831
8832 // The following MOVE event should be processed.
8833 processSlot(mapper, FIRST_SLOT);
8834 processId(mapper, FIRST_TRACKING_ID);
8835 processPosition(mapper, x1 + 1, y1 + 1);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8839 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8840
8841 // second finger up.
8842 processSlot(mapper, SECOND_SLOT);
8843 processId(mapper, INVALID_TRACKING_ID);
8844 processSync(mapper);
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8847
8848 // first finger keep moving
8849 processSlot(mapper, FIRST_SLOT);
8850 processId(mapper, FIRST_TRACKING_ID);
8851 processPosition(mapper, x1 + 2, y1 + 2);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8855
8856 // first finger up.
8857 processId(mapper, INVALID_TRACKING_ID);
8858 processSync(mapper);
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8861 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008862}
8863
Arthur Hung9ad18942021-06-19 02:04:46 +00008864/**
8865 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8866 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8867 * cause slot be valid again.
8868 */
8869TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8870 addConfigurationProperty("touch.deviceType", "touchScreen");
8871 prepareDisplay(DISPLAY_ORIENTATION_0);
8872 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8874
8875 NotifyMotionArgs motionArgs;
8876
8877 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8878 // First finger down.
8879 processId(mapper, FIRST_TRACKING_ID);
8880 processPosition(mapper, x1, y1);
8881 processPressure(mapper, RAW_PRESSURE_MAX);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8885 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8886
8887 // First finger move.
8888 processId(mapper, FIRST_TRACKING_ID);
8889 processPosition(mapper, x1 + 1, y1 + 1);
8890 processPressure(mapper, RAW_PRESSURE_MAX);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8894 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8895
8896 // Second finger down.
8897 processSlot(mapper, SECOND_SLOT);
8898 processId(mapper, SECOND_TRACKING_ID);
8899 processPosition(mapper, x2, y2);
8900 processPressure(mapper, RAW_PRESSURE_MAX);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008903 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008904 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8905
8906 // second finger up with some unexpected data.
8907 processSlot(mapper, SECOND_SLOT);
8908 processId(mapper, INVALID_TRACKING_ID);
8909 processPosition(mapper, x2, y2);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008912 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008913 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8914
8915 // first finger up with some unexpected data.
8916 processSlot(mapper, FIRST_SLOT);
8917 processId(mapper, INVALID_TRACKING_ID);
8918 processPosition(mapper, x2, y2);
8919 processPressure(mapper, RAW_PRESSURE_MAX);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8923 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8924}
8925
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008926// --- MultiTouchInputMapperTest_ExternalDevice ---
8927
8928class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8929protected:
Chris Yea52ade12020-08-27 16:49:20 -07008930 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008931};
8932
8933/**
8934 * Expect fallback to internal viewport if device is external and external viewport is not present.
8935 */
8936TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8937 prepareAxes(POSITION);
8938 addConfigurationProperty("touch.deviceType", "touchScreen");
8939 prepareDisplay(DISPLAY_ORIENTATION_0);
8940 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8941
8942 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8943
8944 NotifyMotionArgs motionArgs;
8945
8946 // Expect the event to be sent to the internal viewport,
8947 // because an external viewport is not present.
8948 processPosition(mapper, 100, 100);
8949 processSync(mapper);
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8951 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8952
8953 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008954 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008955 processPosition(mapper, 100, 100);
8956 processSync(mapper);
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8958 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8959}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008960
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008961TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8962 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8963 std::shared_ptr<FakePointerController> fakePointerController =
8964 std::make_shared<FakePointerController>();
8965 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8966 fakePointerController->setPosition(0, 0);
8967 fakePointerController->setButtonState(0);
8968
8969 // prepare device and capture
8970 prepareDisplay(DISPLAY_ORIENTATION_0);
8971 prepareAxes(POSITION | ID | SLOT);
8972 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8973 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8974 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008975 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8977
8978 // captured touchpad should be a touchpad source
8979 NotifyDeviceResetArgs resetArgs;
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8981 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8982
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008983 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008984
8985 const InputDeviceInfo::MotionRange* relRangeX =
8986 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8987 ASSERT_NE(relRangeX, nullptr);
8988 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8989 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8990 const InputDeviceInfo::MotionRange* relRangeY =
8991 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8992 ASSERT_NE(relRangeY, nullptr);
8993 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8994 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8995
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008996 // run captured pointer tests - note that this is unscaled, so input listener events should be
8997 // identical to what the hardware sends (accounting for any
8998 // calibration).
8999 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009000 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009001 processId(mapper, 1);
9002 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9003 processKey(mapper, BTN_TOUCH, 1);
9004 processSync(mapper);
9005
9006 // expect coord[0] to contain initial location of touch 0
9007 NotifyMotionArgs args;
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9010 ASSERT_EQ(1U, args.pointerCount);
9011 ASSERT_EQ(0, args.pointerProperties[0].id);
9012 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9013 ASSERT_NO_FATAL_FAILURE(
9014 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9015
9016 // FINGER 1 DOWN
9017 processSlot(mapper, 1);
9018 processId(mapper, 2);
9019 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9020 processSync(mapper);
9021
9022 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009024 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009025 ASSERT_EQ(2U, args.pointerCount);
9026 ASSERT_EQ(0, args.pointerProperties[0].id);
9027 ASSERT_EQ(1, args.pointerProperties[1].id);
9028 ASSERT_NO_FATAL_FAILURE(
9029 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9030 ASSERT_NO_FATAL_FAILURE(
9031 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9032
9033 // FINGER 1 MOVE
9034 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9035 processSync(mapper);
9036
9037 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9038 // from move
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9041 ASSERT_NO_FATAL_FAILURE(
9042 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9043 ASSERT_NO_FATAL_FAILURE(
9044 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9045
9046 // FINGER 0 MOVE
9047 processSlot(mapper, 0);
9048 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9049 processSync(mapper);
9050
9051 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9054 ASSERT_NO_FATAL_FAILURE(
9055 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9056 ASSERT_NO_FATAL_FAILURE(
9057 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9058
9059 // BUTTON DOWN
9060 processKey(mapper, BTN_LEFT, 1);
9061 processSync(mapper);
9062
9063 // touchinputmapper design sends a move before button press
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9068
9069 // BUTTON UP
9070 processKey(mapper, BTN_LEFT, 0);
9071 processSync(mapper);
9072
9073 // touchinputmapper design sends a move after button release
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9078
9079 // FINGER 0 UP
9080 processId(mapper, -1);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9083 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9084
9085 // FINGER 1 MOVE
9086 processSlot(mapper, 1);
9087 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9088 processSync(mapper);
9089
9090 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9093 ASSERT_EQ(1U, args.pointerCount);
9094 ASSERT_EQ(1, args.pointerProperties[0].id);
9095 ASSERT_NO_FATAL_FAILURE(
9096 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9097
9098 // FINGER 1 UP
9099 processId(mapper, -1);
9100 processKey(mapper, BTN_TOUCH, 0);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9103 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9104
9105 // non captured touchpad should be a mouse source
9106 mFakePolicy->setPointerCapture(false);
9107 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9109 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9110}
9111
9112TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9113 std::shared_ptr<FakePointerController> fakePointerController =
9114 std::make_shared<FakePointerController>();
9115 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9116 fakePointerController->setPosition(0, 0);
9117 fakePointerController->setButtonState(0);
9118
9119 // prepare device and capture
9120 prepareDisplay(DISPLAY_ORIENTATION_0);
9121 prepareAxes(POSITION | ID | SLOT);
9122 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9123 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009124 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9126 // run uncaptured pointer tests - pushes out generic events
9127 // FINGER 0 DOWN
9128 processId(mapper, 3);
9129 processPosition(mapper, 100, 100);
9130 processKey(mapper, BTN_TOUCH, 1);
9131 processSync(mapper);
9132
9133 // start at (100,100), cursor should be at (0,0) * scale
9134 NotifyMotionArgs args;
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9137 ASSERT_NO_FATAL_FAILURE(
9138 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9139
9140 // FINGER 0 MOVE
9141 processPosition(mapper, 200, 200);
9142 processSync(mapper);
9143
9144 // compute scaling to help with touch position checking
9145 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9146 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9147 float scale =
9148 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9149
9150 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9152 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9154 0, 0, 0, 0, 0, 0, 0));
9155}
9156
9157TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9158 std::shared_ptr<FakePointerController> fakePointerController =
9159 std::make_shared<FakePointerController>();
9160
9161 prepareDisplay(DISPLAY_ORIENTATION_0);
9162 prepareAxes(POSITION | ID | SLOT);
9163 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009164 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009165 mFakePolicy->setPointerCapture(false);
9166 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9167
9168 // uncaptured touchpad should be a pointer device
9169 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9170
9171 // captured touchpad should be a touchpad device
9172 mFakePolicy->setPointerCapture(true);
9173 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9174 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9175}
9176
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009177// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009178
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009179class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009180protected:
9181 static const char* DEVICE_NAME;
9182 static const char* DEVICE_LOCATION;
9183 static const int32_t DEVICE_ID;
9184 static const int32_t DEVICE_GENERATION;
9185 static const int32_t DEVICE_CONTROLLER_NUMBER;
9186 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9187 static const int32_t EVENTHUB_ID;
9188
9189 std::shared_ptr<FakeEventHub> mFakeEventHub;
9190 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009191 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009192 std::unique_ptr<InstrumentedInputReader> mReader;
9193 std::shared_ptr<InputDevice> mDevice;
9194
9195 virtual void SetUp(Flags<InputDeviceClass> classes) {
9196 mFakeEventHub = std::make_unique<FakeEventHub>();
9197 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009198 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009199 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009200 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009201 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9202 }
9203
9204 void SetUp() override { SetUp(DEVICE_CLASSES); }
9205
9206 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009207 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009208 mFakePolicy.clear();
9209 }
9210
9211 void configureDevice(uint32_t changes) {
9212 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9213 mReader->requestRefreshConfiguration(changes);
9214 mReader->loopOnce();
9215 }
9216 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9217 }
9218
9219 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9220 const std::string& location, int32_t eventHubId,
9221 Flags<InputDeviceClass> classes) {
9222 InputDeviceIdentifier identifier;
9223 identifier.name = name;
9224 identifier.location = location;
9225 std::shared_ptr<InputDevice> device =
9226 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9227 identifier);
9228 mReader->pushNextDevice(device);
9229 mFakeEventHub->addDevice(eventHubId, name, classes);
9230 mReader->loopOnce();
9231 return device;
9232 }
9233
9234 template <class T, typename... Args>
9235 T& addControllerAndConfigure(Args... args) {
9236 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9237
9238 return controller;
9239 }
9240};
9241
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009242const char* PeripheralControllerTest::DEVICE_NAME = "device";
9243const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9244const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9245const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9246const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9247const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009248 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009249const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009250
9251// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009252class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009253protected:
9254 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009255 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009256 }
9257};
9258
9259TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009260 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009261
9262 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9263 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9264}
9265
9266TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009267 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009268
9269 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9270 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9271}
9272
9273// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009274class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009275protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009276 void SetUp() override {
9277 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9278 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009279};
9280
Chris Ye85758332021-05-16 23:05:17 -07009281TEST_F(LightControllerTest, MonoLight) {
9282 RawLightInfo infoMono = {.id = 1,
9283 .name = "Mono",
9284 .maxBrightness = 255,
9285 .flags = InputLightClass::BRIGHTNESS,
9286 .path = ""};
9287 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009288
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009289 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009290 InputDeviceInfo info;
9291 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009292 std::vector<InputDeviceLightInfo> lights = info.getLights();
9293 ASSERT_EQ(1U, lights.size());
9294 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009295
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009296 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9297 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009298}
9299
9300TEST_F(LightControllerTest, RGBLight) {
9301 RawLightInfo infoRed = {.id = 1,
9302 .name = "red",
9303 .maxBrightness = 255,
9304 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9305 .path = ""};
9306 RawLightInfo infoGreen = {.id = 2,
9307 .name = "green",
9308 .maxBrightness = 255,
9309 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9310 .path = ""};
9311 RawLightInfo infoBlue = {.id = 3,
9312 .name = "blue",
9313 .maxBrightness = 255,
9314 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9315 .path = ""};
9316 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9317 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9318 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9319
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009320 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009321 InputDeviceInfo info;
9322 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009323 std::vector<InputDeviceLightInfo> lights = info.getLights();
9324 ASSERT_EQ(1U, lights.size());
9325 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009326
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009327 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9328 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009329}
9330
9331TEST_F(LightControllerTest, MultiColorRGBLight) {
9332 RawLightInfo infoColor = {.id = 1,
9333 .name = "red",
9334 .maxBrightness = 255,
9335 .flags = InputLightClass::BRIGHTNESS |
9336 InputLightClass::MULTI_INTENSITY |
9337 InputLightClass::MULTI_INDEX,
9338 .path = ""};
9339
9340 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9341
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009342 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009343 InputDeviceInfo info;
9344 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009345 std::vector<InputDeviceLightInfo> lights = info.getLights();
9346 ASSERT_EQ(1U, lights.size());
9347 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009348
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009349 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9350 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009351}
9352
9353TEST_F(LightControllerTest, PlayerIdLight) {
9354 RawLightInfo info1 = {.id = 1,
9355 .name = "player1",
9356 .maxBrightness = 255,
9357 .flags = InputLightClass::BRIGHTNESS,
9358 .path = ""};
9359 RawLightInfo info2 = {.id = 2,
9360 .name = "player2",
9361 .maxBrightness = 255,
9362 .flags = InputLightClass::BRIGHTNESS,
9363 .path = ""};
9364 RawLightInfo info3 = {.id = 3,
9365 .name = "player3",
9366 .maxBrightness = 255,
9367 .flags = InputLightClass::BRIGHTNESS,
9368 .path = ""};
9369 RawLightInfo info4 = {.id = 4,
9370 .name = "player4",
9371 .maxBrightness = 255,
9372 .flags = InputLightClass::BRIGHTNESS,
9373 .path = ""};
9374 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9375 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9376 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9377 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9378
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009379 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009380 InputDeviceInfo info;
9381 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009382 std::vector<InputDeviceLightInfo> lights = info.getLights();
9383 ASSERT_EQ(1U, lights.size());
9384 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009385
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009386 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9387 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9388 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009389}
9390
Michael Wrightd02c5b62014-02-10 15:10:22 -08009391} // namespace android