blob: 54cf15d1d4bf25aa8dcdbdf7b1723b99f7812798 [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;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100442 // fake mapping which would normally come from keyCharacterMap
443 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700444 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
445 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800446 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700447 bool enabled;
448
449 status_t enable() {
450 enabled = true;
451 return OK;
452 }
453
454 status_t disable() {
455 enabled = false;
456 return OK;
457 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458
Chris Ye1b0c7342020-07-28 21:57:03 -0700459 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 };
461
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700462 std::mutex mLock;
463 std::condition_variable mEventsCondition;
464
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100466 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000467 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600468 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000469 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800470 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
471 // Simulates a device light brightness, from light id to light brightness.
472 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
473 // Simulates a device light intensities, from light id to light intensities map.
474 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
475 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700477public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 virtual ~FakeEventHub() {
479 for (size_t i = 0; i < mDevices.size(); i++) {
480 delete mDevices.valueAt(i);
481 }
482 }
483
Michael Wrightd02c5b62014-02-10 15:10:22 -0800484 FakeEventHub() { }
485
Chris Ye1b0c7342020-07-28 21:57:03 -0700486 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487 Device* device = new Device(classes);
488 device->identifier.name = name;
489 mDevices.add(deviceId, device);
490
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000491 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800492 }
493
494 void removeDevice(int32_t deviceId) {
495 delete mDevices.valueFor(deviceId);
496 mDevices.removeItem(deviceId);
497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000498 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 }
500
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700501 bool isDeviceEnabled(int32_t deviceId) {
502 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700503 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700504 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
505 return false;
506 }
507 return device->enabled;
508 }
509
510 status_t enableDevice(int32_t deviceId) {
511 status_t result;
512 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700513 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700514 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
515 return BAD_VALUE;
516 }
517 if (device->enabled) {
518 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
519 return OK;
520 }
521 result = device->enable();
522 return result;
523 }
524
525 status_t disableDevice(int32_t deviceId) {
526 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700527 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700528 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
529 return BAD_VALUE;
530 }
531 if (!device->enabled) {
532 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
533 return OK;
534 }
535 return device->disable();
536 }
537
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000539 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541
542 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
543 Device* device = getDevice(deviceId);
544 device->configuration.addProperty(key, value);
545 }
546
547 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
548 Device* device = getDevice(deviceId);
549 device->configuration.addAll(configuration);
550 }
551
552 void addAbsoluteAxis(int32_t deviceId, int axis,
553 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
554 Device* device = getDevice(deviceId);
555
556 RawAbsoluteAxisInfo info;
557 info.valid = true;
558 info.minValue = minValue;
559 info.maxValue = maxValue;
560 info.flat = flat;
561 info.fuzz = fuzz;
562 info.resolution = resolution;
563 device->absoluteAxes.add(axis, info);
564 }
565
566 void addRelativeAxis(int32_t deviceId, int32_t axis) {
567 Device* device = getDevice(deviceId);
568 device->relativeAxes.add(axis, true);
569 }
570
571 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
572 Device* device = getDevice(deviceId);
573 device->keyCodeStates.replaceValueFor(keyCode, state);
574 }
575
576 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
577 Device* device = getDevice(deviceId);
578 device->scanCodeStates.replaceValueFor(scanCode, state);
579 }
580
581 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
582 Device* device = getDevice(deviceId);
583 device->switchStates.replaceValueFor(switchCode, state);
584 }
585
586 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
587 Device* device = getDevice(deviceId);
588 device->absoluteAxisValue.replaceValueFor(axis, value);
589 }
590
591 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
592 int32_t keyCode, uint32_t flags) {
593 Device* device = getDevice(deviceId);
594 KeyInfo info;
595 info.keyCode = keyCode;
596 info.flags = flags;
597 if (scanCode) {
598 device->keysByScanCode.add(scanCode, info);
599 }
600 if (usageCode) {
601 device->keysByUsageCode.add(usageCode, info);
602 }
603 }
604
Philip Junker4af3b3d2021-12-14 10:36:55 +0100605 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
606 Device* device = getDevice(deviceId);
607 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
608 }
609
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 void addLed(int32_t deviceId, int32_t led, bool initialState) {
611 Device* device = getDevice(deviceId);
612 device->leds.add(led, initialState);
613 }
614
Chris Yef59a2f42020-10-16 12:55:26 -0700615 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
616 int32_t sensorDataIndex) {
617 Device* device = getDevice(deviceId);
618 SensorInfo info;
619 info.sensorType = sensorType;
620 info.sensorDataIndex = sensorDataIndex;
621 device->sensorsByAbsCode.emplace(absCode, info);
622 }
623
624 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
625 Device* device = getDevice(deviceId);
626 typename BitArray<MSC_MAX>::Buffer buffer;
627 buffer[mscEvent / 32] = 1 << mscEvent % 32;
628 device->mscBitmask.loadFromBuffer(buffer);
629 }
630
Chris Ye3fdbfef2021-01-06 18:45:18 -0800631 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
632 mRawLightInfos.emplace(rawId, std::move(info));
633 }
634
635 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
636 mLightBrightness.emplace(rawId, brightness);
637 }
638
639 void fakeLightIntensities(int32_t rawId,
640 const std::unordered_map<LightColor, int32_t> intensities) {
641 mLightIntensities.emplace(rawId, std::move(intensities));
642 }
643
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 bool getLedState(int32_t deviceId, int32_t led) {
645 Device* device = getDevice(deviceId);
646 return device->leds.valueFor(led);
647 }
648
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100649 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800650 return mExcludedDevices;
651 }
652
653 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
654 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800655 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 }
657
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000658 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
659 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700660 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 RawEvent event;
662 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000663 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664 event.deviceId = deviceId;
665 event.type = type;
666 event.code = code;
667 event.value = value;
668 mEvents.push_back(event);
669
670 if (type == EV_ABS) {
671 setAbsoluteAxisValue(deviceId, code, value);
672 }
673 }
674
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600675 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
676 std::vector<TouchVideoFrame>> videoFrames) {
677 mVideoFrames = std::move(videoFrames);
678 }
679
Michael Wrightd02c5b62014-02-10 15:10:22 -0800680 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700681 std::unique_lock<std::mutex> lock(mLock);
682 base::ScopedLockAssertion assumeLocked(mLock);
683 const bool queueIsEmpty =
684 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
685 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
686 if (!queueIsEmpty) {
687 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
688 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689 }
690
691private:
692 Device* getDevice(int32_t deviceId) const {
693 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100694 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800695 }
696
Chris Yea52ade12020-08-27 16:49:20 -0700697 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800698 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700699 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800700 }
701
Chris Yea52ade12020-08-27 16:49:20 -0700702 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 Device* device = getDevice(deviceId);
704 return device ? device->identifier : InputDeviceIdentifier();
705 }
706
Chris Yea52ade12020-08-27 16:49:20 -0700707 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800708
Chris Yea52ade12020-08-27 16:49:20 -0700709 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 Device* device = getDevice(deviceId);
711 if (device) {
712 *outConfiguration = device->configuration;
713 }
714 }
715
Chris Yea52ade12020-08-27 16:49:20 -0700716 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
717 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800719 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 ssize_t index = device->absoluteAxes.indexOfKey(axis);
721 if (index >= 0) {
722 *outAxisInfo = device->absoluteAxes.valueAt(index);
723 return OK;
724 }
725 }
726 outAxisInfo->clear();
727 return -1;
728 }
729
Chris Yea52ade12020-08-27 16:49:20 -0700730 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800731 Device* device = getDevice(deviceId);
732 if (device) {
733 return device->relativeAxes.indexOfKey(axis) >= 0;
734 }
735 return false;
736 }
737
Chris Yea52ade12020-08-27 16:49:20 -0700738 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739
Chris Yef59a2f42020-10-16 12:55:26 -0700740 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
741 Device* device = getDevice(deviceId);
742 if (device) {
743 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
744 }
745 return false;
746 }
747
Chris Yea52ade12020-08-27 16:49:20 -0700748 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
749 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750 Device* device = getDevice(deviceId);
751 if (device) {
752 const KeyInfo* key = getKey(device, scanCode, usageCode);
753 if (key) {
754 if (outKeycode) {
755 *outKeycode = key->keyCode;
756 }
757 if (outFlags) {
758 *outFlags = key->flags;
759 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700760 if (outMetaState) {
761 *outMetaState = metaState;
762 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 return OK;
764 }
765 }
766 return NAME_NOT_FOUND;
767 }
768
769 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
770 if (usageCode) {
771 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
772 if (index >= 0) {
773 return &device->keysByUsageCode.valueAt(index);
774 }
775 }
776 if (scanCode) {
777 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
778 if (index >= 0) {
779 return &device->keysByScanCode.valueAt(index);
780 }
781 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700782 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800783 }
784
Chris Yea52ade12020-08-27 16:49:20 -0700785 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800786
Chris Yef59a2f42020-10-16 12:55:26 -0700787 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
788 int32_t absCode) {
789 Device* device = getDevice(deviceId);
790 if (!device) {
791 return Errorf("Sensor device not found.");
792 }
793 auto it = device->sensorsByAbsCode.find(absCode);
794 if (it == device->sensorsByAbsCode.end()) {
795 return Errorf("Sensor map not found.");
796 }
797 const SensorInfo& info = it->second;
798 return std::make_pair(info.sensorType, info.sensorDataIndex);
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802 mExcludedDevices = devices;
803 }
804
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000805 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
806 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000808 const size_t filledSize = std::min(mEvents.size(), bufferSize);
809 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
810
811 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700812 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000813 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814 }
815
Chris Yea52ade12020-08-27 16:49:20 -0700816 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600817 auto it = mVideoFrames.find(deviceId);
818 if (it != mVideoFrames.end()) {
819 std::vector<TouchVideoFrame> frames = std::move(it->second);
820 mVideoFrames.erase(deviceId);
821 return frames;
822 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800823 return {};
824 }
825
Chris Yea52ade12020-08-27 16:49:20 -0700826 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800827 Device* device = getDevice(deviceId);
828 if (device) {
829 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
830 if (index >= 0) {
831 return device->scanCodeStates.valueAt(index);
832 }
833 }
834 return AKEY_STATE_UNKNOWN;
835 }
836
Chris Yea52ade12020-08-27 16:49:20 -0700837 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838 Device* device = getDevice(deviceId);
839 if (device) {
840 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
841 if (index >= 0) {
842 return device->keyCodeStates.valueAt(index);
843 }
844 }
845 return AKEY_STATE_UNKNOWN;
846 }
847
Chris Yea52ade12020-08-27 16:49:20 -0700848 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800849 Device* device = getDevice(deviceId);
850 if (device) {
851 ssize_t index = device->switchStates.indexOfKey(sw);
852 if (index >= 0) {
853 return device->switchStates.valueAt(index);
854 }
855 }
856 return AKEY_STATE_UNKNOWN;
857 }
858
Chris Yea52ade12020-08-27 16:49:20 -0700859 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
860 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDevice(deviceId);
862 if (device) {
863 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
864 if (index >= 0) {
865 *outValue = device->absoluteAxisValue.valueAt(index);
866 return OK;
867 }
868 }
869 *outValue = 0;
870 return -1;
871 }
872
Philip Junker4af3b3d2021-12-14 10:36:55 +0100873 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
874 Device* device = getDevice(deviceId);
875 if (!device) {
876 return AKEYCODE_UNKNOWN;
877 }
878 auto it = device->keyCodeMapping.find(locationKeyCode);
879 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
880 }
881
Chris Yea52ade12020-08-27 16:49:20 -0700882 // Return true if the device has non-empty key layout.
883 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
884 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 bool result = false;
886 Device* device = getDevice(deviceId);
887 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700888 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 for (size_t i = 0; i < numCodes; i++) {
890 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
891 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
892 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893 }
894 }
895 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
896 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
897 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 }
899 }
900 }
901 }
902 return result;
903 }
904
Chris Yea52ade12020-08-27 16:49:20 -0700905 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800906 Device* device = getDevice(deviceId);
907 if (device) {
908 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
909 return index >= 0;
910 }
911 return false;
912 }
913
Arthur Hungcb40a002021-08-03 14:31:01 +0000914 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
915 Device* device = getDevice(deviceId);
916 if (!device) {
917 return false;
918 }
919 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
920 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
921 return true;
922 }
923 }
924 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
925 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
926 return true;
927 }
928 }
929 return false;
930 }
931
Chris Yea52ade12020-08-27 16:49:20 -0700932 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 Device* device = getDevice(deviceId);
934 return device && device->leds.indexOfKey(led) >= 0;
935 }
936
Chris Yea52ade12020-08-27 16:49:20 -0700937 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 Device* device = getDevice(deviceId);
939 if (device) {
940 ssize_t index = device->leds.indexOfKey(led);
941 if (index >= 0) {
942 device->leds.replaceValueAt(led, on);
943 } else {
944 ADD_FAILURE()
945 << "Attempted to set the state of an LED that the EventHub declared "
946 "was not present. led=" << led;
947 }
948 }
949 }
950
Chris Yea52ade12020-08-27 16:49:20 -0700951 void getVirtualKeyDefinitions(
952 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 outVirtualKeys.clear();
954
955 Device* device = getDevice(deviceId);
956 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800957 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959 }
960
Chris Yea52ade12020-08-27 16:49:20 -0700961 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700962 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 return false;
967 }
968
Chris Yea52ade12020-08-27 16:49:20 -0700969 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800970
Chris Yea52ade12020-08-27 16:49:20 -0700971 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972
Chris Ye87143712020-11-10 05:05:58 +0000973 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
974
Chris Yee2b1e5c2021-03-10 22:45:12 -0800975 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
976 return BATTERY_CAPACITY;
977 }
Kim Low03ea0352020-11-06 12:45:07 -0800978
Chris Yee2b1e5c2021-03-10 22:45:12 -0800979 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
980 return BATTERY_STATUS;
981 }
982
983 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
984
985 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
986 return std::nullopt;
987 }
Kim Low03ea0352020-11-06 12:45:07 -0800988
Chris Ye3fdbfef2021-01-06 18:45:18 -0800989 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
990 std::vector<int32_t> ids;
991 for (const auto& [rawId, info] : mRawLightInfos) {
992 ids.push_back(rawId);
993 }
994 return ids;
995 }
996
997 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
998 auto it = mRawLightInfos.find(lightId);
999 if (it == mRawLightInfos.end()) {
1000 return std::nullopt;
1001 }
1002 return it->second;
1003 }
1004
1005 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1006 mLightBrightness.emplace(lightId, brightness);
1007 }
1008
1009 void setLightIntensities(int32_t deviceId, int32_t lightId,
1010 std::unordered_map<LightColor, int32_t> intensities) override {
1011 mLightIntensities.emplace(lightId, intensities);
1012 };
1013
1014 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
1015 auto lightIt = mLightBrightness.find(lightId);
1016 if (lightIt == mLightBrightness.end()) {
1017 return std::nullopt;
1018 }
1019 return lightIt->second;
1020 }
1021
1022 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1023 int32_t deviceId, int32_t lightId) override {
1024 auto lightIt = mLightIntensities.find(lightId);
1025 if (lightIt == mLightIntensities.end()) {
1026 return std::nullopt;
1027 }
1028 return lightIt->second;
1029 };
1030
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001031 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 return false;
1033 }
1034
Chris Yea52ade12020-08-27 16:49:20 -07001035 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036
Chris Yea52ade12020-08-27 16:49:20 -07001037 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Chris Yea52ade12020-08-27 16:49:20 -07001039 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001040
Chris Yea52ade12020-08-27 16:49:20 -07001041 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042};
1043
Michael Wrightd02c5b62014-02-10 15:10:22 -08001044// --- FakeInputMapper ---
1045
1046class FakeInputMapper : public InputMapper {
1047 uint32_t mSources;
1048 int32_t mKeyboardType;
1049 int32_t mMetaState;
1050 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1051 KeyedVector<int32_t, int32_t> mScanCodeStates;
1052 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001053 // fake mapping which would normally come from keyCharacterMap
1054 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001055 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001057 std::mutex mLock;
1058 std::condition_variable mStateChangedCondition;
1059 bool mConfigureWasCalled GUARDED_BY(mLock);
1060 bool mResetWasCalled GUARDED_BY(mLock);
1061 bool mProcessWasCalled GUARDED_BY(mLock);
1062 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001063
Arthur Hungc23540e2018-11-29 20:42:11 +08001064 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001065public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001066 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1067 : InputMapper(deviceContext),
1068 mSources(sources),
1069 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001070 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001071 mConfigureWasCalled(false),
1072 mResetWasCalled(false),
1073 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001074
Chris Yea52ade12020-08-27 16:49:20 -07001075 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001076
1077 void setKeyboardType(int32_t keyboardType) {
1078 mKeyboardType = keyboardType;
1079 }
1080
1081 void setMetaState(int32_t metaState) {
1082 mMetaState = metaState;
1083 }
1084
1085 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001086 std::unique_lock<std::mutex> lock(mLock);
1087 base::ScopedLockAssertion assumeLocked(mLock);
1088 const bool configureCalled =
1089 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1090 return mConfigureWasCalled;
1091 });
1092 if (!configureCalled) {
1093 FAIL() << "Expected configure() to have been called.";
1094 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001095 mConfigureWasCalled = false;
1096 }
1097
1098 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001099 std::unique_lock<std::mutex> lock(mLock);
1100 base::ScopedLockAssertion assumeLocked(mLock);
1101 const bool resetCalled =
1102 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1103 return mResetWasCalled;
1104 });
1105 if (!resetCalled) {
1106 FAIL() << "Expected reset() to have been called.";
1107 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108 mResetWasCalled = false;
1109 }
1110
Yi Kong9b14ac62018-07-17 13:48:38 -07001111 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001112 std::unique_lock<std::mutex> lock(mLock);
1113 base::ScopedLockAssertion assumeLocked(mLock);
1114 const bool processCalled =
1115 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1116 return mProcessWasCalled;
1117 });
1118 if (!processCalled) {
1119 FAIL() << "Expected process() to have been called.";
1120 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001121 if (outLastEvent) {
1122 *outLastEvent = mLastEvent;
1123 }
1124 mProcessWasCalled = false;
1125 }
1126
1127 void setKeyCodeState(int32_t keyCode, int32_t state) {
1128 mKeyCodeStates.replaceValueFor(keyCode, state);
1129 }
1130
1131 void setScanCodeState(int32_t scanCode, int32_t state) {
1132 mScanCodeStates.replaceValueFor(scanCode, state);
1133 }
1134
1135 void setSwitchState(int32_t switchCode, int32_t state) {
1136 mSwitchStates.replaceValueFor(switchCode, state);
1137 }
1138
1139 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001140 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 }
1142
Philip Junker4af3b3d2021-12-14 10:36:55 +01001143 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1144 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1145 }
1146
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001148 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001149
Chris Yea52ade12020-08-27 16:49:20 -07001150 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 InputMapper::populateDeviceInfo(deviceInfo);
1152
1153 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1154 deviceInfo->setKeyboardType(mKeyboardType);
1155 }
1156 }
1157
Chris Yea52ade12020-08-27 16:49:20 -07001158 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001159 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001161
1162 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001163 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001164 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1165 mViewport = config->getDisplayViewportByPort(*displayPort);
1166 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001167
1168 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001169 }
1170
Chris Yea52ade12020-08-27 16:49:20 -07001171 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001172 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001174 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001175 }
1176
Chris Yea52ade12020-08-27 16:49:20 -07001177 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001178 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001179 mLastEvent = *rawEvent;
1180 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001181 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001182 }
1183
Chris Yea52ade12020-08-27 16:49:20 -07001184 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1186 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1187 }
1188
Philip Junker4af3b3d2021-12-14 10:36:55 +01001189 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1190 auto it = mKeyCodeMapping.find(locationKeyCode);
1191 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1192 }
1193
Chris Yea52ade12020-08-27 16:49:20 -07001194 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1196 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1197 }
1198
Chris Yea52ade12020-08-27 16:49:20 -07001199 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001200 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1201 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1202 }
1203
Chris Yea52ade12020-08-27 16:49:20 -07001204 // Return true if the device has non-empty key layout.
1205 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1206 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 for (size_t i = 0; i < numCodes; i++) {
1208 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1209 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1210 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212 }
1213 }
Chris Yea52ade12020-08-27 16:49:20 -07001214 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 return result;
1216 }
1217
1218 virtual int32_t getMetaState() {
1219 return mMetaState;
1220 }
1221
1222 virtual void fadePointer() {
1223 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001224
1225 virtual std::optional<int32_t> getAssociatedDisplay() {
1226 if (mViewport) {
1227 return std::make_optional(mViewport->displayId);
1228 }
1229 return std::nullopt;
1230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231};
1232
1233
1234// --- InstrumentedInputReader ---
1235
1236class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001237 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001238
1239public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001240 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1241 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001242 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001243 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001245 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001247 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001249 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001250 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001251 InputDeviceIdentifier identifier;
1252 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001253 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001254 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001255 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256 }
1257
Prabir Pradhan28efc192019-11-05 01:10:04 +00001258 // Make the protected loopOnce method accessible to tests.
1259 using InputReader::loopOnce;
1260
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001262 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1263 const InputDeviceIdentifier& identifier)
1264 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001265 if (!mNextDevices.empty()) {
1266 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1267 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 return device;
1269 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001270 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001271 }
1272
arthurhungdcef2dc2020-08-11 14:47:50 +08001273 // --- FakeInputReaderContext ---
1274 class FakeInputReaderContext : public ContextImpl {
1275 int32_t mGlobalMetaState;
1276 bool mUpdateGlobalMetaStateWasCalled;
1277 int32_t mGeneration;
1278
1279 public:
1280 FakeInputReaderContext(InputReader* reader)
1281 : ContextImpl(reader),
1282 mGlobalMetaState(0),
1283 mUpdateGlobalMetaStateWasCalled(false),
1284 mGeneration(1) {}
1285
1286 virtual ~FakeInputReaderContext() {}
1287
1288 void assertUpdateGlobalMetaStateWasCalled() {
1289 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1290 << "Expected updateGlobalMetaState() to have been called.";
1291 mUpdateGlobalMetaStateWasCalled = false;
1292 }
1293
1294 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1295
1296 uint32_t getGeneration() { return mGeneration; }
1297
1298 void updateGlobalMetaState() override {
1299 mUpdateGlobalMetaStateWasCalled = true;
1300 ContextImpl::updateGlobalMetaState();
1301 }
1302
1303 int32_t getGlobalMetaState() override {
1304 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1305 }
1306
1307 int32_t bumpGeneration() override {
1308 mGeneration = ContextImpl::bumpGeneration();
1309 return mGeneration;
1310 }
1311 } mFakeContext;
1312
Michael Wrightd02c5b62014-02-10 15:10:22 -08001313 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001314
1315public:
1316 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001317};
1318
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001319// --- InputReaderPolicyTest ---
1320class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001321protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001322 sp<FakeInputReaderPolicy> mFakePolicy;
1323
Chris Yea52ade12020-08-27 16:49:20 -07001324 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1325 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001326};
1327
1328/**
1329 * Check that empty set of viewports is an acceptable configuration.
1330 * Also try to get internal viewport two different ways - by type and by uniqueId.
1331 *
1332 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1333 * Such configuration is not currently allowed.
1334 */
1335TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001336 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001337
1338 // We didn't add any viewports yet, so there shouldn't be any.
1339 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001340 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001341 ASSERT_FALSE(internalViewport);
1342
1343 // Add an internal viewport, then clear it
1344 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001345 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001346 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001347
1348 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001349 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001350 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001351 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001352
1353 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001354 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001356 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001357
1358 mFakePolicy->clearViewports();
1359 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001360 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001361 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001362 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363 ASSERT_FALSE(internalViewport);
1364}
1365
1366TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1367 const std::string internalUniqueId = "local:0";
1368 const std::string externalUniqueId = "local:1";
1369 const std::string virtualUniqueId1 = "virtual:2";
1370 const std::string virtualUniqueId2 = "virtual:3";
1371 constexpr int32_t virtualDisplayId1 = 2;
1372 constexpr int32_t virtualDisplayId2 = 3;
1373
1374 // Add an internal viewport
1375 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001376 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1377 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001378 // Add an external viewport
1379 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001380 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1381 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001382 // Add an virtual viewport
1383 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001384 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1385 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001386 // Add another virtual viewport
1387 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001388 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1389 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390
1391 // Check matching by type for internal
1392 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001393 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394 ASSERT_TRUE(internalViewport);
1395 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1396
1397 // Check matching by type for external
1398 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001399 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_TRUE(externalViewport);
1401 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1402
1403 // Check matching by uniqueId for virtual viewport #1
1404 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001405 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001406 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001407 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001408 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1409 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1410
1411 // Check matching by uniqueId for virtual viewport #2
1412 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001413 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001414 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001415 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001416 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1417 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1418}
1419
1420
1421/**
1422 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1423 * that lookup works by checking display id.
1424 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1425 */
1426TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1427 const std::string uniqueId1 = "uniqueId1";
1428 const std::string uniqueId2 = "uniqueId2";
1429 constexpr int32_t displayId1 = 2;
1430 constexpr int32_t displayId2 = 3;
1431
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001432 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1433 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 for (const ViewportType& type : types) {
1435 mFakePolicy->clearViewports();
1436 // Add a viewport
1437 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1439 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440 // Add another viewport
1441 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001442 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1443 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444
1445 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001446 std::optional<DisplayViewport> viewport1 =
1447 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(viewport1);
1449 ASSERT_EQ(displayId1, viewport1->displayId);
1450 ASSERT_EQ(type, viewport1->type);
1451
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001452 std::optional<DisplayViewport> viewport2 =
1453 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_TRUE(viewport2);
1455 ASSERT_EQ(displayId2, viewport2->displayId);
1456 ASSERT_EQ(type, viewport2->type);
1457
1458 // When there are multiple viewports of the same kind, and uniqueId is not specified
1459 // in the call to getDisplayViewport, then that situation is not supported.
1460 // The viewports can be stored in any order, so we cannot rely on the order, since that
1461 // is just implementation detail.
1462 // However, we can check that it still returns *a* viewport, we just cannot assert
1463 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001464 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001465 ASSERT_TRUE(someViewport);
1466 }
1467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001468
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001469/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001470 * When we have multiple internal displays make sure we always return the default display when
1471 * querying by type.
1472 */
1473TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1474 const std::string uniqueId1 = "uniqueId1";
1475 const std::string uniqueId2 = "uniqueId2";
1476 constexpr int32_t nonDefaultDisplayId = 2;
1477 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1478 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1479
1480 // Add the default display first and ensure it gets returned.
1481 mFakePolicy->clearViewports();
1482 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001483 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001484 ViewportType::INTERNAL);
1485 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001486 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001487 ViewportType::INTERNAL);
1488
1489 std::optional<DisplayViewport> viewport =
1490 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1491 ASSERT_TRUE(viewport);
1492 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1493 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1494
1495 // Add the default display second to make sure order doesn't matter.
1496 mFakePolicy->clearViewports();
1497 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001498 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001499 ViewportType::INTERNAL);
1500 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001501 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001502 ViewportType::INTERNAL);
1503
1504 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1505 ASSERT_TRUE(viewport);
1506 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1507 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1508}
1509
1510/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001511 * Check getDisplayViewportByPort
1512 */
1513TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001514 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001515 const std::string uniqueId1 = "uniqueId1";
1516 const std::string uniqueId2 = "uniqueId2";
1517 constexpr int32_t displayId1 = 1;
1518 constexpr int32_t displayId2 = 2;
1519 const uint8_t hdmi1 = 0;
1520 const uint8_t hdmi2 = 1;
1521 const uint8_t hdmi3 = 2;
1522
1523 mFakePolicy->clearViewports();
1524 // Add a viewport that's associated with some display port that's not of interest.
1525 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001526 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1527 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001528 // Add another viewport, connected to HDMI1 port
1529 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001530 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1531 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001532
1533 // Check that correct display viewport was returned by comparing the display ports.
1534 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1535 ASSERT_TRUE(hdmi1Viewport);
1536 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1537 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1538
1539 // Check that we can still get the same viewport using the uniqueId
1540 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1541 ASSERT_TRUE(hdmi1Viewport);
1542 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1543 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1544 ASSERT_EQ(type, hdmi1Viewport->type);
1545
1546 // Check that we cannot find a port with "HDMI2", because we never added one
1547 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1548 ASSERT_FALSE(hdmi2Viewport);
1549}
1550
Michael Wrightd02c5b62014-02-10 15:10:22 -08001551// --- InputReaderTest ---
1552
1553class InputReaderTest : public testing::Test {
1554protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001555 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001557 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001558 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559
Chris Yea52ade12020-08-27 16:49:20 -07001560 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001561 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001562 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001563 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001564
Prabir Pradhan28efc192019-11-05 01:10:04 +00001565 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001566 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567 }
1568
Chris Yea52ade12020-08-27 16:49:20 -07001569 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001570 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001571 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001572 }
1573
Chris Ye1b0c7342020-07-28 21:57:03 -07001574 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 const PropertyMap* configuration) {
1576 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001577
1578 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001579 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001580 }
1581 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001582 mReader->loopOnce();
1583 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001584 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1585 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001586 }
1587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001588 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001589 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001590 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001591 }
1592
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001593 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001594 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001595 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001596 }
1597
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001598 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001599 const std::string& name,
1600 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001601 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001602 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1603 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001604 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001605 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 return mapper;
1607 }
1608};
1609
Chris Ye98d3f532020-10-01 21:48:59 -07001610TEST_F(InputReaderTest, PolicyGetInputDevices) {
1611 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1612 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1613 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614
1615 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001616 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001618 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001619 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001620 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1621 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001622 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001623}
1624
Chris Yee7310032020-09-22 15:36:28 -07001625TEST_F(InputReaderTest, GetMergedInputDevices) {
1626 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1627 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1628 // Add two subdevices to device
1629 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1630 // Must add at least one mapper or the device will be ignored!
1631 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1632 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1633
1634 // Push same device instance for next device to be added, so they'll have same identifier.
1635 mReader->pushNextDevice(device);
1636 mReader->pushNextDevice(device);
1637 ASSERT_NO_FATAL_FAILURE(
1638 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1639 ASSERT_NO_FATAL_FAILURE(
1640 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1641
1642 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001643 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001644}
1645
Chris Yee14523a2020-12-19 13:46:00 -08001646TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1647 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1648 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1649 // Add two subdevices to device
1650 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1651 // Must add at least one mapper or the device will be ignored!
1652 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1653 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1654
1655 // Push same device instance for next device to be added, so they'll have same identifier.
1656 mReader->pushNextDevice(device);
1657 mReader->pushNextDevice(device);
1658 // Sensor device is initially disabled
1659 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1660 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1661 nullptr));
1662 // Device is disabled because the only sub device is a sensor device and disabled initially.
1663 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1664 ASSERT_FALSE(device->isEnabled());
1665 ASSERT_NO_FATAL_FAILURE(
1666 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1667 // The merged device is enabled if any sub device is enabled
1668 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1669 ASSERT_TRUE(device->isEnabled());
1670}
1671
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001672TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001673 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001674 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 constexpr int32_t eventHubId = 1;
1676 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001677 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001679 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001680 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001681
Yi Kong9b14ac62018-07-17 13:48:38 -07001682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001683
1684 NotifyDeviceResetArgs resetArgs;
1685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001686 ASSERT_EQ(deviceId, resetArgs.deviceId);
1687
1688 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001689 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001690 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001691
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693 ASSERT_EQ(deviceId, resetArgs.deviceId);
1694 ASSERT_EQ(device->isEnabled(), false);
1695
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001696 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001697 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001700 ASSERT_EQ(device->isEnabled(), false);
1701
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001702 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001703 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001705 ASSERT_EQ(deviceId, resetArgs.deviceId);
1706 ASSERT_EQ(device->isEnabled(), true);
1707}
1708
Michael Wrightd02c5b62014-02-10 15:10:22 -08001709TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001710 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001711 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001713 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001714 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001715 AINPUT_SOURCE_KEYBOARD, nullptr);
1716 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717
1718 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1719 AINPUT_SOURCE_ANY, AKEYCODE_A))
1720 << "Should return unknown when the device id is >= 0 but unknown.";
1721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001722 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1723 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1724 << "Should return unknown when the device id is valid but the sources are not "
1725 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001726
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001727 ASSERT_EQ(AKEY_STATE_DOWN,
1728 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1729 AKEYCODE_A))
1730 << "Should return value provided by mapper when device id is valid and the device "
1731 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
1733 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1734 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1735 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1736
1737 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1738 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1739 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1740}
1741
Philip Junker4af3b3d2021-12-14 10:36:55 +01001742TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1743 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1744 constexpr int32_t eventHubId = 1;
1745 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1746 InputDeviceClass::KEYBOARD,
1747 AINPUT_SOURCE_KEYBOARD, nullptr);
1748 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1749
1750 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1751 << "Should return unknown when the device with the specified id is not found.";
1752
1753 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1754 << "Should return correct mapping when device id is valid and mapping exists.";
1755
1756 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1757 << "Should return the location key code when device id is valid and there's no "
1758 "mapping.";
1759}
1760
1761TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1762 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1763 constexpr int32_t eventHubId = 1;
1764 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1765 InputDeviceClass::JOYSTICK,
1766 AINPUT_SOURCE_GAMEPAD, nullptr);
1767 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1768
1769 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1770 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1771}
1772
Michael Wrightd02c5b62014-02-10 15:10:22 -08001773TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001775 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001776 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001777 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001778 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001779 AINPUT_SOURCE_KEYBOARD, nullptr);
1780 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001781
1782 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1783 AINPUT_SOURCE_ANY, KEY_A))
1784 << "Should return unknown when the device id is >= 0 but unknown.";
1785
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1787 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1788 << "Should return unknown when the device id is valid but the sources are not "
1789 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001791 ASSERT_EQ(AKEY_STATE_DOWN,
1792 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1793 KEY_A))
1794 << "Should return value provided by mapper when device id is valid and the device "
1795 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001796
1797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1798 AINPUT_SOURCE_TRACKBALL, KEY_A))
1799 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1800
1801 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1802 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1803 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1804}
1805
1806TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001807 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001808 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001809 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001810 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001811 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001812 AINPUT_SOURCE_KEYBOARD, nullptr);
1813 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001814
1815 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1816 AINPUT_SOURCE_ANY, SW_LID))
1817 << "Should return unknown when the device id is >= 0 but unknown.";
1818
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001819 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1820 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1821 << "Should return unknown when the device id is valid but the sources are not "
1822 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001823
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001824 ASSERT_EQ(AKEY_STATE_DOWN,
1825 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1826 SW_LID))
1827 << "Should return value provided by mapper when device id is valid and the device "
1828 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001829
1830 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1831 AINPUT_SOURCE_TRACKBALL, SW_LID))
1832 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1833
1834 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1835 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1836 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1837}
1838
1839TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001841 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001843 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001844 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001845 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001846
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001847 mapper.addSupportedKeyCode(AKEYCODE_A);
1848 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001849
1850 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1851 uint8_t flags[4] = { 0, 0, 0, 1 };
1852
1853 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1854 << "Should return false when device id is >= 0 but unknown.";
1855 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1856
1857 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001858 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1859 << "Should return false when device id is valid but the sources are not supported by "
1860 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001861 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1862
1863 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001864 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1865 keyCodes, flags))
1866 << "Should return value provided by mapper when device id is valid and the device "
1867 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001868 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1869
1870 flags[3] = 1;
1871 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1872 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1873 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1874
1875 flags[3] = 1;
1876 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1877 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1878 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1879}
1880
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001881TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001883 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
1885 NotifyConfigurationChangedArgs args;
1886
1887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1888 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1889}
1890
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001891TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001892 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001893 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001894 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001896 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001897 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001898 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001899 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001901 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001902 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001903 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1904
1905 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001906 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001907 ASSERT_EQ(when, event.when);
1908 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001910 ASSERT_EQ(EV_KEY, event.type);
1911 ASSERT_EQ(KEY_A, event.code);
1912 ASSERT_EQ(1, event.value);
1913}
1914
Garfield Tan1c7bc862020-01-28 13:24:04 -08001915TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001916 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001917 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001918 constexpr int32_t eventHubId = 1;
1919 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001920 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001921 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001922 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001923 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001924
1925 NotifyDeviceResetArgs resetArgs;
1926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001927 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001929 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001930 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001932 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001933 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001935 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001936 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001938 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001939 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001942 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001944 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001945 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001946}
1947
Garfield Tan1c7bc862020-01-28 13:24:04 -08001948TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1949 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001950 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001951 constexpr int32_t eventHubId = 1;
1952 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1953 // Must add at least one mapper or the device will be ignored!
1954 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001955 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001956 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1957
1958 NotifyDeviceResetArgs resetArgs;
1959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1960 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1961}
1962
Arthur Hungc23540e2018-11-29 20:42:11 +08001963TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001965 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001966 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001967 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001968 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1969 FakeInputMapper& mapper =
1970 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001971 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001972
1973 const uint8_t hdmi1 = 1;
1974
1975 // Associated touch screen with second display.
1976 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1977
1978 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001979 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001980 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001981 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001982 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001983 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001984 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001985 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001986 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001987 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001988
1989 // Add the device, and make sure all of the callbacks are triggered.
1990 // The device is added after the input port associations are processed since
1991 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001995 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001996
Arthur Hung2c9a3342019-07-23 14:18:59 +08001997 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001998 ASSERT_EQ(deviceId, device->getId());
1999 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2000 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002001
2002 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002004 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002005 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002006}
2007
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002008TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2009 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2010 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
2011 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2012 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2013 // Must add at least one mapper or the device will be ignored!
2014 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2015 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2016 mReader->pushNextDevice(device);
2017 mReader->pushNextDevice(device);
2018 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2019 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2020
2021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2022
2023 NotifyDeviceResetArgs resetArgs;
2024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2025 ASSERT_EQ(deviceId, resetArgs.deviceId);
2026 ASSERT_TRUE(device->isEnabled());
2027 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2028 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2029
2030 disableDevice(deviceId);
2031 mReader->loopOnce();
2032
2033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2034 ASSERT_EQ(deviceId, resetArgs.deviceId);
2035 ASSERT_FALSE(device->isEnabled());
2036 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2037 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2038
2039 enableDevice(deviceId);
2040 mReader->loopOnce();
2041
2042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2043 ASSERT_EQ(deviceId, resetArgs.deviceId);
2044 ASSERT_TRUE(device->isEnabled());
2045 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2046 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2047}
2048
2049TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2050 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2051 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
2052 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2053 // Add two subdevices to device
2054 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2055 FakeInputMapper& mapperDevice1 =
2056 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2057 FakeInputMapper& mapperDevice2 =
2058 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2059 mReader->pushNextDevice(device);
2060 mReader->pushNextDevice(device);
2061 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2063
2064 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2065 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2066
2067 ASSERT_EQ(AKEY_STATE_DOWN,
2068 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2069 ASSERT_EQ(AKEY_STATE_DOWN,
2070 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2071 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2072 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2073}
2074
Prabir Pradhan7e186182020-11-10 13:56:45 -08002075TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2076 NotifyPointerCaptureChangedArgs args;
2077
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002078 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002079 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2080 mReader->loopOnce();
2081 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002082 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2083 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002084
2085 mFakePolicy->setPointerCapture(false);
2086 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2087 mReader->loopOnce();
2088 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002089 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002090
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002091 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002092 // does not change.
2093 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2094 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002095 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002096}
2097
Chris Ye87143712020-11-10 05:05:58 +00002098class FakeVibratorInputMapper : public FakeInputMapper {
2099public:
2100 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2101 : FakeInputMapper(deviceContext, sources) {}
2102
2103 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2104};
2105
2106TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2107 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2108 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2109 constexpr int32_t eventHubId = 1;
2110 const char* DEVICE_LOCATION = "BLUETOOTH";
2111 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2112 FakeVibratorInputMapper& mapper =
2113 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2114 mReader->pushNextDevice(device);
2115
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2117 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2118
2119 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2120 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2121}
2122
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002123// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002124
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002125class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002126public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002127 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002128
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002129 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002130
2131 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2132
2133 void dump(std::string& dump) override {}
2134
2135 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2136 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002137 }
2138
Chris Yee2b1e5c2021-03-10 22:45:12 -08002139 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2140 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002141 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002142
2143 bool setLightColor(int32_t lightId, int32_t color) override {
2144 getDeviceContext().setLightBrightness(lightId, color >> 24);
2145 return true;
2146 }
2147
2148 std::optional<int32_t> getLightColor(int32_t lightId) override {
2149 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2150 if (!result.has_value()) {
2151 return std::nullopt;
2152 }
2153 return result.value() << 24;
2154 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002155
2156 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2157
2158 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2159
2160private:
2161 InputDeviceContext& mDeviceContext;
2162 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2163 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002164};
2165
Chris Yee2b1e5c2021-03-10 22:45:12 -08002166TEST_F(InputReaderTest, BatteryGetCapacity) {
2167 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2168 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2169 constexpr int32_t eventHubId = 1;
2170 const char* DEVICE_LOCATION = "BLUETOOTH";
2171 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002172 FakePeripheralController& controller =
2173 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002174 mReader->pushNextDevice(device);
2175
2176 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2177
2178 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2179 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2180}
2181
2182TEST_F(InputReaderTest, BatteryGetStatus) {
2183 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2184 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2185 constexpr int32_t eventHubId = 1;
2186 const char* DEVICE_LOCATION = "BLUETOOTH";
2187 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002188 FakePeripheralController& controller =
2189 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002190 mReader->pushNextDevice(device);
2191
2192 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2193
2194 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2195 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2196}
2197
Chris Ye3fdbfef2021-01-06 18:45:18 -08002198TEST_F(InputReaderTest, LightGetColor) {
2199 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2200 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2201 constexpr int32_t eventHubId = 1;
2202 const char* DEVICE_LOCATION = "BLUETOOTH";
2203 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002204 FakePeripheralController& controller =
2205 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002206 mReader->pushNextDevice(device);
2207 RawLightInfo info = {.id = 1,
2208 .name = "Mono",
2209 .maxBrightness = 255,
2210 .flags = InputLightClass::BRIGHTNESS,
2211 .path = ""};
2212 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2213 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2214
2215 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002216
Chris Yee2b1e5c2021-03-10 22:45:12 -08002217 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2218 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002219 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2220 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2221}
2222
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002223// --- InputReaderIntegrationTest ---
2224
2225// These tests create and interact with the InputReader only through its interface.
2226// The InputReader is started during SetUp(), which starts its processing in its own
2227// thread. The tests use linux uinput to emulate input devices.
2228// NOTE: Interacting with the physical device while these tests are running may cause
2229// the tests to fail.
2230class InputReaderIntegrationTest : public testing::Test {
2231protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002232 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002233 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002234 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002235
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002236 std::shared_ptr<FakePointerController> mFakePointerController;
2237
Chris Yea52ade12020-08-27 16:49:20 -07002238 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002239 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002240 mFakePointerController = std::make_shared<FakePointerController>();
2241 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002242 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2243 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002244
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002245 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2246 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002247 ASSERT_EQ(mReader->start(), OK);
2248
2249 // Since this test is run on a real device, all the input devices connected
2250 // to the test device will show up in mReader. We wait for those input devices to
2251 // show up before beginning the tests.
2252 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2253 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2254 }
2255
Chris Yea52ade12020-08-27 16:49:20 -07002256 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002257 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002258 mReader.reset();
2259 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002260 mFakePolicy.clear();
2261 }
2262};
2263
2264TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2265 // An invalid input device that is only used for this test.
2266 class InvalidUinputDevice : public UinputDevice {
2267 public:
2268 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2269
2270 private:
2271 void configureDevice(int fd, uinput_user_dev* device) override {}
2272 };
2273
2274 const size_t numDevices = mFakePolicy->getInputDevices().size();
2275
2276 // UinputDevice does not set any event or key bits, so InputReader should not
2277 // consider it as a valid device.
2278 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2279 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2280 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2281 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2282
2283 invalidDevice.reset();
2284 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2285 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2286 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2287}
2288
2289TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2290 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2291
2292 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2294 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2295 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2296
2297 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002298 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002299 const auto& it =
2300 std::find_if(inputDevices.begin(), inputDevices.end(),
2301 [&keyboard](const InputDeviceInfo& info) {
2302 return info.getIdentifier().name == keyboard->getName();
2303 });
2304
2305 ASSERT_NE(it, inputDevices.end());
2306 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2307 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2308 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002309
2310 keyboard.reset();
2311 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2312 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2313 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2314}
2315
2316TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2317 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2318 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2319
2320 NotifyConfigurationChangedArgs configChangedArgs;
2321 ASSERT_NO_FATAL_FAILURE(
2322 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002323 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002324 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2325
2326 NotifyKeyArgs keyArgs;
2327 keyboard->pressAndReleaseHomeKey();
2328 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2329 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002330 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002331 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002332 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002333 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002334 prevTimestamp = keyArgs.eventTime;
2335
2336 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2337 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002338 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002339 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002340 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002343/**
2344 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2345 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2346 * are passed to the listener.
2347 */
2348static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2349TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2350 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2352 NotifyKeyArgs keyArgs;
2353
2354 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2357 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2358
2359 controller->pressAndReleaseKey(BTN_GEAR_UP);
2360 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2362 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2363}
2364
Arthur Hungaab25622020-01-16 11:22:11 +08002365// --- TouchProcessTest ---
2366class TouchIntegrationTest : public InputReaderIntegrationTest {
2367protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002368 const std::string UNIQUE_ID = "local:0";
2369
Chris Yea52ade12020-08-27 16:49:20 -07002370 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002371 InputReaderIntegrationTest::SetUp();
2372 // At least add an internal display.
2373 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2374 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002375 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002376
2377 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2378 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2379 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2380 }
2381
2382 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2383 int32_t orientation, const std::string& uniqueId,
2384 std::optional<uint8_t> physicalPort,
2385 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002386 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2387 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002388 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2389 }
2390
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002391 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2392 NotifyMotionArgs args;
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2394 EXPECT_EQ(action, args.action);
2395 ASSERT_EQ(points.size(), args.pointerCount);
2396 for (size_t i = 0; i < args.pointerCount; i++) {
2397 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2398 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2399 }
2400 }
2401
Arthur Hungaab25622020-01-16 11:22:11 +08002402 std::unique_ptr<UinputTouchScreen> mDevice;
2403};
2404
2405TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2406 NotifyMotionArgs args;
2407 const Point centerPoint = mDevice->getCenterPoint();
2408
2409 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002410 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002411 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002412 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2415
2416 // ACTION_MOVE
2417 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002418 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2421
2422 // ACTION_UP
2423 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002424 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2426 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2427}
2428
2429TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2430 NotifyMotionArgs args;
2431 const Point centerPoint = mDevice->getCenterPoint();
2432
2433 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002434 mDevice->sendSlot(FIRST_SLOT);
2435 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002436 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002437 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2439 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2440
2441 // ACTION_POINTER_DOWN (Second slot)
2442 const Point secondPoint = centerPoint + Point(100, 100);
2443 mDevice->sendSlot(SECOND_SLOT);
2444 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002445 mDevice->sendDown(secondPoint);
2446 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002448 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002449
2450 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002451 mDevice->sendMove(secondPoint + Point(1, 1));
2452 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2455
2456 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002457 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002458 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002459 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002460 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002461
2462 // ACTION_UP
2463 mDevice->sendSlot(FIRST_SLOT);
2464 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002465 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2468}
2469
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002470/**
2471 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2472 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2473 * data?
2474 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2475 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2476 * for Pointer 0 only is generated after.
2477 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2478 * events, we will not miss any information.
2479 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2480 * event generated afterwards that contains the newest movement of pointer 0.
2481 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2482 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2483 * losing information about non-palm pointers.
2484 */
2485TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2486 NotifyMotionArgs args;
2487 const Point centerPoint = mDevice->getCenterPoint();
2488
2489 // ACTION_DOWN
2490 mDevice->sendSlot(FIRST_SLOT);
2491 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2492 mDevice->sendDown(centerPoint);
2493 mDevice->sendSync();
2494 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2495
2496 // ACTION_POINTER_DOWN (Second slot)
2497 const Point secondPoint = centerPoint + Point(100, 100);
2498 mDevice->sendSlot(SECOND_SLOT);
2499 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2500 mDevice->sendDown(secondPoint);
2501 mDevice->sendSync();
2502 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2503
2504 // ACTION_MOVE (First slot)
2505 mDevice->sendSlot(FIRST_SLOT);
2506 mDevice->sendMove(centerPoint + Point(5, 5));
2507 // ACTION_POINTER_UP (Second slot)
2508 mDevice->sendSlot(SECOND_SLOT);
2509 mDevice->sendPointerUp();
2510 // Send a single sync for the above 2 pointer updates
2511 mDevice->sendSync();
2512
2513 // First, we should get POINTER_UP for the second pointer
2514 assertReceivedMotion(ACTION_POINTER_1_UP,
2515 {/*first pointer */ centerPoint + Point(5, 5),
2516 /*second pointer*/ secondPoint});
2517
2518 // Next, the MOVE event for the first pointer
2519 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2520}
2521
2522/**
2523 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2524 * move, and then it will go up, all in the same frame.
2525 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2526 * gets sent to the listener.
2527 */
2528TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2529 NotifyMotionArgs args;
2530 const Point centerPoint = mDevice->getCenterPoint();
2531
2532 // ACTION_DOWN
2533 mDevice->sendSlot(FIRST_SLOT);
2534 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2535 mDevice->sendDown(centerPoint);
2536 mDevice->sendSync();
2537 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2538
2539 // ACTION_POINTER_DOWN (Second slot)
2540 const Point secondPoint = centerPoint + Point(100, 100);
2541 mDevice->sendSlot(SECOND_SLOT);
2542 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2543 mDevice->sendDown(secondPoint);
2544 mDevice->sendSync();
2545 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2546
2547 // ACTION_MOVE (First slot)
2548 mDevice->sendSlot(FIRST_SLOT);
2549 mDevice->sendMove(centerPoint + Point(5, 5));
2550 // ACTION_POINTER_UP (Second slot)
2551 mDevice->sendSlot(SECOND_SLOT);
2552 mDevice->sendMove(secondPoint + Point(6, 6));
2553 mDevice->sendPointerUp();
2554 // Send a single sync for the above 2 pointer updates
2555 mDevice->sendSync();
2556
2557 // First, we should get POINTER_UP for the second pointer
2558 // The movement of the second pointer during the liftoff frame is ignored.
2559 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2560 assertReceivedMotion(ACTION_POINTER_1_UP,
2561 {/*first pointer */ centerPoint + Point(5, 5),
2562 /*second pointer*/ secondPoint});
2563
2564 // Next, the MOVE event for the first pointer
2565 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2566}
2567
Arthur Hungaab25622020-01-16 11:22:11 +08002568TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2569 NotifyMotionArgs args;
2570 const Point centerPoint = mDevice->getCenterPoint();
2571
2572 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002573 mDevice->sendSlot(FIRST_SLOT);
2574 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002575 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002576 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002577 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2579
arthurhungcc7f9802020-04-30 17:55:40 +08002580 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002581 const Point secondPoint = centerPoint + Point(100, 100);
2582 mDevice->sendSlot(SECOND_SLOT);
2583 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2584 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002585 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002587 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002588
arthurhungcc7f9802020-04-30 17:55:40 +08002589 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002590 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002591 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002592 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2594
arthurhungcc7f9802020-04-30 17:55:40 +08002595 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2596 // a palm event.
2597 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002598 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002599 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002600 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002601 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002602 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002603
arthurhungcc7f9802020-04-30 17:55:40 +08002604 // Send up to second slot, expect first slot send moving.
2605 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002609
arthurhungcc7f9802020-04-30 17:55:40 +08002610 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002611 mDevice->sendSlot(FIRST_SLOT);
2612 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002613 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002614
arthurhungcc7f9802020-04-30 17:55:40 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2616 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002617}
2618
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620class InputDeviceTest : public testing::Test {
2621protected:
2622 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002623 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 static const int32_t DEVICE_ID;
2625 static const int32_t DEVICE_GENERATION;
2626 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002627 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002628 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002630 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002632 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002633 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002634 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635
Chris Yea52ade12020-08-27 16:49:20 -07002636 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002637 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002639 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002640 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002641 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 InputDeviceIdentifier identifier;
2643 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002644 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002645 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002646 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002647 mReader->pushNextDevice(mDevice);
2648 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2649 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650 }
2651
Chris Yea52ade12020-08-27 16:49:20 -07002652 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002653 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002655 }
2656};
2657
2658const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002659const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002660const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2662const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002663const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2664 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002665const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666
2667TEST_F(InputDeviceTest, ImmutableProperties) {
2668 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002669 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002670 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671}
2672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002673TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2674 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002675}
2676
Michael Wrightd02c5b62014-02-10 15:10:22 -08002677TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2678 // Configuration.
2679 InputReaderConfiguration config;
2680 mDevice->configure(ARBITRARY_TIME, &config, 0);
2681
2682 // Reset.
2683 mDevice->reset(ARBITRARY_TIME);
2684
2685 NotifyDeviceResetArgs resetArgs;
2686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2687 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2688 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2689
2690 // Metadata.
2691 ASSERT_TRUE(mDevice->isIgnored());
2692 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2693
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002694 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002695 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002696 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2698 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2699
2700 // State queries.
2701 ASSERT_EQ(0, mDevice->getMetaState());
2702
2703 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2704 << "Ignored device should return unknown key code state.";
2705 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2706 << "Ignored device should return unknown scan code state.";
2707 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2708 << "Ignored device should return unknown switch state.";
2709
2710 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2711 uint8_t flags[2] = { 0, 1 };
2712 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2713 << "Ignored device should never mark any key codes.";
2714 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2715 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2716}
2717
2718TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2719 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002720 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002721
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002722 FakeInputMapper& mapper1 =
2723 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002724 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2725 mapper1.setMetaState(AMETA_ALT_ON);
2726 mapper1.addSupportedKeyCode(AKEYCODE_A);
2727 mapper1.addSupportedKeyCode(AKEYCODE_B);
2728 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2729 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2730 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2731 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2732 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002733
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002734 FakeInputMapper& mapper2 =
2735 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002736 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737
2738 InputReaderConfiguration config;
2739 mDevice->configure(ARBITRARY_TIME, &config, 0);
2740
2741 String8 propertyValue;
2742 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2743 << "Device should have read configuration during configuration phase.";
2744 ASSERT_STREQ("value", propertyValue.string());
2745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002746 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2747 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748
2749 // Reset
2750 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002751 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2752 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002753
2754 NotifyDeviceResetArgs resetArgs;
2755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2756 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2757 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2758
2759 // Metadata.
2760 ASSERT_FALSE(mDevice->isIgnored());
2761 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2762
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002763 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002765 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002766 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2767 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2768
2769 // State queries.
2770 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2771 << "Should query mappers and combine meta states.";
2772
2773 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2774 << "Should return unknown key code state when source not supported.";
2775 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2776 << "Should return unknown scan code state when source not supported.";
2777 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2778 << "Should return unknown switch state when source not supported.";
2779
2780 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2781 << "Should query mapper when source is supported.";
2782 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2783 << "Should query mapper when source is supported.";
2784 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2785 << "Should query mapper when source is supported.";
2786
2787 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2788 uint8_t flags[4] = { 0, 0, 0, 1 };
2789 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2790 << "Should do nothing when source is unsupported.";
2791 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2792 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2793 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2794 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2795
2796 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2797 << "Should query mapper when source is supported.";
2798 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2799 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2800 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2801 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2802
2803 // Event handling.
2804 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002805 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 mDevice->process(&event, 1);
2807
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002808 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2809 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810}
2811
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812// A single input device is associated with a specific display. Check that:
2813// 1. Device is disabled if the viewport corresponding to the associated display is not found
2814// 2. Device is disabled when setEnabled API is called
2815TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002816 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002817
2818 // First Configuration.
2819 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2820
2821 // Device should be enabled by default.
2822 ASSERT_TRUE(mDevice->isEnabled());
2823
2824 // Prepare associated info.
2825 constexpr uint8_t hdmi = 1;
2826 const std::string UNIQUE_ID = "local:1";
2827
2828 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2829 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2830 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2831 // Device should be disabled because it is associated with a specific display via
2832 // input port <-> display port association, but the corresponding display is not found
2833 ASSERT_FALSE(mDevice->isEnabled());
2834
2835 // Prepare displays.
2836 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002837 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2838 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002839 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2840 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2841 ASSERT_TRUE(mDevice->isEnabled());
2842
2843 // Device should be disabled after set disable.
2844 mFakePolicy->addDisabledDevice(mDevice->getId());
2845 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2846 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2847 ASSERT_FALSE(mDevice->isEnabled());
2848
2849 // Device should still be disabled even found the associated display.
2850 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2851 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2852 ASSERT_FALSE(mDevice->isEnabled());
2853}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Christine Franks1ba71cc2021-04-07 14:37:42 -07002855TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2856 // Device should be enabled by default.
2857 mFakePolicy->clearViewports();
2858 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2859 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2860 ASSERT_TRUE(mDevice->isEnabled());
2861
2862 // Device should be disabled because it is associated with a specific display, but the
2863 // corresponding display is not found.
2864 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
Christine Franks2a2293c2022-01-18 11:51:16 -08002865 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2867 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2868 ASSERT_FALSE(mDevice->isEnabled());
2869
2870 // Device should be enabled when a display is found.
2871 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2872 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2873 NO_PORT, ViewportType::INTERNAL);
2874 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2875 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2876 ASSERT_TRUE(mDevice->isEnabled());
2877
2878 // Device should be disabled after set disable.
2879 mFakePolicy->addDisabledDevice(mDevice->getId());
2880 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2881 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2882 ASSERT_FALSE(mDevice->isEnabled());
2883
2884 // Device should still be disabled even found the associated display.
2885 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2886 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2887 ASSERT_FALSE(mDevice->isEnabled());
2888}
2889
Christine Franks2a2293c2022-01-18 11:51:16 -08002890TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2891 mFakePolicy->clearViewports();
2892 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2894
2895 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2896 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2897 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2898 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2899 NO_PORT, ViewportType::INTERNAL);
2900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2902 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2903}
2904
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905// --- InputMapperTest ---
2906
2907class InputMapperTest : public testing::Test {
2908protected:
2909 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002910 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002911 static const int32_t DEVICE_ID;
2912 static const int32_t DEVICE_GENERATION;
2913 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002914 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002915 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002916
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002917 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002919 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002920 std::unique_ptr<InstrumentedInputReader> mReader;
2921 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002922
Chris Ye1b0c7342020-07-28 21:57:03 -07002923 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002924 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002926 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002927 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002928 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002929 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002930 }
2931
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002932 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002933 SetUp(DEVICE_CLASSES);
2934 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002935
Chris Yea52ade12020-08-27 16:49:20 -07002936 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002937 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002939 }
2940
2941 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002942 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943 }
2944
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002945 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002946 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002947 mReader->requestRefreshConfiguration(changes);
2948 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002949 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002950 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2951 }
2952
arthurhungdcef2dc2020-08-11 14:47:50 +08002953 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2954 const std::string& location, int32_t eventHubId,
2955 Flags<InputDeviceClass> classes) {
2956 InputDeviceIdentifier identifier;
2957 identifier.name = name;
2958 identifier.location = location;
2959 std::shared_ptr<InputDevice> device =
2960 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2961 identifier);
2962 mReader->pushNextDevice(device);
2963 mFakeEventHub->addDevice(eventHubId, name, classes);
2964 mReader->loopOnce();
2965 return device;
2966 }
2967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002968 template <class T, typename... Args>
2969 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002970 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002971 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002973 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002974 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002975 }
2976
2977 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002978 int32_t orientation, const std::string& uniqueId,
2979 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002980 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2981 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002982 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2983 }
2984
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002985 void clearViewports() {
2986 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987 }
2988
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002989 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2990 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 RawEvent event;
2992 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002994 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 event.type = type;
2996 event.code = code;
2997 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002998 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002999 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000 }
3001
3002 static void assertMotionRange(const InputDeviceInfo& info,
3003 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3004 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003005 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003006 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3007 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3008 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3009 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3010 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3011 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3012 }
3013
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003014 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3015 float size, float touchMajor, float touchMinor, float toolMajor,
3016 float toolMinor, float orientation, float distance,
3017 float scaledAxisEpsilon = 1.f) {
3018 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3019 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3021 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003022 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3023 scaledAxisEpsilon);
3024 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3025 scaledAxisEpsilon);
3026 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3027 scaledAxisEpsilon);
3028 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3029 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3031 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3032 }
3033
Michael Wright17db18e2020-06-26 20:51:44 +01003034 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003036 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037 ASSERT_NEAR(x, actualX, 1);
3038 ASSERT_NEAR(y, actualY, 1);
3039 }
3040};
3041
3042const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003043const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003044const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3046const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07003047const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3048 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003049const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
3051// --- SwitchInputMapperTest ---
3052
3053class SwitchInputMapperTest : public InputMapperTest {
3054protected:
3055};
3056
3057TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003058 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003060 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061}
3062
3063TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003066 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003067 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003069 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071}
3072
3073TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080
3081 NotifySwitchArgs args;
3082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3083 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003084 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3085 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 args.switchMask);
3087 ASSERT_EQ(uint32_t(0), args.policyFlags);
3088}
3089
Chris Ye87143712020-11-10 05:05:58 +00003090// --- VibratorInputMapperTest ---
3091class VibratorInputMapperTest : public InputMapperTest {
3092protected:
3093 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3094};
3095
3096TEST_F(VibratorInputMapperTest, GetSources) {
3097 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3098
3099 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3100}
3101
3102TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3103 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3104
3105 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3106}
3107
3108TEST_F(VibratorInputMapperTest, Vibrate) {
3109 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003110 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003111 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3112
3113 VibrationElement pattern(2);
3114 VibrationSequence sequence(2);
3115 pattern.duration = std::chrono::milliseconds(200);
3116 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3117 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3118 sequence.addElement(pattern);
3119 pattern.duration = std::chrono::milliseconds(500);
3120 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3121 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3122 sequence.addElement(pattern);
3123
3124 std::vector<int64_t> timings = {0, 1};
3125 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3126
3127 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003128 // Start vibrating
3129 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003130 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003131 // Verify vibrator state listener was notified.
3132 mReader->loopOnce();
3133 NotifyVibratorStateArgs args;
3134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3135 ASSERT_EQ(DEVICE_ID, args.deviceId);
3136 ASSERT_TRUE(args.isOn);
3137 // Stop vibrating
3138 mapper.cancelVibrate(VIBRATION_TOKEN);
3139 ASSERT_FALSE(mapper.isVibrating());
3140 // Verify vibrator state listener was notified.
3141 mReader->loopOnce();
3142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3143 ASSERT_EQ(DEVICE_ID, args.deviceId);
3144 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003145}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003146
Chris Yef59a2f42020-10-16 12:55:26 -07003147// --- SensorInputMapperTest ---
3148
3149class SensorInputMapperTest : public InputMapperTest {
3150protected:
3151 static const int32_t ACCEL_RAW_MIN;
3152 static const int32_t ACCEL_RAW_MAX;
3153 static const int32_t ACCEL_RAW_FUZZ;
3154 static const int32_t ACCEL_RAW_FLAT;
3155 static const int32_t ACCEL_RAW_RESOLUTION;
3156
3157 static const int32_t GYRO_RAW_MIN;
3158 static const int32_t GYRO_RAW_MAX;
3159 static const int32_t GYRO_RAW_FUZZ;
3160 static const int32_t GYRO_RAW_FLAT;
3161 static const int32_t GYRO_RAW_RESOLUTION;
3162
3163 static const float GRAVITY_MS2_UNIT;
3164 static const float DEGREE_RADIAN_UNIT;
3165
3166 void prepareAccelAxes();
3167 void prepareGyroAxes();
3168 void setAccelProperties();
3169 void setGyroProperties();
3170 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3171};
3172
3173const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3174const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3175const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3176const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3177const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3178
3179const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3180const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3181const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3182const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3183const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3184
3185const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3186const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3187
3188void SensorInputMapperTest::prepareAccelAxes() {
3189 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3190 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3191 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3192 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3193 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3194 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3195}
3196
3197void SensorInputMapperTest::prepareGyroAxes() {
3198 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3199 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3200 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3201 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3203 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3204}
3205
3206void SensorInputMapperTest::setAccelProperties() {
3207 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3208 /* sensorDataIndex */ 0);
3209 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3210 /* sensorDataIndex */ 1);
3211 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3212 /* sensorDataIndex */ 2);
3213 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3214 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3215 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3216 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3217 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3218}
3219
3220void SensorInputMapperTest::setGyroProperties() {
3221 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3222 /* sensorDataIndex */ 0);
3223 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3224 /* sensorDataIndex */ 1);
3225 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3226 /* sensorDataIndex */ 2);
3227 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3228 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3229 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3230 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3231 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3232}
3233
3234TEST_F(SensorInputMapperTest, GetSources) {
3235 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3236
3237 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3238}
3239
3240TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3241 setAccelProperties();
3242 prepareAccelAxes();
3243 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3244
3245 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3246 std::chrono::microseconds(10000),
3247 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003248 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003254
3255 NotifySensorArgs args;
3256 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3257 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3258 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3259
3260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3261 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3262 ASSERT_EQ(args.deviceId, DEVICE_ID);
3263 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3264 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3265 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3266 ASSERT_EQ(args.values, values);
3267 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3268}
3269
3270TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3271 setGyroProperties();
3272 prepareGyroAxes();
3273 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3274
3275 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3276 std::chrono::microseconds(10000),
3277 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003278 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003284
3285 NotifySensorArgs args;
3286 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3287 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3288 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3289
3290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3291 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3292 ASSERT_EQ(args.deviceId, DEVICE_ID);
3293 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3294 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3295 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3296 ASSERT_EQ(args.values, values);
3297 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3298}
3299
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300// --- KeyboardInputMapperTest ---
3301
3302class KeyboardInputMapperTest : public InputMapperTest {
3303protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003304 const std::string UNIQUE_ID = "local:0";
3305
3306 void prepareDisplay(int32_t orientation);
3307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003309 int32_t originalKeyCode, int32_t rotatedKeyCode,
3310 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311};
3312
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003313/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3314 * orientation.
3315 */
3316void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003317 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3318 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003319}
3320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003322 int32_t originalScanCode, int32_t originalKeyCode,
3323 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324 NotifyKeyArgs args;
3325
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3328 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3329 ASSERT_EQ(originalScanCode, args.scanCode);
3330 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003331 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3335 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3336 ASSERT_EQ(originalScanCode, args.scanCode);
3337 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003338 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339}
3340
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 KeyboardInputMapper& mapper =
3343 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3344 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003346 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347}
3348
3349TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3350 const int32_t USAGE_A = 0x070004;
3351 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003352 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3353 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003354 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3355 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3356 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 KeyboardInputMapper& mapper =
3359 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3360 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003361 // Initial metastate to AMETA_NONE.
3362 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3363 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
3365 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 NotifyKeyArgs args;
3368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3369 ASSERT_EQ(DEVICE_ID, args.deviceId);
3370 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3371 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3372 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3373 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3374 ASSERT_EQ(KEY_HOME, args.scanCode);
3375 ASSERT_EQ(AMETA_NONE, args.metaState);
3376 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3377 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3378 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3379
3380 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3383 ASSERT_EQ(DEVICE_ID, args.deviceId);
3384 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3385 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3386 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3387 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3388 ASSERT_EQ(KEY_HOME, args.scanCode);
3389 ASSERT_EQ(AMETA_NONE, args.metaState);
3390 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3391 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3392 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3393
3394 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(DEVICE_ID, args.deviceId);
3399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3400 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3402 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3403 ASSERT_EQ(0, args.scanCode);
3404 ASSERT_EQ(AMETA_NONE, args.metaState);
3405 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3406 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3407 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3408
3409 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3411 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3413 ASSERT_EQ(DEVICE_ID, args.deviceId);
3414 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3415 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3417 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3418 ASSERT_EQ(0, args.scanCode);
3419 ASSERT_EQ(AMETA_NONE, args.metaState);
3420 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3421 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3423
3424 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(DEVICE_ID, args.deviceId);
3429 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3430 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3431 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3432 ASSERT_EQ(0, args.keyCode);
3433 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3434 ASSERT_EQ(AMETA_NONE, args.metaState);
3435 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3436 ASSERT_EQ(0U, args.policyFlags);
3437 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3438
3439 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3441 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(DEVICE_ID, args.deviceId);
3444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3445 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3446 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3447 ASSERT_EQ(0, args.keyCode);
3448 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3449 ASSERT_EQ(AMETA_NONE, args.metaState);
3450 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3451 ASSERT_EQ(0U, args.policyFlags);
3452 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3453}
3454
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455/**
3456 * Ensure that the readTime is set to the time when the EV_KEY is received.
3457 */
3458TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3460
3461 KeyboardInputMapper& mapper =
3462 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3463 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3464 NotifyKeyArgs args;
3465
3466 // Key down
3467 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3469 ASSERT_EQ(12, args.readTime);
3470
3471 // Key up
3472 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3474 ASSERT_EQ(15, args.readTime);
3475}
3476
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003480 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3482 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 KeyboardInputMapper& mapper =
3485 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3486 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
arthurhungc903df12020-08-11 15:08:42 +08003488 // Initial metastate to AMETA_NONE.
3489 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3490 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
3492 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 NotifyKeyArgs args;
3495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003498 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499
3500 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003501 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3509 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511
3512 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3515 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003517 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518}
3519
3520TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3523 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3524 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 KeyboardInputMapper& mapper =
3527 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3528 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003530 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3532 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3534 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3535 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3536 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3538 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3539}
3540
3541TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 KeyboardInputMapper& mapper =
3549 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3550 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003552 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003553 ASSERT_NO_FATAL_FAILURE(
3554 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3556 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3558 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3560 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003562 clearViewports();
3563 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003564 ASSERT_NO_FATAL_FAILURE(
3565 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3567 AKEYCODE_DPAD_UP, DISPLAY_ID));
3568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3569 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3571 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003573 clearViewports();
3574 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003575 ASSERT_NO_FATAL_FAILURE(
3576 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3577 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3578 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3579 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3580 AKEYCODE_DPAD_UP, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3582 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003584 clearViewports();
3585 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586 ASSERT_NO_FATAL_FAILURE(
3587 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3589 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3591 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3593 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594
3595 // Special case: if orientation changes while key is down, we still emit the same keycode
3596 // in the key up as we did in the key down.
3597 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003598 clearViewports();
3599 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3603 ASSERT_EQ(KEY_UP, args.scanCode);
3604 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3605
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003606 clearViewports();
3607 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3610 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3611 ASSERT_EQ(KEY_UP, args.scanCode);
3612 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3613}
3614
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003615TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3616 // If the keyboard is not orientation aware,
3617 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003620 KeyboardInputMapper& mapper =
3621 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3622 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003623 NotifyKeyArgs args;
3624
3625 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3630 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3631
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003632 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3637 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3638}
3639
3640TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3641 // If the keyboard is orientation aware,
3642 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003644
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 KeyboardInputMapper& mapper =
3647 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649 NotifyKeyArgs args;
3650
3651 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3652 // ^--- already checked by the previous test
3653
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003654 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003655 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3660 ASSERT_EQ(DISPLAY_ID, args.displayId);
3661
3662 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003663 clearViewports();
3664 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003665 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3670 ASSERT_EQ(newDisplayId, args.displayId);
3671}
3672
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 KeyboardInputMapper& mapper =
3675 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003678 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003679 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003681 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683}
3684
Philip Junker4af3b3d2021-12-14 10:36:55 +01003685TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3686 KeyboardInputMapper& mapper =
3687 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3688 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3689
3690 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3691 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3692 << "If a mapping is available, the result is equal to the mapping";
3693
3694 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3695 << "If no mapping is available, the result is the key location";
3696}
3697
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 KeyboardInputMapper& mapper =
3700 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3701 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003703 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003706 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708}
3709
3710TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 KeyboardInputMapper& mapper =
3712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
3717 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3718 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720 ASSERT_TRUE(flags[0]);
3721 ASSERT_FALSE(flags[1]);
3722}
3723
3724TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3726 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3727 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3728 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3730 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 KeyboardInputMapper& mapper =
3733 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003735 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003736 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3737 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
3739 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3742 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
3744 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003747 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
3752 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3756 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3757 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003758 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3764 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775
3776 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3781 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783
3784 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3789 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791}
3792
Chris Yea52ade12020-08-27 16:49:20 -07003793TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3794 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3796 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3797 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3798
3799 KeyboardInputMapper& mapper =
3800 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3801 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3802
3803 // Initial metastate should be AMETA_NONE as no meta keys added.
3804 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3805 // Meta state should be AMETA_NONE after reset
3806 mapper.reset(ARBITRARY_TIME);
3807 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3808 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3809 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3810 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3811
3812 NotifyKeyArgs args;
3813 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3816 ASSERT_EQ(AMETA_NONE, args.metaState);
3817 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3818 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3819 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3820
3821 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003822 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3824 ASSERT_EQ(AMETA_NONE, args.metaState);
3825 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3826 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3827 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3828}
3829
Arthur Hung2c9a3342019-07-23 14:18:59 +08003830TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3831 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3835 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003836
3837 // keyboard 2.
3838 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003839 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003840 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003842 std::shared_ptr<InputDevice> device2 =
3843 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3844 Flags<InputDeviceClass>(0));
3845
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003846 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3847 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3848 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3849 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003850
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 KeyboardInputMapper& mapper =
3852 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3853 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003856 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003858 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3859 device2->reset(ARBITRARY_TIME);
3860
3861 // Prepared displays and associated info.
3862 constexpr uint8_t hdmi1 = 0;
3863 constexpr uint8_t hdmi2 = 1;
3864 const std::string SECONDARY_UNIQUE_ID = "local:1";
3865
3866 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3867 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3868
3869 // No associated display viewport found, should disable the device.
3870 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3871 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3872 ASSERT_FALSE(device2->isEnabled());
3873
3874 // Prepare second display.
3875 constexpr int32_t newDisplayId = 2;
3876 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003877 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003878 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003879 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003880 // Default device will reconfigure above, need additional reconfiguration for another device.
3881 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3882 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3883
3884 // Device should be enabled after the associated display is found.
3885 ASSERT_TRUE(mDevice->isEnabled());
3886 ASSERT_TRUE(device2->isEnabled());
3887
3888 // Test pad key events
3889 ASSERT_NO_FATAL_FAILURE(
3890 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3891 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3892 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3893 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3894 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3895 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3896 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3897
3898 ASSERT_NO_FATAL_FAILURE(
3899 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3900 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3901 AKEYCODE_DPAD_RIGHT, newDisplayId));
3902 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3903 AKEYCODE_DPAD_DOWN, newDisplayId));
3904 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3905 AKEYCODE_DPAD_LEFT, newDisplayId));
3906}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003907
arthurhungc903df12020-08-11 15:08:42 +08003908TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3909 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3910 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3911 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3912 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3915
3916 KeyboardInputMapper& mapper =
3917 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3918 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3919 // Initial metastate to AMETA_NONE.
3920 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3921 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3922
3923 // Initialization should have turned all of the lights off.
3924 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3925 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3926 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3927
3928 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003931 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3932 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3933
3934 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003937 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3938 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3939
3940 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003943 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3944 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3945
3946 mFakeEventHub->removeDevice(EVENTHUB_ID);
3947 mReader->loopOnce();
3948
3949 // keyboard 2 should default toggle keys.
3950 const std::string USB2 = "USB2";
3951 const std::string DEVICE_NAME2 = "KEYBOARD2";
3952 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3953 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3954 std::shared_ptr<InputDevice> device2 =
3955 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3956 Flags<InputDeviceClass>(0));
3957 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3958 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3959 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3960 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3961 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3962 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3963
arthurhung6fe95782020-10-05 22:41:16 +08003964 KeyboardInputMapper& mapper2 =
3965 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003967 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3968 device2->reset(ARBITRARY_TIME);
3969
3970 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3971 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3972 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003973 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3974 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003975}
3976
Arthur Hungcb40a002021-08-03 14:31:01 +00003977TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3978 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3981
3982 // Suppose we have two mappers. (DPAD + KEYBOARD)
3983 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3984 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3985 KeyboardInputMapper& mapper =
3986 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3987 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3988 // Initialize metastate to AMETA_NUM_LOCK_ON.
3989 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3990 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3991
3992 mReader->toggleCapsLockState(DEVICE_ID);
3993 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3994}
3995
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003996// --- KeyboardInputMapperTest_ExternalDevice ---
3997
3998class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3999protected:
Chris Yea52ade12020-08-27 16:49:20 -07004000 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004001};
4002
4003TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004004 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4005 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004006
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4008 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4009 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4010 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004012 KeyboardInputMapper& mapper =
4013 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4014 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004015
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004017 NotifyKeyArgs args;
4018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4019 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4020
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004021 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4023 ASSERT_EQ(uint32_t(0), args.policyFlags);
4024
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4027 ASSERT_EQ(uint32_t(0), args.policyFlags);
4028
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4031 ASSERT_EQ(uint32_t(0), args.policyFlags);
4032
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4035 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4036
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004037 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4039 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4040}
4041
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004042TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004043 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004044
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4047 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004048
Powei Fengd041c5d2019-05-03 17:11:33 -07004049 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 KeyboardInputMapper& mapper =
4051 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4052 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004053
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004055 NotifyKeyArgs args;
4056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4057 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4058
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004059 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4061 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4062
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4065 ASSERT_EQ(uint32_t(0), args.policyFlags);
4066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004067 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4069 ASSERT_EQ(uint32_t(0), args.policyFlags);
4070
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4073 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4074
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004075 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4077 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4078}
4079
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080// --- CursorInputMapperTest ---
4081
4082class CursorInputMapperTest : public InputMapperTest {
4083protected:
4084 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4085
Michael Wright17db18e2020-06-26 20:51:44 +01004086 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087
Chris Yea52ade12020-08-27 16:49:20 -07004088 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 InputMapperTest::SetUp();
4090
Michael Wright17db18e2020-06-26 20:51:44 +01004091 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004092 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093 }
4094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004095 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4096 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004097
4098 void prepareDisplay(int32_t orientation) {
4099 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004100 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004101 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4102 orientation, uniqueId, NO_PORT, viewportType);
4103 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004104
4105 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4106 float pressure) {
4107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4108 0.0f, 0.0f, 0.0f, EPSILON));
4109 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110};
4111
4112const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4115 int32_t originalY, int32_t rotatedX,
4116 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 NotifyMotionArgs args;
4118
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004124 ASSERT_NO_FATAL_FAILURE(
4125 assertCursorPointerCoords(args.pointerCoords[0],
4126 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4127 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004128}
4129
4130TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004134 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135}
4136
4137TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004141 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142}
4143
4144TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
4148 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004149 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150
4151 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004152 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4153 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4155 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4156
4157 // When the bounds are set, then there should be a valid motion range.
4158 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4159
4160 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004161 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
4163 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4164 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4165 1, 800 - 1, 0.0f, 0.0f));
4166 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4167 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4168 2, 480 - 1, 0.0f, 0.0f));
4169 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4170 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4171 0.0f, 1.0f, 0.0f, 0.0f));
4172}
4173
4174TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
4178 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180
4181 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4182 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4183 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4184 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4185 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4186 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4187 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4188 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4189 0.0f, 1.0f, 0.0f, 0.0f));
4190}
4191
4192TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004194 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195
arthurhungdcef2dc2020-08-11 14:47:50 +08004196 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
4198 NotifyMotionArgs args;
4199
4200 // Button press.
4201 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4205 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4206 ASSERT_EQ(DEVICE_ID, args.deviceId);
4207 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4208 ASSERT_EQ(uint32_t(0), args.policyFlags);
4209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4210 ASSERT_EQ(0, args.flags);
4211 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4212 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4213 ASSERT_EQ(0, args.edgeFlags);
4214 ASSERT_EQ(uint32_t(1), args.pointerCount);
4215 ASSERT_EQ(0, args.pointerProperties[0].id);
4216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004217 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4219 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4220 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4221
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4223 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4224 ASSERT_EQ(DEVICE_ID, args.deviceId);
4225 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4226 ASSERT_EQ(uint32_t(0), args.policyFlags);
4227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4228 ASSERT_EQ(0, args.flags);
4229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4230 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4231 ASSERT_EQ(0, args.edgeFlags);
4232 ASSERT_EQ(uint32_t(1), args.pointerCount);
4233 ASSERT_EQ(0, args.pointerProperties[0].id);
4234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004235 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4237 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4238 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4239
Michael Wrightd02c5b62014-02-10 15:10:22 -08004240 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004241 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4242 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4244 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4245 ASSERT_EQ(DEVICE_ID, args.deviceId);
4246 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4247 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4249 ASSERT_EQ(0, args.flags);
4250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4251 ASSERT_EQ(0, args.buttonState);
4252 ASSERT_EQ(0, args.edgeFlags);
4253 ASSERT_EQ(uint32_t(1), args.pointerCount);
4254 ASSERT_EQ(0, args.pointerProperties[0].id);
4255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004256 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4258 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4259 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4260
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4262 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4263 ASSERT_EQ(DEVICE_ID, args.deviceId);
4264 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4265 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4267 ASSERT_EQ(0, args.flags);
4268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4269 ASSERT_EQ(0, args.buttonState);
4270 ASSERT_EQ(0, args.edgeFlags);
4271 ASSERT_EQ(uint32_t(1), args.pointerCount);
4272 ASSERT_EQ(0, args.pointerProperties[0].id);
4273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004274 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4276 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4277 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4278}
4279
4280TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004282 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283
4284 NotifyMotionArgs args;
4285
4286 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4292 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4293 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4301 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302}
4303
4304TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004306 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
4308 NotifyMotionArgs args;
4309
4310 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4314 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004319 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004330 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331}
4332
4333TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004335 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336
4337 NotifyMotionArgs args;
4338
4339 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4347 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4348 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4353 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4354 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004362 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4363 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4364 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365
4366 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004371 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372
4373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004375 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376}
4377
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004378TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004380 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4381 // need to be rotated.
4382 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004383 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004385 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4387 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4394}
4395
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004396TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004398 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4399 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004400 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004401
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004402 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4405 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4406 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4407 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4408 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4411
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004412 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4416 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4417 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4418 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004422 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4431
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004432 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004433 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4434 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4435 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4436 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4437 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4438 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4439 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4440 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441}
4442
4443TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004445 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446
4447 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4448 mFakePointerController->setPosition(100, 200);
4449 mFakePointerController->setButtonState(0);
4450
4451 NotifyMotionArgs motionArgs;
4452 NotifyKeyArgs keyArgs;
4453
4454 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4459 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4460 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004461 ASSERT_NO_FATAL_FAILURE(
4462 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4466 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4467 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004468 ASSERT_NO_FATAL_FAILURE(
4469 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004470
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(0, motionArgs.buttonState);
4476 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004477 ASSERT_NO_FATAL_FAILURE(
4478 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479
4480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_EQ(0, motionArgs.buttonState);
4483 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004484 ASSERT_NO_FATAL_FAILURE(
4485 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004486
4487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489 ASSERT_EQ(0, motionArgs.buttonState);
4490 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004491 ASSERT_NO_FATAL_FAILURE(
4492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4500 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4501 motionArgs.buttonState);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4503 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004504 ASSERT_NO_FATAL_FAILURE(
4505 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4508 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4509 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4510 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4511 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004512 ASSERT_NO_FATAL_FAILURE(
4513 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514
4515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4517 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4518 motionArgs.buttonState);
4519 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4520 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004521 ASSERT_NO_FATAL_FAILURE(
4522 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004523
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4529 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004530 ASSERT_NO_FATAL_FAILURE(
4531 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004532
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004535 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4536 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004537 ASSERT_NO_FATAL_FAILURE(
4538 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4544 ASSERT_EQ(0, motionArgs.buttonState);
4545 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(
4547 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004550
4551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_EQ(0, motionArgs.buttonState);
4553 ASSERT_EQ(0, mFakePointerController->getButtonState());
4554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004555 ASSERT_NO_FATAL_FAILURE(
4556 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004557
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4559 ASSERT_EQ(0, motionArgs.buttonState);
4560 ASSERT_EQ(0, mFakePointerController->getButtonState());
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004562 ASSERT_NO_FATAL_FAILURE(
4563 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4570 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004571
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4575 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(
4577 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004578
4579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4581 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4582 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(
4584 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004589 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 ASSERT_EQ(0, motionArgs.buttonState);
4591 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004592 ASSERT_NO_FATAL_FAILURE(
4593 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004594
4595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597 ASSERT_EQ(0, motionArgs.buttonState);
4598 ASSERT_EQ(0, mFakePointerController->getButtonState());
4599
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004600 ASSERT_NO_FATAL_FAILURE(
4601 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4603 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4604 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4605
4606 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4610 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4611 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4616 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004617 ASSERT_NO_FATAL_FAILURE(
4618 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4622 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4623 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004624 ASSERT_NO_FATAL_FAILURE(
4625 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 ASSERT_EQ(0, motionArgs.buttonState);
4632 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004633 ASSERT_NO_FATAL_FAILURE(
4634 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4638 ASSERT_EQ(0, motionArgs.buttonState);
4639 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4644 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4645 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4646
4647 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4651 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4652 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004653
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4657 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004658 ASSERT_NO_FATAL_FAILURE(
4659 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004660
4661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4663 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4664 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004665 ASSERT_NO_FATAL_FAILURE(
4666 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004672 ASSERT_EQ(0, motionArgs.buttonState);
4673 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004674 ASSERT_NO_FATAL_FAILURE(
4675 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4679 ASSERT_EQ(0, motionArgs.buttonState);
4680 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004683
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4685 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4686 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4687
4688 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4692 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4693 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004694
Michael Wrightd02c5b62014-02-10 15:10:22 -08004695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004696 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004697 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4698 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004699 ASSERT_NO_FATAL_FAILURE(
4700 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4704 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4705 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004706 ASSERT_NO_FATAL_FAILURE(
4707 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004712 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(0, motionArgs.buttonState);
4714 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004715 ASSERT_NO_FATAL_FAILURE(
4716 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717
4718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4719 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4720 ASSERT_EQ(0, motionArgs.buttonState);
4721 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004722 ASSERT_NO_FATAL_FAILURE(
4723 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004724
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4726 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4727 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4728}
4729
4730TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004732 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733
4734 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4735 mFakePointerController->setPosition(100, 200);
4736 mFakePointerController->setButtonState(0);
4737
4738 NotifyMotionArgs args;
4739
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004744 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4747 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 +01004748 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004749}
4750
4751TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004752 addConfigurationProperty("cursor.mode", "pointer");
4753 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004754 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004755
4756 NotifyDeviceResetArgs resetArgs;
4757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4758 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4759 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4760
4761 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4762 mFakePointerController->setPosition(100, 200);
4763 mFakePointerController->setButtonState(0);
4764
4765 NotifyMotionArgs args;
4766
4767 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4772 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4775 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 +01004776 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004777
4778 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4782 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4783 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4785 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4787 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4790 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4791
4792 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004793 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4794 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4796 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4799 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4801 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4804 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4805
4806 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4811 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4814 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 +01004815 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004816
4817 // Disable pointer capture and check that the device generation got bumped
4818 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004819 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004820 mFakePolicy->setPointerCapture(false);
4821 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004822 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004823
4824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4825 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4826 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4827
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4832 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4835 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 +01004836 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837}
4838
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004839TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004840 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004841
Garfield Tan888a6a42020-01-09 11:39:16 -08004842 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004843 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004844 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4845 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004846 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4847 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004848 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4849 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4850
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004851 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4852 mFakePointerController->setPosition(100, 200);
4853 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004854
4855 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4860 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4863 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 +01004864 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004865 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4866}
4867
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868// --- TouchInputMapperTest ---
4869
4870class TouchInputMapperTest : public InputMapperTest {
4871protected:
4872 static const int32_t RAW_X_MIN;
4873 static const int32_t RAW_X_MAX;
4874 static const int32_t RAW_Y_MIN;
4875 static const int32_t RAW_Y_MAX;
4876 static const int32_t RAW_TOUCH_MIN;
4877 static const int32_t RAW_TOUCH_MAX;
4878 static const int32_t RAW_TOOL_MIN;
4879 static const int32_t RAW_TOOL_MAX;
4880 static const int32_t RAW_PRESSURE_MIN;
4881 static const int32_t RAW_PRESSURE_MAX;
4882 static const int32_t RAW_ORIENTATION_MIN;
4883 static const int32_t RAW_ORIENTATION_MAX;
4884 static const int32_t RAW_DISTANCE_MIN;
4885 static const int32_t RAW_DISTANCE_MAX;
4886 static const int32_t RAW_TILT_MIN;
4887 static const int32_t RAW_TILT_MAX;
4888 static const int32_t RAW_ID_MIN;
4889 static const int32_t RAW_ID_MAX;
4890 static const int32_t RAW_SLOT_MIN;
4891 static const int32_t RAW_SLOT_MAX;
4892 static const float X_PRECISION;
4893 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004894 static const float X_PRECISION_VIRTUAL;
4895 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896
4897 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004898 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899
4900 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4901
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004902 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004903 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 enum Axes {
4906 POSITION = 1 << 0,
4907 TOUCH = 1 << 1,
4908 TOOL = 1 << 2,
4909 PRESSURE = 1 << 3,
4910 ORIENTATION = 1 << 4,
4911 MINOR = 1 << 5,
4912 ID = 1 << 6,
4913 DISTANCE = 1 << 7,
4914 TILT = 1 << 8,
4915 SLOT = 1 << 9,
4916 TOOL_TYPE = 1 << 10,
4917 };
4918
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004919 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4920 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004923 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 int32_t toRawX(float displayX);
4925 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004926 int32_t toRotatedRawX(float displayX);
4927 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004928 float toCookedX(float rawX, float rawY);
4929 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004931 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004933 float toDisplayY(int32_t rawY, int32_t displayHeight);
4934
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935};
4936
4937const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4938const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4939const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4940const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4941const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4942const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4943const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4944const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004945const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4946const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4948const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4949const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4950const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4951const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4952const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4953const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4954const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4955const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4956const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4957const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4958const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004959const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4960 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4961const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4962 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004963const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4964 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004965
4966const float TouchInputMapperTest::GEOMETRIC_SCALE =
4967 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4968 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4969
4970const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4971 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4972 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4973};
4974
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004975void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004976 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4977 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004978}
4979
4980void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4981 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4982 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983}
4984
Santos Cordonfa5cf462017-04-05 10:37:00 -07004985void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004986 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4987 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4988 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004989}
4990
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004992 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4993 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996}
4997
Jason Gerecke489fda82012-09-07 17:19:40 -07004998void TouchInputMapperTest::prepareLocationCalibration() {
4999 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5000}
5001
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002int32_t TouchInputMapperTest::toRawX(float displayX) {
5003 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5004}
5005
5006int32_t TouchInputMapperTest::toRawY(float displayY) {
5007 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5008}
5009
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005010int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5011 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5012}
5013
5014int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5015 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5016}
5017
Jason Gerecke489fda82012-09-07 17:19:40 -07005018float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5019 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5020 return rawX;
5021}
5022
5023float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5024 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5025 return rawY;
5026}
5027
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005029 return toDisplayX(rawX, DISPLAY_WIDTH);
5030}
5031
5032float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5033 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034}
5035
5036float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005037 return toDisplayY(rawY, DISPLAY_HEIGHT);
5038}
5039
5040float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5041 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042}
5043
5044
5045// --- SingleTouchInputMapperTest ---
5046
5047class SingleTouchInputMapperTest : public TouchInputMapperTest {
5048protected:
5049 void prepareButtons();
5050 void prepareAxes(int axes);
5051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005052 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5053 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5054 void processUp(SingleTouchInputMapper& mappery);
5055 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5056 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5057 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5058 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5059 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5060 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061};
5062
5063void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005064 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065}
5066
5067void SingleTouchInputMapperTest::prepareAxes(int axes) {
5068 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 }
5072 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5074 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 }
5076 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5078 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079 }
5080 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5082 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 }
5084 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 }
5088}
5089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005090void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094}
5095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005096void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099}
5100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005101void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103}
5104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005105void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107}
5108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005109void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5110 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112}
5113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116}
5117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5119 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122}
5123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005124void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5125 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127}
5128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131}
5132
Michael Wrightd02c5b62014-02-10 15:10:22 -08005133TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 prepareButtons();
5135 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005136 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005138 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139}
5140
5141TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005142 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5143 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 prepareButtons();
5145 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005146 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005148 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149}
5150
5151TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152 prepareButtons();
5153 prepareAxes(POSITION);
5154 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005155 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005157 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158}
5159
5160TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161 prepareButtons();
5162 prepareAxes(POSITION);
5163 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005164 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
5169TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 addConfigurationProperty("touch.deviceType", "touchScreen");
5171 prepareDisplay(DISPLAY_ORIENTATION_0);
5172 prepareButtons();
5173 prepareAxes(POSITION);
5174 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005175 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176
5177 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179
5180 // Virtual key is down.
5181 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5182 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5183 processDown(mapper, x, y);
5184 processSync(mapper);
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 // Virtual key is up.
5190 processUp(mapper);
5191 processSync(mapper);
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005194 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195}
5196
5197TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 addConfigurationProperty("touch.deviceType", "touchScreen");
5199 prepareDisplay(DISPLAY_ORIENTATION_0);
5200 prepareButtons();
5201 prepareAxes(POSITION);
5202 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204
5205 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005206 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207
5208 // Virtual key is down.
5209 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5210 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5211 processDown(mapper, x, y);
5212 processSync(mapper);
5213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005215 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216
5217 // Virtual key is up.
5218 processUp(mapper);
5219 processSync(mapper);
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005222 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223}
5224
5225TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 addConfigurationProperty("touch.deviceType", "touchScreen");
5227 prepareDisplay(DISPLAY_ORIENTATION_0);
5228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
5233 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5234 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236 ASSERT_TRUE(flags[0]);
5237 ASSERT_FALSE(flags[1]);
5238}
5239
5240TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005241 addConfigurationProperty("touch.deviceType", "touchScreen");
5242 prepareDisplay(DISPLAY_ORIENTATION_0);
5243 prepareButtons();
5244 prepareAxes(POSITION);
5245 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005246 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247
arthurhungdcef2dc2020-08-11 14:47:50 +08005248 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249
5250 NotifyKeyArgs args;
5251
5252 // Press virtual key.
5253 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5254 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5255 processDown(mapper, x, y);
5256 processSync(mapper);
5257
5258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5259 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5260 ASSERT_EQ(DEVICE_ID, args.deviceId);
5261 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5262 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5264 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5265 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5266 ASSERT_EQ(KEY_HOME, args.scanCode);
5267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5268 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5269
5270 // Release virtual key.
5271 processUp(mapper);
5272 processSync(mapper);
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5275 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5276 ASSERT_EQ(DEVICE_ID, args.deviceId);
5277 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5278 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5280 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5281 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5282 ASSERT_EQ(KEY_HOME, args.scanCode);
5283 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5284 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5285
5286 // Should not have sent any motions.
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5288}
5289
5290TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 addConfigurationProperty("touch.deviceType", "touchScreen");
5292 prepareDisplay(DISPLAY_ORIENTATION_0);
5293 prepareButtons();
5294 prepareAxes(POSITION);
5295 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005296 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297
arthurhungdcef2dc2020-08-11 14:47:50 +08005298 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299
5300 NotifyKeyArgs keyArgs;
5301
5302 // Press virtual key.
5303 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5304 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5305 processDown(mapper, x, y);
5306 processSync(mapper);
5307
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5309 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5310 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5311 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5312 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5313 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5314 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5315 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5316 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5317 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5318 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5319
5320 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5321 // into the display area.
5322 y -= 100;
5323 processMove(mapper, x, y);
5324 processSync(mapper);
5325
5326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5327 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5328 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5329 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5330 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5331 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5332 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5333 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5334 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5335 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5337 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5338
5339 NotifyMotionArgs motionArgs;
5340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5342 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5343 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5344 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5346 ASSERT_EQ(0, motionArgs.flags);
5347 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5348 ASSERT_EQ(0, motionArgs.buttonState);
5349 ASSERT_EQ(0, motionArgs.edgeFlags);
5350 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5351 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5354 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5355 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5356 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5358
5359 // Keep moving out of bounds. Should generate a pointer move.
5360 y -= 50;
5361 processMove(mapper, x, y);
5362 processSync(mapper);
5363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5366 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5370 ASSERT_EQ(0, motionArgs.flags);
5371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5372 ASSERT_EQ(0, motionArgs.buttonState);
5373 ASSERT_EQ(0, motionArgs.edgeFlags);
5374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5378 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5379 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5380 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5382
5383 // Release out of bounds. Should generate a pointer up.
5384 processUp(mapper);
5385 processSync(mapper);
5386
5387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5393 ASSERT_EQ(0, motionArgs.flags);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5395 ASSERT_EQ(0, motionArgs.buttonState);
5396 ASSERT_EQ(0, motionArgs.edgeFlags);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5405
5406 // Should not have sent any more keys or motions.
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5409}
5410
5411TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412 addConfigurationProperty("touch.deviceType", "touchScreen");
5413 prepareDisplay(DISPLAY_ORIENTATION_0);
5414 prepareButtons();
5415 prepareAxes(POSITION);
5416 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005417 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005418
arthurhungdcef2dc2020-08-11 14:47:50 +08005419 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420
5421 NotifyMotionArgs motionArgs;
5422
5423 // Initially go down out of bounds.
5424 int32_t x = -10;
5425 int32_t y = -10;
5426 processDown(mapper, x, y);
5427 processSync(mapper);
5428
5429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5430
5431 // Move into the display area. Should generate a pointer down.
5432 x = 50;
5433 y = 75;
5434 processMove(mapper, x, y);
5435 processSync(mapper);
5436
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5439 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5440 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5441 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5442 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5443 ASSERT_EQ(0, motionArgs.flags);
5444 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5445 ASSERT_EQ(0, motionArgs.buttonState);
5446 ASSERT_EQ(0, motionArgs.edgeFlags);
5447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5451 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5452 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5453 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5455
5456 // Release. Should generate a pointer up.
5457 processUp(mapper);
5458 processSync(mapper);
5459
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5462 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5463 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5464 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5466 ASSERT_EQ(0, motionArgs.flags);
5467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5468 ASSERT_EQ(0, motionArgs.buttonState);
5469 ASSERT_EQ(0, motionArgs.edgeFlags);
5470 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5471 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5472 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5474 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5475 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5476 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5478
5479 // Should not have sent any more keys or motions.
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5482}
5483
Santos Cordonfa5cf462017-04-05 10:37:00 -07005484TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005485 addConfigurationProperty("touch.deviceType", "touchScreen");
5486 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5487
5488 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5489 prepareButtons();
5490 prepareAxes(POSITION);
5491 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005493
arthurhungdcef2dc2020-08-11 14:47:50 +08005494 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005495
5496 NotifyMotionArgs motionArgs;
5497
5498 // Down.
5499 int32_t x = 100;
5500 int32_t y = 125;
5501 processDown(mapper, x, y);
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5506 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5507 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5509 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5510 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5511 ASSERT_EQ(0, motionArgs.flags);
5512 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5513 ASSERT_EQ(0, motionArgs.buttonState);
5514 ASSERT_EQ(0, motionArgs.edgeFlags);
5515 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5516 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5519 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5520 1, 0, 0, 0, 0, 0, 0, 0));
5521 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5522 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5523 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5524
5525 // Move.
5526 x += 50;
5527 y += 75;
5528 processMove(mapper, x, y);
5529 processSync(mapper);
5530
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5534 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5538 ASSERT_EQ(0, motionArgs.flags);
5539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5540 ASSERT_EQ(0, motionArgs.buttonState);
5541 ASSERT_EQ(0, motionArgs.edgeFlags);
5542 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5546 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5547 1, 0, 0, 0, 0, 0, 0, 0));
5548 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5549 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5551
5552 // Up.
5553 processUp(mapper);
5554 processSync(mapper);
5555
5556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5557 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5558 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5559 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5560 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5561 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5562 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5563 ASSERT_EQ(0, motionArgs.flags);
5564 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5565 ASSERT_EQ(0, motionArgs.buttonState);
5566 ASSERT_EQ(0, motionArgs.edgeFlags);
5567 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5568 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5571 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5572 1, 0, 0, 0, 0, 0, 0, 0));
5573 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5574 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5576
5577 // Should not have sent any more keys or motions.
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5580}
5581
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 addConfigurationProperty("touch.deviceType", "touchScreen");
5584 prepareDisplay(DISPLAY_ORIENTATION_0);
5585 prepareButtons();
5586 prepareAxes(POSITION);
5587 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005588 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589
arthurhungdcef2dc2020-08-11 14:47:50 +08005590 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591
5592 NotifyMotionArgs motionArgs;
5593
5594 // Down.
5595 int32_t x = 100;
5596 int32_t y = 125;
5597 processDown(mapper, x, y);
5598 processSync(mapper);
5599
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5602 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5603 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5604 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5606 ASSERT_EQ(0, motionArgs.flags);
5607 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5608 ASSERT_EQ(0, motionArgs.buttonState);
5609 ASSERT_EQ(0, motionArgs.edgeFlags);
5610 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5611 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5614 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5615 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5616 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5617 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5618
5619 // Move.
5620 x += 50;
5621 y += 75;
5622 processMove(mapper, x, y);
5623 processSync(mapper);
5624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5626 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5627 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5628 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5629 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5631 ASSERT_EQ(0, motionArgs.flags);
5632 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5633 ASSERT_EQ(0, motionArgs.buttonState);
5634 ASSERT_EQ(0, motionArgs.edgeFlags);
5635 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5636 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5639 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5640 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5641 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5643
5644 // Up.
5645 processUp(mapper);
5646 processSync(mapper);
5647
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5649 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5650 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5651 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5652 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5653 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5654 ASSERT_EQ(0, motionArgs.flags);
5655 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5656 ASSERT_EQ(0, motionArgs.buttonState);
5657 ASSERT_EQ(0, motionArgs.edgeFlags);
5658 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5659 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5662 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5663 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5664 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5666
5667 // Should not have sent any more keys or motions.
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5670}
5671
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005672TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005673 addConfigurationProperty("touch.deviceType", "touchScreen");
5674 prepareButtons();
5675 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005676 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5677 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005678 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679
5680 NotifyMotionArgs args;
5681
5682 // Rotation 90.
5683 prepareDisplay(DISPLAY_ORIENTATION_90);
5684 processDown(mapper, toRawX(50), toRawY(75));
5685 processSync(mapper);
5686
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5688 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5689 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5690
5691 processUp(mapper);
5692 processSync(mapper);
5693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5694}
5695
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005696TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 addConfigurationProperty("touch.deviceType", "touchScreen");
5698 prepareButtons();
5699 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005700 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5701 // orientation-aware are affected by display rotation.
5702 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005703 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 NotifyMotionArgs args;
5706
5707 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005708 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 prepareDisplay(DISPLAY_ORIENTATION_0);
5710 processDown(mapper, toRawX(50), toRawY(75));
5711 processSync(mapper);
5712
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5714 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5715 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5716
5717 processUp(mapper);
5718 processSync(mapper);
5719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5720
5721 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005722 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005724 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 processSync(mapper);
5726
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5728 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5729 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5730
5731 processUp(mapper);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5734
5735 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005736 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737 prepareDisplay(DISPLAY_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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5742 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5743 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5744
5745 processUp(mapper);
5746 processSync(mapper);
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5748
5749 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005750 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005751 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005752 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753 processSync(mapper);
5754
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5756 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5757 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5758
5759 processUp(mapper);
5760 processSync(mapper);
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5762}
5763
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005764TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5765 addConfigurationProperty("touch.deviceType", "touchScreen");
5766 prepareButtons();
5767 prepareAxes(POSITION);
5768 addConfigurationProperty("touch.orientationAware", "1");
5769 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5770 clearViewports();
5771 prepareDisplay(DISPLAY_ORIENTATION_0);
5772 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5773 NotifyMotionArgs args;
5774
5775 // Orientation 0.
5776 processDown(mapper, toRawX(50), toRawY(75));
5777 processSync(mapper);
5778
5779 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5780 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5781 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5782
5783 processUp(mapper);
5784 processSync(mapper);
5785 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5786}
5787
5788TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5789 addConfigurationProperty("touch.deviceType", "touchScreen");
5790 prepareButtons();
5791 prepareAxes(POSITION);
5792 addConfigurationProperty("touch.orientationAware", "1");
5793 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5794 clearViewports();
5795 prepareDisplay(DISPLAY_ORIENTATION_0);
5796 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5797 NotifyMotionArgs args;
5798
5799 // Orientation 90.
5800 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5801 processSync(mapper);
5802
5803 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5804 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5805 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5806
5807 processUp(mapper);
5808 processSync(mapper);
5809 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5810}
5811
5812TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5813 addConfigurationProperty("touch.deviceType", "touchScreen");
5814 prepareButtons();
5815 prepareAxes(POSITION);
5816 addConfigurationProperty("touch.orientationAware", "1");
5817 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5818 clearViewports();
5819 prepareDisplay(DISPLAY_ORIENTATION_0);
5820 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5821 NotifyMotionArgs args;
5822
5823 // Orientation 180.
5824 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5825 processSync(mapper);
5826
5827 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5828 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5829 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5830
5831 processUp(mapper);
5832 processSync(mapper);
5833 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5834}
5835
5836TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5837 addConfigurationProperty("touch.deviceType", "touchScreen");
5838 prepareButtons();
5839 prepareAxes(POSITION);
5840 addConfigurationProperty("touch.orientationAware", "1");
5841 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5842 clearViewports();
5843 prepareDisplay(DISPLAY_ORIENTATION_0);
5844 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5845 NotifyMotionArgs args;
5846
5847 // Orientation 270.
5848 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5849 processSync(mapper);
5850
5851 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5852 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5853 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5854
5855 processUp(mapper);
5856 processSync(mapper);
5857 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5858}
5859
5860TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5861 addConfigurationProperty("touch.deviceType", "touchScreen");
5862 prepareButtons();
5863 prepareAxes(POSITION);
5864 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5865 // orientation-aware are affected by display rotation.
5866 addConfigurationProperty("touch.orientationAware", "0");
5867 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5868 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5869
5870 NotifyMotionArgs args;
5871
5872 // Orientation 90, Rotation 0.
5873 clearViewports();
5874 prepareDisplay(DISPLAY_ORIENTATION_0);
5875 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5876 processSync(mapper);
5877
5878 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5879 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5880 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5881
5882 processUp(mapper);
5883 processSync(mapper);
5884 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5885
5886 // Orientation 90, Rotation 90.
5887 clearViewports();
5888 prepareDisplay(DISPLAY_ORIENTATION_90);
5889 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5890 processSync(mapper);
5891
5892 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5893 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5894 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5895
5896 processUp(mapper);
5897 processSync(mapper);
5898 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5899
5900 // Orientation 90, Rotation 180.
5901 clearViewports();
5902 prepareDisplay(DISPLAY_ORIENTATION_180);
5903 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5904 processSync(mapper);
5905
5906 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5907 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5908 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5909
5910 processUp(mapper);
5911 processSync(mapper);
5912 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5913
5914 // Orientation 90, Rotation 270.
5915 clearViewports();
5916 prepareDisplay(DISPLAY_ORIENTATION_270);
5917 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5918 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5919 processSync(mapper);
5920
5921 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5922 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5923 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5924
5925 processUp(mapper);
5926 processSync(mapper);
5927 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5928}
5929
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931 addConfigurationProperty("touch.deviceType", "touchScreen");
5932 prepareDisplay(DISPLAY_ORIENTATION_0);
5933 prepareButtons();
5934 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005935 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936
5937 // These calculations are based on the input device calibration documentation.
5938 int32_t rawX = 100;
5939 int32_t rawY = 200;
5940 int32_t rawPressure = 10;
5941 int32_t rawToolMajor = 12;
5942 int32_t rawDistance = 2;
5943 int32_t rawTiltX = 30;
5944 int32_t rawTiltY = 110;
5945
5946 float x = toDisplayX(rawX);
5947 float y = toDisplayY(rawY);
5948 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5949 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5950 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5951 float distance = float(rawDistance);
5952
5953 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5954 float tiltScale = M_PI / 180;
5955 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5956 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5957 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5958 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5959
5960 processDown(mapper, rawX, rawY);
5961 processPressure(mapper, rawPressure);
5962 processToolMajor(mapper, rawToolMajor);
5963 processDistance(mapper, rawDistance);
5964 processTilt(mapper, rawTiltX, rawTiltY);
5965 processSync(mapper);
5966
5967 NotifyMotionArgs args;
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5970 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5971 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5972}
5973
Jason Gerecke489fda82012-09-07 17:19:40 -07005974TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005975 addConfigurationProperty("touch.deviceType", "touchScreen");
5976 prepareDisplay(DISPLAY_ORIENTATION_0);
5977 prepareLocationCalibration();
5978 prepareButtons();
5979 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005980 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005981
5982 int32_t rawX = 100;
5983 int32_t rawY = 200;
5984
5985 float x = toDisplayX(toCookedX(rawX, rawY));
5986 float y = toDisplayY(toCookedY(rawX, rawY));
5987
5988 processDown(mapper, rawX, rawY);
5989 processSync(mapper);
5990
5991 NotifyMotionArgs args;
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5994 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5995}
5996
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 addConfigurationProperty("touch.deviceType", "touchScreen");
5999 prepareDisplay(DISPLAY_ORIENTATION_0);
6000 prepareButtons();
6001 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006002 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003
6004 NotifyMotionArgs motionArgs;
6005 NotifyKeyArgs keyArgs;
6006
6007 processDown(mapper, 100, 200);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6011 ASSERT_EQ(0, motionArgs.buttonState);
6012
6013 // press BTN_LEFT, release BTN_LEFT
6014 processKey(mapper, BTN_LEFT, 1);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6018 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6019
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6022 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6023
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 processKey(mapper, BTN_LEFT, 0);
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033
6034 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6035 processKey(mapper, BTN_RIGHT, 1);
6036 processKey(mapper, BTN_MIDDLE, 1);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6040 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6041 motionArgs.buttonState);
6042
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6046
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6049 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6050 motionArgs.buttonState);
6051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 processKey(mapper, BTN_RIGHT, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061
6062 processKey(mapper, BTN_MIDDLE, 0);
6063 processSync(mapper);
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006066 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006070 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 // press BTN_BACK, release BTN_BACK
6073 processKey(mapper, BTN_BACK, 1);
6074 processSync(mapper);
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6077 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006078
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086
6087 processKey(mapper, BTN_BACK, 0);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(0, motionArgs.buttonState);
6096
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6098 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6099 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6100
6101 // press BTN_SIDE, release BTN_SIDE
6102 processKey(mapper, BTN_SIDE, 1);
6103 processSync(mapper);
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6105 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6106 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006107
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006110 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6111
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6114 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115
6116 processKey(mapper, BTN_SIDE, 0);
6117 processSync(mapper);
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_EQ(0, motionArgs.buttonState);
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6127 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6128 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6129
6130 // press BTN_FORWARD, release BTN_FORWARD
6131 processKey(mapper, BTN_FORWARD, 1);
6132 processSync(mapper);
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6134 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6135 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006136
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006139 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144
6145 processKey(mapper, BTN_FORWARD, 0);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(0, motionArgs.buttonState);
6154
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6156 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6157 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6158
6159 // press BTN_EXTRA, release BTN_EXTRA
6160 processKey(mapper, BTN_EXTRA, 1);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6164 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6169
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6171 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6172 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173
6174 processKey(mapper, BTN_EXTRA, 0);
6175 processSync(mapper);
6176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006177 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006179
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006182 ASSERT_EQ(0, motionArgs.buttonState);
6183
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6185 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6186 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6187
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6189
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 // press BTN_STYLUS, release BTN_STYLUS
6191 processKey(mapper, BTN_STYLUS, 1);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6196
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6199 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006200
6201 processKey(mapper, BTN_STYLUS, 0);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006205 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006206
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006209 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 // press BTN_STYLUS2, release BTN_STYLUS2
6212 processKey(mapper, BTN_STYLUS2, 1);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006216 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6220 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221
6222 processKey(mapper, BTN_STYLUS2, 0);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006227
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006230 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231
6232 // release touch
6233 processUp(mapper);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6237 ASSERT_EQ(0, motionArgs.buttonState);
6238}
6239
6240TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 addConfigurationProperty("touch.deviceType", "touchScreen");
6242 prepareDisplay(DISPLAY_ORIENTATION_0);
6243 prepareButtons();
6244 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006245 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246
6247 NotifyMotionArgs motionArgs;
6248
6249 // default tool type is finger
6250 processDown(mapper, 100, 200);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6255
6256 // eraser
6257 processKey(mapper, BTN_TOOL_RUBBER, 1);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6262
6263 // stylus
6264 processKey(mapper, BTN_TOOL_RUBBER, 0);
6265 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6270
6271 // brush
6272 processKey(mapper, BTN_TOOL_PEN, 0);
6273 processKey(mapper, BTN_TOOL_BRUSH, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6278
6279 // pencil
6280 processKey(mapper, BTN_TOOL_BRUSH, 0);
6281 processKey(mapper, BTN_TOOL_PENCIL, 1);
6282 processSync(mapper);
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6286
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006287 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 processKey(mapper, BTN_TOOL_PENCIL, 0);
6289 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6293 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6294
6295 // mouse
6296 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6297 processKey(mapper, BTN_TOOL_MOUSE, 1);
6298 processSync(mapper);
6299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6302
6303 // lens
6304 processKey(mapper, BTN_TOOL_MOUSE, 0);
6305 processKey(mapper, BTN_TOOL_LENS, 1);
6306 processSync(mapper);
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6310
6311 // double-tap
6312 processKey(mapper, BTN_TOOL_LENS, 0);
6313 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6314 processSync(mapper);
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6318
6319 // triple-tap
6320 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6321 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6322 processSync(mapper);
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6326
6327 // quad-tap
6328 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6329 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6330 processSync(mapper);
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6334
6335 // finger
6336 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6337 processKey(mapper, BTN_TOOL_FINGER, 1);
6338 processSync(mapper);
6339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6342
6343 // stylus trumps finger
6344 processKey(mapper, BTN_TOOL_PEN, 1);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6349
6350 // eraser trumps stylus
6351 processKey(mapper, BTN_TOOL_RUBBER, 1);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6355 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6356
6357 // mouse trumps eraser
6358 processKey(mapper, BTN_TOOL_MOUSE, 1);
6359 processSync(mapper);
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6363
6364 // back to default tool type
6365 processKey(mapper, BTN_TOOL_MOUSE, 0);
6366 processKey(mapper, BTN_TOOL_RUBBER, 0);
6367 processKey(mapper, BTN_TOOL_PEN, 0);
6368 processKey(mapper, BTN_TOOL_FINGER, 0);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6373}
6374
6375TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 addConfigurationProperty("touch.deviceType", "touchScreen");
6377 prepareDisplay(DISPLAY_ORIENTATION_0);
6378 prepareButtons();
6379 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006380 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006381 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382
6383 NotifyMotionArgs motionArgs;
6384
6385 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6386 processKey(mapper, BTN_TOOL_FINGER, 1);
6387 processMove(mapper, 100, 200);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6392 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6393
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6397 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6398
6399 // move a little
6400 processMove(mapper, 150, 250);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6405 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6406
6407 // down when BTN_TOUCH is pressed, pressure defaults to 1
6408 processKey(mapper, BTN_TOUCH, 1);
6409 processSync(mapper);
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6413 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6418 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6419
6420 // up when BTN_TOUCH is released, hover restored
6421 processKey(mapper, BTN_TOUCH, 0);
6422 processSync(mapper);
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6426 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6427
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6431 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6432
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6436 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6437
6438 // exit hover when pointer goes away
6439 processKey(mapper, BTN_TOOL_FINGER, 0);
6440 processSync(mapper);
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6444 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6445}
6446
6447TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006448 addConfigurationProperty("touch.deviceType", "touchScreen");
6449 prepareDisplay(DISPLAY_ORIENTATION_0);
6450 prepareButtons();
6451 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006452 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453
6454 NotifyMotionArgs motionArgs;
6455
6456 // initially hovering because pressure is 0
6457 processDown(mapper, 100, 200);
6458 processPressure(mapper, 0);
6459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6464
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6468 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // move a little
6471 processMove(mapper, 150, 250);
6472 processSync(mapper);
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6476 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6477
6478 // down when pressure is non-zero
6479 processPressure(mapper, RAW_PRESSURE_MAX);
6480 processSync(mapper);
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6484 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6489 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6490
6491 // up when pressure becomes 0, hover restored
6492 processPressure(mapper, 0);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6497 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6498
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6500 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6502 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6503
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6507 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6508
6509 // exit hover when pointer goes away
6510 processUp(mapper);
6511 processSync(mapper);
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6515 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6516}
6517
Prabir Pradhan5632d622021-09-06 07:57:20 -07006518// --- TouchDisplayProjectionTest ---
6519
6520class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6521public:
6522 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6523 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6524 // rotated equivalent of the given un-rotated physical display bounds.
6525 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6526 uint32_t inverseRotationFlags;
6527 auto width = DISPLAY_WIDTH;
6528 auto height = DISPLAY_HEIGHT;
6529 switch (orientation) {
6530 case DISPLAY_ORIENTATION_90:
6531 inverseRotationFlags = ui::Transform::ROT_270;
6532 std::swap(width, height);
6533 break;
6534 case DISPLAY_ORIENTATION_180:
6535 inverseRotationFlags = ui::Transform::ROT_180;
6536 break;
6537 case DISPLAY_ORIENTATION_270:
6538 inverseRotationFlags = ui::Transform::ROT_90;
6539 std::swap(width, height);
6540 break;
6541 case DISPLAY_ORIENTATION_0:
6542 inverseRotationFlags = ui::Transform::ROT_0;
6543 break;
6544 default:
6545 FAIL() << "Invalid orientation: " << orientation;
6546 }
6547
6548 const ui::Transform rotation(inverseRotationFlags, width, height);
6549 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6550
6551 std::optional<DisplayViewport> internalViewport =
6552 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6553 DisplayViewport& v = *internalViewport;
6554 v.displayId = DISPLAY_ID;
6555 v.orientation = orientation;
6556
6557 v.logicalLeft = 0;
6558 v.logicalTop = 0;
6559 v.logicalRight = 100;
6560 v.logicalBottom = 100;
6561
6562 v.physicalLeft = rotatedPhysicalDisplay.left;
6563 v.physicalTop = rotatedPhysicalDisplay.top;
6564 v.physicalRight = rotatedPhysicalDisplay.right;
6565 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6566
6567 v.deviceWidth = width;
6568 v.deviceHeight = height;
6569
6570 v.isActive = true;
6571 v.uniqueId = UNIQUE_ID;
6572 v.type = ViewportType::INTERNAL;
6573 mFakePolicy->updateViewport(v);
6574 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6575 }
6576
6577 void assertReceivedMove(const Point& point) {
6578 NotifyMotionArgs motionArgs;
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6583 1, 0, 0, 0, 0, 0, 0, 0));
6584 }
6585};
6586
6587TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6588 addConfigurationProperty("touch.deviceType", "touchScreen");
6589 prepareDisplay(DISPLAY_ORIENTATION_0);
6590
6591 prepareButtons();
6592 prepareAxes(POSITION);
6593 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6594
6595 NotifyMotionArgs motionArgs;
6596
6597 // Configure the DisplayViewport such that the logical display maps to a subsection of
6598 // the display panel called the physical display. Here, the physical display is bounded by the
6599 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6600 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6601 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6602 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6603
6604 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6605 DISPLAY_ORIENTATION_270}) {
6606 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6607
6608 // Touches outside the physical display should be ignored, and should not generate any
6609 // events. Ensure touches at the following points that lie outside of the physical display
6610 // area do not generate any events.
6611 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6612 processDown(mapper, toRawX(point.x), toRawY(point.y));
6613 processSync(mapper);
6614 processUp(mapper);
6615 processSync(mapper);
6616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6617 << "Unexpected event generated for touch outside physical display at point: "
6618 << point.x << ", " << point.y;
6619 }
6620 }
6621}
6622
6623TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6624 addConfigurationProperty("touch.deviceType", "touchScreen");
6625 prepareDisplay(DISPLAY_ORIENTATION_0);
6626
6627 prepareButtons();
6628 prepareAxes(POSITION);
6629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6630
6631 NotifyMotionArgs motionArgs;
6632
6633 // Configure the DisplayViewport such that the logical display maps to a subsection of
6634 // the display panel called the physical display. Here, the physical display is bounded by the
6635 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6636 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6637
6638 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6639 DISPLAY_ORIENTATION_270}) {
6640 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6641
6642 // Touches that start outside the physical display should be ignored until it enters the
6643 // physical display bounds, at which point it should generate a down event. Start a touch at
6644 // the point (5, 100), which is outside the physical display bounds.
6645 static const Point kOutsidePoint{5, 100};
6646 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6649
6650 // Move the touch into the physical display area. This should generate a pointer down.
6651 processMove(mapper, toRawX(11), toRawY(21));
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6655 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6656 ASSERT_NO_FATAL_FAILURE(
6657 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6658
6659 // Move the touch inside the physical display area. This should generate a pointer move.
6660 processMove(mapper, toRawX(69), toRawY(159));
6661 processSync(mapper);
6662 assertReceivedMove({69, 159});
6663
6664 // Move outside the physical display area. Since the pointer is already down, this should
6665 // now continue generating events.
6666 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6667 processSync(mapper);
6668 assertReceivedMove(kOutsidePoint);
6669
6670 // Release. This should generate a pointer up.
6671 processUp(mapper);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6676 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6677
6678 // Ensure no more events were generated.
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6681 }
6682}
6683
Michael Wrightd02c5b62014-02-10 15:10:22 -08006684// --- MultiTouchInputMapperTest ---
6685
6686class MultiTouchInputMapperTest : public TouchInputMapperTest {
6687protected:
6688 void prepareAxes(int axes);
6689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006690 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6691 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6692 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6693 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6694 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6695 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6696 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6697 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6698 void processId(MultiTouchInputMapper& mapper, int32_t id);
6699 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6700 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6701 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6702 void processMTSync(MultiTouchInputMapper& mapper);
6703 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006704};
6705
6706void MultiTouchInputMapperTest::prepareAxes(int axes) {
6707 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6709 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710 }
6711 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006712 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6713 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006714 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6716 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717 }
6718 }
6719 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006720 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6721 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006722 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006723 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006724 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725 }
6726 }
6727 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006728 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6729 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730 }
6731 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006732 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6733 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734 }
6735 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006736 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6737 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006738 }
6739 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6741 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006742 }
6743 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006744 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6745 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746 }
6747 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006748 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006749 }
6750}
6751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006752void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6753 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006756}
6757
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6759 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006761}
6762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006763void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6764 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006766}
6767
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006768void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006770}
6771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006772void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006774}
6775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006776void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6777 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006779}
6780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006781void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006783}
6784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006785void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787}
6788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006789void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006791}
6792
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006793void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795}
6796
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006797void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799}
6800
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006801void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6802 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006804}
6805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006806void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808}
6809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006810void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006812}
6813
Michael Wrightd02c5b62014-02-10 15:10:22 -08006814TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 addConfigurationProperty("touch.deviceType", "touchScreen");
6816 prepareDisplay(DISPLAY_ORIENTATION_0);
6817 prepareAxes(POSITION);
6818 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006819 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006820
arthurhungdcef2dc2020-08-11 14:47:50 +08006821 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822
6823 NotifyMotionArgs motionArgs;
6824
6825 // Two fingers down at once.
6826 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6827 processPosition(mapper, x1, y1);
6828 processMTSync(mapper);
6829 processPosition(mapper, x2, y2);
6830 processMTSync(mapper);
6831 processSync(mapper);
6832
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6835 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6836 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6837 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6839 ASSERT_EQ(0, motionArgs.flags);
6840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6841 ASSERT_EQ(0, motionArgs.buttonState);
6842 ASSERT_EQ(0, motionArgs.edgeFlags);
6843 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6844 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6847 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6848 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6849 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6850 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6851
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6853 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6854 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6855 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6856 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006857 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006858 ASSERT_EQ(0, motionArgs.flags);
6859 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6860 ASSERT_EQ(0, motionArgs.buttonState);
6861 ASSERT_EQ(0, motionArgs.edgeFlags);
6862 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6863 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6865 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6868 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6870 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6874
6875 // Move.
6876 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6877 processPosition(mapper, x1, y1);
6878 processMTSync(mapper);
6879 processPosition(mapper, x2, y2);
6880 processMTSync(mapper);
6881 processSync(mapper);
6882
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6885 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6886 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6887 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6889 ASSERT_EQ(0, motionArgs.flags);
6890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6891 ASSERT_EQ(0, motionArgs.buttonState);
6892 ASSERT_EQ(0, motionArgs.edgeFlags);
6893 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6896 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6899 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6901 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6902 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6903 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6904 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6905
6906 // First finger up.
6907 x2 += 15; y2 -= 20;
6908 processPosition(mapper, x2, y2);
6909 processMTSync(mapper);
6910 processSync(mapper);
6911
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6914 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6915 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6916 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006917 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918 ASSERT_EQ(0, motionArgs.flags);
6919 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6920 ASSERT_EQ(0, motionArgs.buttonState);
6921 ASSERT_EQ(0, motionArgs.edgeFlags);
6922 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6923 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6925 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6926 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6928 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6930 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6931 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6932 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6933 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6934
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6936 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6937 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6938 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6939 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6941 ASSERT_EQ(0, motionArgs.flags);
6942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6943 ASSERT_EQ(0, motionArgs.buttonState);
6944 ASSERT_EQ(0, motionArgs.edgeFlags);
6945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6946 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6950 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6951 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6952 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6953
6954 // Move.
6955 x2 += 20; y2 -= 25;
6956 processPosition(mapper, x2, y2);
6957 processMTSync(mapper);
6958 processSync(mapper);
6959
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6962 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6964 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6966 ASSERT_EQ(0, motionArgs.flags);
6967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6968 ASSERT_EQ(0, motionArgs.buttonState);
6969 ASSERT_EQ(0, motionArgs.edgeFlags);
6970 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6971 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6974 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6975 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6976 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6978
6979 // New finger down.
6980 int32_t x3 = 700, y3 = 300;
6981 processPosition(mapper, x2, y2);
6982 processMTSync(mapper);
6983 processPosition(mapper, x3, y3);
6984 processMTSync(mapper);
6985 processSync(mapper);
6986
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6989 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6991 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006992 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 ASSERT_EQ(0, motionArgs.flags);
6994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6995 ASSERT_EQ(0, motionArgs.buttonState);
6996 ASSERT_EQ(0, motionArgs.edgeFlags);
6997 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7000 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7003 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7005 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7006 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7007 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7008 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7009
7010 // Second finger up.
7011 x3 += 30; y3 -= 20;
7012 processPosition(mapper, x3, y3);
7013 processMTSync(mapper);
7014 processSync(mapper);
7015
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7018 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7019 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7020 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007021 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022 ASSERT_EQ(0, motionArgs.flags);
7023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7024 ASSERT_EQ(0, motionArgs.buttonState);
7025 ASSERT_EQ(0, motionArgs.edgeFlags);
7026 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7027 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7029 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7030 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7032 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7034 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7035 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7036 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7037 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7038
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7045 ASSERT_EQ(0, motionArgs.flags);
7046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7047 ASSERT_EQ(0, motionArgs.buttonState);
7048 ASSERT_EQ(0, motionArgs.edgeFlags);
7049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7053 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7057
7058 // Last finger up.
7059 processMTSync(mapper);
7060 processSync(mapper);
7061
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7068 ASSERT_EQ(0, motionArgs.flags);
7069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7070 ASSERT_EQ(0, motionArgs.buttonState);
7071 ASSERT_EQ(0, motionArgs.edgeFlags);
7072 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7076 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7080
7081 // Should not have sent any more keys or motions.
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7084}
7085
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007086TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7087 addConfigurationProperty("touch.deviceType", "touchScreen");
7088 prepareDisplay(DISPLAY_ORIENTATION_0);
7089
7090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7091 /*fuzz*/ 0, /*resolution*/ 10);
7092 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7093 /*fuzz*/ 0, /*resolution*/ 11);
7094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7095 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7097 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7099 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7101 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7102
7103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7104
7105 // X and Y axes
7106 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7107 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7108 // Touch major and minor
7109 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7110 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7111 // Tool major and minor
7112 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7113 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7114}
7115
7116TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7117 addConfigurationProperty("touch.deviceType", "touchScreen");
7118 prepareDisplay(DISPLAY_ORIENTATION_0);
7119
7120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7121 /*fuzz*/ 0, /*resolution*/ 10);
7122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7123 /*fuzz*/ 0, /*resolution*/ 11);
7124
7125 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7126
7127 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7128
7129 // Touch major and minor
7130 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7131 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7132 // Tool major and minor
7133 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7134 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7135}
7136
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 addConfigurationProperty("touch.deviceType", "touchScreen");
7139 prepareDisplay(DISPLAY_ORIENTATION_0);
7140 prepareAxes(POSITION | ID);
7141 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007142 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143
arthurhungdcef2dc2020-08-11 14:47:50 +08007144 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145
7146 NotifyMotionArgs motionArgs;
7147
7148 // Two fingers down at once.
7149 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7150 processPosition(mapper, x1, y1);
7151 processId(mapper, 1);
7152 processMTSync(mapper);
7153 processPosition(mapper, x2, y2);
7154 processId(mapper, 2);
7155 processMTSync(mapper);
7156 processSync(mapper);
7157
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7160 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7161 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7164 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007167 ASSERT_EQ(ACTION_POINTER_1_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(x1), toDisplayY(y1), 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 // Move.
7179 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7180 processPosition(mapper, x1, y1);
7181 processId(mapper, 1);
7182 processMTSync(mapper);
7183 processPosition(mapper, x2, y2);
7184 processId(mapper, 2);
7185 processMTSync(mapper);
7186 processSync(mapper);
7187
7188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7190 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7191 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7193 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7196 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7198 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7199
7200 // First finger up.
7201 x2 += 15; y2 -= 20;
7202 processPosition(mapper, x2, y2);
7203 processId(mapper, 2);
7204 processMTSync(mapper);
7205 processSync(mapper);
7206
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007208 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7210 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7212 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7215 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7217 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7218
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7222 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7226
7227 // Move.
7228 x2 += 20; y2 -= 25;
7229 processPosition(mapper, x2, y2);
7230 processId(mapper, 2);
7231 processMTSync(mapper);
7232 processSync(mapper);
7233
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7236 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7237 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7241
7242 // New finger down.
7243 int32_t x3 = 700, y3 = 300;
7244 processPosition(mapper, x2, y2);
7245 processId(mapper, 2);
7246 processMTSync(mapper);
7247 processPosition(mapper, x3, y3);
7248 processId(mapper, 3);
7249 processMTSync(mapper);
7250 processSync(mapper);
7251
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007253 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7257 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7260 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7262 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7263
7264 // Second finger up.
7265 x3 += 30; y3 -= 20;
7266 processPosition(mapper, x3, y3);
7267 processId(mapper, 3);
7268 processMTSync(mapper);
7269 processSync(mapper);
7270
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007272 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7274 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7276 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7279 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7281 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7282
7283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7285 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7286 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7289 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7290
7291 // Last finger up.
7292 processMTSync(mapper);
7293 processSync(mapper);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7297 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7301 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7302
7303 // Should not have sent any more keys or motions.
7304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7306}
7307
7308TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007309 addConfigurationProperty("touch.deviceType", "touchScreen");
7310 prepareDisplay(DISPLAY_ORIENTATION_0);
7311 prepareAxes(POSITION | ID | SLOT);
7312 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314
arthurhungdcef2dc2020-08-11 14:47:50 +08007315 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007316
7317 NotifyMotionArgs motionArgs;
7318
7319 // Two fingers down at once.
7320 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7321 processPosition(mapper, x1, y1);
7322 processId(mapper, 1);
7323 processSlot(mapper, 1);
7324 processPosition(mapper, x2, y2);
7325 processId(mapper, 2);
7326 processSync(mapper);
7327
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7334 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7335
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007337 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007338 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7339 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7341 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7344 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7346 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7347
7348 // Move.
7349 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7350 processSlot(mapper, 0);
7351 processPosition(mapper, x1, y1);
7352 processSlot(mapper, 1);
7353 processPosition(mapper, x2, y2);
7354 processSync(mapper);
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7364 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7367
7368 // First finger up.
7369 x2 += 15; y2 -= 20;
7370 processSlot(mapper, 0);
7371 processId(mapper, -1);
7372 processSlot(mapper, 1);
7373 processPosition(mapper, x2, y2);
7374 processSync(mapper);
7375
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007377 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7381 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7387
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7390 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7391 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7394 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7395
7396 // Move.
7397 x2 += 20; y2 -= 25;
7398 processPosition(mapper, x2, y2);
7399 processSync(mapper);
7400
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7403 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7404 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7407 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7408
7409 // New finger down.
7410 int32_t x3 = 700, y3 = 300;
7411 processPosition(mapper, x2, y2);
7412 processSlot(mapper, 0);
7413 processId(mapper, 3);
7414 processPosition(mapper, x3, y3);
7415 processSync(mapper);
7416
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007418 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007419 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7420 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7422 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7425 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7426 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7427 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7428
7429 // Second finger up.
7430 x3 += 30; y3 -= 20;
7431 processSlot(mapper, 1);
7432 processId(mapper, -1);
7433 processSlot(mapper, 0);
7434 processPosition(mapper, x3, y3);
7435 processSync(mapper);
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007438 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7442 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7443 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7445 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7447 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7451 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7452 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7456
7457 // Last finger up.
7458 processId(mapper, -1);
7459 processSync(mapper);
7460
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7463 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7468
7469 // Should not have sent any more keys or motions.
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7472}
7473
7474TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475 addConfigurationProperty("touch.deviceType", "touchScreen");
7476 prepareDisplay(DISPLAY_ORIENTATION_0);
7477 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007478 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479
7480 // These calculations are based on the input device calibration documentation.
7481 int32_t rawX = 100;
7482 int32_t rawY = 200;
7483 int32_t rawTouchMajor = 7;
7484 int32_t rawTouchMinor = 6;
7485 int32_t rawToolMajor = 9;
7486 int32_t rawToolMinor = 8;
7487 int32_t rawPressure = 11;
7488 int32_t rawDistance = 0;
7489 int32_t rawOrientation = 3;
7490 int32_t id = 5;
7491
7492 float x = toDisplayX(rawX);
7493 float y = toDisplayY(rawY);
7494 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7495 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7496 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7497 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7498 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7499 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7500 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7501 float distance = float(rawDistance);
7502
7503 processPosition(mapper, rawX, rawY);
7504 processTouchMajor(mapper, rawTouchMajor);
7505 processTouchMinor(mapper, rawTouchMinor);
7506 processToolMajor(mapper, rawToolMajor);
7507 processToolMinor(mapper, rawToolMinor);
7508 processPressure(mapper, rawPressure);
7509 processOrientation(mapper, rawOrientation);
7510 processDistance(mapper, rawDistance);
7511 processId(mapper, id);
7512 processMTSync(mapper);
7513 processSync(mapper);
7514
7515 NotifyMotionArgs args;
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7517 ASSERT_EQ(0, args.pointerProperties[0].id);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7519 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7520 orientation, distance));
7521}
7522
7523TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 addConfigurationProperty("touch.deviceType", "touchScreen");
7525 prepareDisplay(DISPLAY_ORIENTATION_0);
7526 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7527 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007528 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529
7530 // These calculations are based on the input device calibration documentation.
7531 int32_t rawX = 100;
7532 int32_t rawY = 200;
7533 int32_t rawTouchMajor = 140;
7534 int32_t rawTouchMinor = 120;
7535 int32_t rawToolMajor = 180;
7536 int32_t rawToolMinor = 160;
7537
7538 float x = toDisplayX(rawX);
7539 float y = toDisplayY(rawY);
7540 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7541 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7542 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7543 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7544 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7545
7546 processPosition(mapper, rawX, rawY);
7547 processTouchMajor(mapper, rawTouchMajor);
7548 processTouchMinor(mapper, rawTouchMinor);
7549 processToolMajor(mapper, rawToolMajor);
7550 processToolMinor(mapper, rawToolMinor);
7551 processMTSync(mapper);
7552 processSync(mapper);
7553
7554 NotifyMotionArgs args;
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7557 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7558}
7559
7560TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007561 addConfigurationProperty("touch.deviceType", "touchScreen");
7562 prepareDisplay(DISPLAY_ORIENTATION_0);
7563 prepareAxes(POSITION | TOUCH | TOOL);
7564 addConfigurationProperty("touch.size.calibration", "diameter");
7565 addConfigurationProperty("touch.size.scale", "10");
7566 addConfigurationProperty("touch.size.bias", "160");
7567 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007568 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569
7570 // These calculations are based on the input device calibration documentation.
7571 // Note: We only provide a single common touch/tool value because the device is assumed
7572 // not to emit separate values for each pointer (isSummed = 1).
7573 int32_t rawX = 100;
7574 int32_t rawY = 200;
7575 int32_t rawX2 = 150;
7576 int32_t rawY2 = 250;
7577 int32_t rawTouchMajor = 5;
7578 int32_t rawToolMajor = 8;
7579
7580 float x = toDisplayX(rawX);
7581 float y = toDisplayY(rawY);
7582 float x2 = toDisplayX(rawX2);
7583 float y2 = toDisplayY(rawY2);
7584 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7585 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7586 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7587
7588 processPosition(mapper, rawX, rawY);
7589 processTouchMajor(mapper, rawTouchMajor);
7590 processToolMajor(mapper, rawToolMajor);
7591 processMTSync(mapper);
7592 processPosition(mapper, rawX2, rawY2);
7593 processTouchMajor(mapper, rawTouchMajor);
7594 processToolMajor(mapper, rawToolMajor);
7595 processMTSync(mapper);
7596 processSync(mapper);
7597
7598 NotifyMotionArgs args;
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007603 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604 ASSERT_EQ(size_t(2), args.pointerCount);
7605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7606 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7608 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7609}
7610
7611TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612 addConfigurationProperty("touch.deviceType", "touchScreen");
7613 prepareDisplay(DISPLAY_ORIENTATION_0);
7614 prepareAxes(POSITION | TOUCH | TOOL);
7615 addConfigurationProperty("touch.size.calibration", "area");
7616 addConfigurationProperty("touch.size.scale", "43");
7617 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007618 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619
7620 // These calculations are based on the input device calibration documentation.
7621 int32_t rawX = 100;
7622 int32_t rawY = 200;
7623 int32_t rawTouchMajor = 5;
7624 int32_t rawToolMajor = 8;
7625
7626 float x = toDisplayX(rawX);
7627 float y = toDisplayY(rawY);
7628 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7629 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7630 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7631
7632 processPosition(mapper, rawX, rawY);
7633 processTouchMajor(mapper, rawTouchMajor);
7634 processToolMajor(mapper, rawToolMajor);
7635 processMTSync(mapper);
7636 processSync(mapper);
7637
7638 NotifyMotionArgs args;
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7641 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7642}
7643
7644TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645 addConfigurationProperty("touch.deviceType", "touchScreen");
7646 prepareDisplay(DISPLAY_ORIENTATION_0);
7647 prepareAxes(POSITION | PRESSURE);
7648 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7649 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651
Michael Wrightaa449c92017-12-13 21:21:43 +00007652 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007653 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007654 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7655 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7656 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7657
Michael Wrightd02c5b62014-02-10 15:10:22 -08007658 // These calculations are based on the input device calibration documentation.
7659 int32_t rawX = 100;
7660 int32_t rawY = 200;
7661 int32_t rawPressure = 60;
7662
7663 float x = toDisplayX(rawX);
7664 float y = toDisplayY(rawY);
7665 float pressure = float(rawPressure) * 0.01f;
7666
7667 processPosition(mapper, rawX, rawY);
7668 processPressure(mapper, rawPressure);
7669 processMTSync(mapper);
7670 processSync(mapper);
7671
7672 NotifyMotionArgs args;
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7675 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7676}
7677
7678TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007679 addConfigurationProperty("touch.deviceType", "touchScreen");
7680 prepareDisplay(DISPLAY_ORIENTATION_0);
7681 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007683
7684 NotifyMotionArgs motionArgs;
7685 NotifyKeyArgs keyArgs;
7686
7687 processId(mapper, 1);
7688 processPosition(mapper, 100, 200);
7689 processSync(mapper);
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7692 ASSERT_EQ(0, motionArgs.buttonState);
7693
7694 // press BTN_LEFT, release BTN_LEFT
7695 processKey(mapper, BTN_LEFT, 1);
7696 processSync(mapper);
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7699 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7700
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7703 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7704
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705 processKey(mapper, BTN_LEFT, 0);
7706 processSync(mapper);
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007710
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007713 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714
7715 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7716 processKey(mapper, BTN_RIGHT, 1);
7717 processKey(mapper, BTN_MIDDLE, 1);
7718 processSync(mapper);
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7721 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7722 motionArgs.buttonState);
7723
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7727
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7730 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7731 motionArgs.buttonState);
7732
Michael Wrightd02c5b62014-02-10 15:10:22 -08007733 processKey(mapper, BTN_RIGHT, 0);
7734 processSync(mapper);
7735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007738
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007741 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007742
7743 processKey(mapper, BTN_MIDDLE, 0);
7744 processSync(mapper);
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007747 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007748
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007751 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752
7753 // press BTN_BACK, release BTN_BACK
7754 processKey(mapper, BTN_BACK, 1);
7755 processSync(mapper);
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7757 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7758 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007759
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007762 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7763
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7766 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767
7768 processKey(mapper, BTN_BACK, 0);
7769 processSync(mapper);
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007776 ASSERT_EQ(0, motionArgs.buttonState);
7777
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7779 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7780 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7781
7782 // press BTN_SIDE, release BTN_SIDE
7783 processKey(mapper, BTN_SIDE, 1);
7784 processSync(mapper);
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7786 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7787 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007788
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007791 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7792
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7795 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796
7797 processKey(mapper, BTN_SIDE, 0);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007801 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007805 ASSERT_EQ(0, motionArgs.buttonState);
7806
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7808 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7809 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7810
7811 // press BTN_FORWARD, release BTN_FORWARD
7812 processKey(mapper, BTN_FORWARD, 1);
7813 processSync(mapper);
7814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7815 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7816 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007817
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007820 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7821
7822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7824 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825
7826 processKey(mapper, BTN_FORWARD, 0);
7827 processSync(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007829 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007831
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007834 ASSERT_EQ(0, motionArgs.buttonState);
7835
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7837 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7838 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7839
7840 // press BTN_EXTRA, release BTN_EXTRA
7841 processKey(mapper, BTN_EXTRA, 1);
7842 processSync(mapper);
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7844 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7845 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007846
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007849 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7850
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7853 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854
7855 processKey(mapper, BTN_EXTRA, 0);
7856 processSync(mapper);
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007863 ASSERT_EQ(0, motionArgs.buttonState);
7864
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7867 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7868
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7870
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 // press BTN_STYLUS, release BTN_STYLUS
7872 processKey(mapper, BTN_STYLUS, 1);
7873 processSync(mapper);
7874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007876 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7877
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7880 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881
7882 processKey(mapper, BTN_STYLUS, 0);
7883 processSync(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007887
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007890 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891
7892 // press BTN_STYLUS2, release BTN_STYLUS2
7893 processKey(mapper, BTN_STYLUS2, 1);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007897 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7898
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7900 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7901 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 processKey(mapper, BTN_STYLUS2, 0);
7904 processSync(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007908
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007911 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912
7913 // release touch
7914 processId(mapper, -1);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7917 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7918 ASSERT_EQ(0, motionArgs.buttonState);
7919}
7920
7921TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 addConfigurationProperty("touch.deviceType", "touchScreen");
7923 prepareDisplay(DISPLAY_ORIENTATION_0);
7924 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007925 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926
7927 NotifyMotionArgs motionArgs;
7928
7929 // default tool type is finger
7930 processId(mapper, 1);
7931 processPosition(mapper, 100, 200);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7936
7937 // eraser
7938 processKey(mapper, BTN_TOOL_RUBBER, 1);
7939 processSync(mapper);
7940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7942 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7943
7944 // stylus
7945 processKey(mapper, BTN_TOOL_RUBBER, 0);
7946 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7951
7952 // brush
7953 processKey(mapper, BTN_TOOL_PEN, 0);
7954 processKey(mapper, BTN_TOOL_BRUSH, 1);
7955 processSync(mapper);
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7959
7960 // pencil
7961 processKey(mapper, BTN_TOOL_BRUSH, 0);
7962 processKey(mapper, BTN_TOOL_PENCIL, 1);
7963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7967
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007968 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 processKey(mapper, BTN_TOOL_PENCIL, 0);
7970 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7971 processSync(mapper);
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7975
7976 // mouse
7977 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7978 processKey(mapper, BTN_TOOL_MOUSE, 1);
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_MOUSE, motionArgs.pointerProperties[0].toolType);
7983
7984 // lens
7985 processKey(mapper, BTN_TOOL_MOUSE, 0);
7986 processKey(mapper, BTN_TOOL_LENS, 1);
7987 processSync(mapper);
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7991
7992 // double-tap
7993 processKey(mapper, BTN_TOOL_LENS, 0);
7994 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7999
8000 // triple-tap
8001 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8002 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8006 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8007
8008 // quad-tap
8009 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8010 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8011 processSync(mapper);
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8015
8016 // finger
8017 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8018 processKey(mapper, BTN_TOOL_FINGER, 1);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8023
8024 // stylus trumps finger
8025 processKey(mapper, BTN_TOOL_PEN, 1);
8026 processSync(mapper);
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8030
8031 // eraser trumps stylus
8032 processKey(mapper, BTN_TOOL_RUBBER, 1);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8037
8038 // mouse trumps eraser
8039 processKey(mapper, BTN_TOOL_MOUSE, 1);
8040 processSync(mapper);
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8044
8045 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8046 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8047 processSync(mapper);
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051
8052 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8053 processToolType(mapper, MT_TOOL_PEN);
8054 processSync(mapper);
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8058
8059 // back to default tool type
8060 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8061 processKey(mapper, BTN_TOOL_MOUSE, 0);
8062 processKey(mapper, BTN_TOOL_RUBBER, 0);
8063 processKey(mapper, BTN_TOOL_PEN, 0);
8064 processKey(mapper, BTN_TOOL_FINGER, 0);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8069}
8070
8071TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 addConfigurationProperty("touch.deviceType", "touchScreen");
8073 prepareDisplay(DISPLAY_ORIENTATION_0);
8074 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008075 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008076 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077
8078 NotifyMotionArgs motionArgs;
8079
8080 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8081 processId(mapper, 1);
8082 processPosition(mapper, 100, 200);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8087 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8092 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8093
8094 // move a little
8095 processPosition(mapper, 150, 250);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8100 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8101
8102 // down when BTN_TOUCH is pressed, pressure defaults to 1
8103 processKey(mapper, BTN_TOUCH, 1);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8106 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8108 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8109
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8113 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8114
8115 // up when BTN_TOUCH is released, hover restored
8116 processKey(mapper, BTN_TOUCH, 0);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8119 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8121 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8122
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8126 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8131 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8132
8133 // exit hover when pointer goes away
8134 processId(mapper, -1);
8135 processSync(mapper);
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8139 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8140}
8141
8142TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 addConfigurationProperty("touch.deviceType", "touchScreen");
8144 prepareDisplay(DISPLAY_ORIENTATION_0);
8145 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008146 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147
8148 NotifyMotionArgs motionArgs;
8149
8150 // initially hovering because pressure is 0
8151 processId(mapper, 1);
8152 processPosition(mapper, 100, 200);
8153 processPressure(mapper, 0);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8157 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8158 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8163 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8164
8165 // move a little
8166 processPosition(mapper, 150, 250);
8167 processSync(mapper);
8168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8171 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8172
8173 // down when pressure becomes non-zero
8174 processPressure(mapper, RAW_PRESSURE_MAX);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8179 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8182 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8184 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8185
8186 // up when pressure becomes 0, hover restored
8187 processPressure(mapper, 0);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8192 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8197 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8200 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8202 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8203
8204 // exit hover when pointer goes away
8205 processId(mapper, -1);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8210 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8211}
8212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008213/**
8214 * Set the input device port <--> display port associations, and check that the
8215 * events are routed to the display that matches the display port.
8216 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8217 */
8218TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008219 const std::string usb2 = "USB2";
8220 const uint8_t hdmi1 = 0;
8221 const uint8_t hdmi2 = 1;
8222 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008223 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008224
8225 addConfigurationProperty("touch.deviceType", "touchScreen");
8226 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008227 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008228
8229 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8230 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8231
8232 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8233 // for this input device is specified, and the matching viewport is not present,
8234 // the input device should be disabled (at the mapper level).
8235
8236 // Add viewport for display 2 on hdmi2
8237 prepareSecondaryDisplay(type, hdmi2);
8238 // Send a touch event
8239 processPosition(mapper, 100, 100);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8242
8243 // Add viewport for display 1 on hdmi1
8244 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8245 // Send a touch event again
8246 processPosition(mapper, 100, 100);
8247 processSync(mapper);
8248
8249 NotifyMotionArgs args;
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8251 ASSERT_EQ(DISPLAY_ID, args.displayId);
8252}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008254TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008255 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008256 std::shared_ptr<FakePointerController> fakePointerController =
8257 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008258 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008259 fakePointerController->setPosition(100, 200);
8260 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008261 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008262
Garfield Tan888a6a42020-01-09 11:39:16 -08008263 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008264 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008265
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008266 prepareDisplay(DISPLAY_ORIENTATION_0);
8267 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008268 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008269
8270 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008271 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008272
8273 NotifyMotionArgs motionArgs;
8274 processPosition(mapper, 100, 100);
8275 processSync(mapper);
8276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8279 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8280}
8281
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008282/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008283 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8284 */
8285TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8286 addConfigurationProperty("touch.deviceType", "touchScreen");
8287 prepareAxes(POSITION);
8288 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8289
8290 prepareDisplay(DISPLAY_ORIENTATION_0);
8291 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8292 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8293 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8294 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8295
8296 NotifyMotionArgs args;
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8298 ASSERT_EQ(26, args.readTime);
8299
8300 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8301 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8302 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8303
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8305 ASSERT_EQ(33, args.readTime);
8306}
8307
8308/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008309 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8310 * events should not be delivered to the listener.
8311 */
8312TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8313 addConfigurationProperty("touch.deviceType", "touchScreen");
8314 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8315 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8316 ViewportType::INTERNAL);
8317 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8318 prepareAxes(POSITION);
8319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8320
8321 NotifyMotionArgs motionArgs;
8322 processPosition(mapper, 100, 100);
8323 processSync(mapper);
8324
8325 mFakeListener->assertNotifyMotionWasNotCalled();
8326}
8327
Garfield Tanc734e4f2021-01-15 20:01:39 -08008328TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8329 addConfigurationProperty("touch.deviceType", "touchScreen");
8330 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8331 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8332 ViewportType::INTERNAL);
8333 std::optional<DisplayViewport> optionalDisplayViewport =
8334 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8335 ASSERT_TRUE(optionalDisplayViewport.has_value());
8336 DisplayViewport displayViewport = *optionalDisplayViewport;
8337
8338 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8339 prepareAxes(POSITION);
8340 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8341
8342 // Finger down
8343 int32_t x = 100, y = 100;
8344 processPosition(mapper, x, y);
8345 processSync(mapper);
8346
8347 NotifyMotionArgs motionArgs;
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8350
8351 // Deactivate display viewport
8352 displayViewport.isActive = false;
8353 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8354 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8355
8356 // Finger move
8357 x += 10, y += 10;
8358 processPosition(mapper, x, y);
8359 processSync(mapper);
8360
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8363
8364 // Reactivate display viewport
8365 displayViewport.isActive = true;
8366 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8367 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8368
8369 // Finger move again
8370 x += 10, y += 10;
8371 processPosition(mapper, x, y);
8372 processSync(mapper);
8373
8374 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8375 // no pointer on the touch device.
8376 mFakeListener->assertNotifyMotionWasNotCalled();
8377}
8378
Arthur Hung7c645402019-01-25 17:45:42 +08008379TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8380 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008381 prepareAxes(POSITION | ID | SLOT);
8382 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008383 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008384
8385 // Create the second touch screen device, and enable multi fingers.
8386 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008387 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008388 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008389 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008390 std::shared_ptr<InputDevice> device2 =
8391 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8392 Flags<InputDeviceClass>(0));
8393
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008394 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8395 0 /*flat*/, 0 /*fuzz*/);
8396 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8397 0 /*flat*/, 0 /*fuzz*/);
8398 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8399 0 /*flat*/, 0 /*fuzz*/);
8400 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8401 0 /*flat*/, 0 /*fuzz*/);
8402 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8403 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8404 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008405
8406 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008407 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008408 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8409 device2->reset(ARBITRARY_TIME);
8410
8411 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008412 std::shared_ptr<FakePointerController> fakePointerController =
8413 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008414 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008415
8416 // Setup policy for associated displays and show touches.
8417 const uint8_t hdmi1 = 0;
8418 const uint8_t hdmi2 = 1;
8419 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8420 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8421 mFakePolicy->setShowTouches(true);
8422
8423 // Create displays.
8424 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008425 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008426
8427 // Default device will reconfigure above, need additional reconfiguration for another device.
8428 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008429 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008430
8431 // Two fingers down at default display.
8432 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8433 processPosition(mapper, x1, y1);
8434 processId(mapper, 1);
8435 processSlot(mapper, 1);
8436 processPosition(mapper, x2, y2);
8437 processId(mapper, 2);
8438 processSync(mapper);
8439
8440 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8441 fakePointerController->getSpots().find(DISPLAY_ID);
8442 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8443 ASSERT_EQ(size_t(2), iter->second.size());
8444
8445 // Two fingers down at second display.
8446 processPosition(mapper2, x1, y1);
8447 processId(mapper2, 1);
8448 processSlot(mapper2, 1);
8449 processPosition(mapper2, x2, y2);
8450 processId(mapper2, 2);
8451 processSync(mapper2);
8452
8453 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8454 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8455 ASSERT_EQ(size_t(2), iter->second.size());
8456}
8457
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008458TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008459 prepareAxes(POSITION);
8460 addConfigurationProperty("touch.deviceType", "touchScreen");
8461 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008463
8464 NotifyMotionArgs motionArgs;
8465 // Unrotated video frame
8466 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8467 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008468 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008469 processPosition(mapper, 100, 200);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(frames, motionArgs.videoFrames);
8473
8474 // Subsequent touch events should not have any videoframes
8475 // This is implemented separately in FakeEventHub,
8476 // but that should match the behaviour of TouchVideoDevice.
8477 processPosition(mapper, 200, 200);
8478 processSync(mapper);
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8480 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8481}
8482
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008483TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008484 prepareAxes(POSITION);
8485 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008486 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008487 // Unrotated video frame
8488 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8489 NotifyMotionArgs motionArgs;
8490
8491 // Test all 4 orientations
8492 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008493 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8494 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8495 clearViewports();
8496 prepareDisplay(orientation);
8497 std::vector<TouchVideoFrame> frames{frame};
8498 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8499 processPosition(mapper, 100, 200);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(frames, motionArgs.videoFrames);
8503 }
8504}
8505
8506TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8507 prepareAxes(POSITION);
8508 addConfigurationProperty("touch.deviceType", "touchScreen");
8509 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8510 // orientation-aware are affected by display rotation.
8511 addConfigurationProperty("touch.orientationAware", "0");
8512 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8513 // Unrotated video frame
8514 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8515 NotifyMotionArgs motionArgs;
8516
8517 // Test all 4 orientations
8518 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008519 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8520 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8521 clearViewports();
8522 prepareDisplay(orientation);
8523 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008524 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008525 processPosition(mapper, 100, 200);
8526 processSync(mapper);
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008528 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8529 // compared to the display. This is so that when the window transform (which contains the
8530 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8531 // window's coordinate space.
8532 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008533 ASSERT_EQ(frames, motionArgs.videoFrames);
8534 }
8535}
8536
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008537TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008538 prepareAxes(POSITION);
8539 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008541 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8542 // so mix these.
8543 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8544 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8545 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8546 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8547 NotifyMotionArgs motionArgs;
8548
8549 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008550 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008551 processPosition(mapper, 100, 200);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008554 ASSERT_EQ(frames, motionArgs.videoFrames);
8555}
8556
8557TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8558 prepareAxes(POSITION);
8559 addConfigurationProperty("touch.deviceType", "touchScreen");
8560 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8561 // orientation-aware are affected by display rotation.
8562 addConfigurationProperty("touch.orientationAware", "0");
8563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8564 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8565 // so mix these.
8566 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8567 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8568 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8569 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8570 NotifyMotionArgs motionArgs;
8571
8572 prepareDisplay(DISPLAY_ORIENTATION_90);
8573 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8574 processPosition(mapper, 100, 200);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8578 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8579 // compared to the display. This is so that when the window transform (which contains the
8580 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8581 // window's coordinate space.
8582 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8583 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008584 ASSERT_EQ(frames, motionArgs.videoFrames);
8585}
8586
Arthur Hung9da14732019-09-02 16:16:58 +08008587/**
8588 * If we had defined port associations, but the viewport is not ready, the touch device would be
8589 * expected to be disabled, and it should be enabled after the viewport has found.
8590 */
8591TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008592 constexpr uint8_t hdmi2 = 1;
8593 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008594 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008595
8596 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8597
8598 addConfigurationProperty("touch.deviceType", "touchScreen");
8599 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008600 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008601
8602 ASSERT_EQ(mDevice->isEnabled(), false);
8603
8604 // Add display on hdmi2, the device should be enabled and can receive touch event.
8605 prepareSecondaryDisplay(type, hdmi2);
8606 ASSERT_EQ(mDevice->isEnabled(), true);
8607
8608 // Send a touch event.
8609 processPosition(mapper, 100, 100);
8610 processSync(mapper);
8611
8612 NotifyMotionArgs args;
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8614 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8615}
8616
Arthur Hung421eb1c2020-01-16 00:09:42 +08008617TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008618 addConfigurationProperty("touch.deviceType", "touchScreen");
8619 prepareDisplay(DISPLAY_ORIENTATION_0);
8620 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008621 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008622
8623 NotifyMotionArgs motionArgs;
8624
8625 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8626 // finger down
8627 processId(mapper, 1);
8628 processPosition(mapper, x1, y1);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8633
8634 // finger move
8635 processId(mapper, 1);
8636 processPosition(mapper, x2, y2);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8641
8642 // finger up.
8643 processId(mapper, -1);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8648
8649 // new finger down
8650 processId(mapper, 1);
8651 processPosition(mapper, x3, y3);
8652 processSync(mapper);
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8656}
8657
8658/**
arthurhungcc7f9802020-04-30 17:55:40 +08008659 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8660 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008661 */
arthurhungcc7f9802020-04-30 17:55:40 +08008662TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008663 addConfigurationProperty("touch.deviceType", "touchScreen");
8664 prepareDisplay(DISPLAY_ORIENTATION_0);
8665 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008667
8668 NotifyMotionArgs motionArgs;
8669
8670 // default tool type is finger
8671 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008672 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008673 processPosition(mapper, x1, y1);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8678
8679 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8680 processToolType(mapper, MT_TOOL_PALM);
8681 processSync(mapper);
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8684
8685 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008686 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008687 processPosition(mapper, x2, y2);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8690
8691 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008692 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008693 processSync(mapper);
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8695
8696 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008697 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008698 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008699 processPosition(mapper, x3, y3);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8702 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8704}
8705
arthurhungbf89a482020-04-17 17:37:55 +08008706/**
arthurhungcc7f9802020-04-30 17:55:40 +08008707 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8708 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008709 */
arthurhungcc7f9802020-04-30 17:55:40 +08008710TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008711 addConfigurationProperty("touch.deviceType", "touchScreen");
8712 prepareDisplay(DISPLAY_ORIENTATION_0);
8713 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8715
8716 NotifyMotionArgs motionArgs;
8717
8718 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008719 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8720 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008721 processPosition(mapper, x1, y1);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8724 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8726
8727 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008728 processSlot(mapper, SECOND_SLOT);
8729 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008730 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008731 processSync(mapper);
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008733 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8735
8736 // If the tool type of the first finger changes to MT_TOOL_PALM,
8737 // we expect to receive ACTION_POINTER_UP with cancel flag.
8738 processSlot(mapper, FIRST_SLOT);
8739 processId(mapper, FIRST_TRACKING_ID);
8740 processToolType(mapper, MT_TOOL_PALM);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008743 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008744 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8745
8746 // The following MOVE events of second finger should be processed.
8747 processSlot(mapper, SECOND_SLOT);
8748 processId(mapper, SECOND_TRACKING_ID);
8749 processPosition(mapper, x2 + 1, y2 + 1);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8753 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8754
8755 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8756 // it. Second finger receive move.
8757 processSlot(mapper, FIRST_SLOT);
8758 processId(mapper, INVALID_TRACKING_ID);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8762 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8763
8764 // Second finger keeps moving.
8765 processSlot(mapper, SECOND_SLOT);
8766 processId(mapper, SECOND_TRACKING_ID);
8767 processPosition(mapper, x2 + 2, y2 + 2);
8768 processSync(mapper);
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8771 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8772
8773 // Second finger up.
8774 processId(mapper, INVALID_TRACKING_ID);
8775 processSync(mapper);
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8777 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8778 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8779}
8780
8781/**
8782 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8783 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8784 */
8785TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8786 addConfigurationProperty("touch.deviceType", "touchScreen");
8787 prepareDisplay(DISPLAY_ORIENTATION_0);
8788 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8789 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8790
8791 NotifyMotionArgs motionArgs;
8792
8793 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8794 // First finger down.
8795 processId(mapper, FIRST_TRACKING_ID);
8796 processPosition(mapper, x1, y1);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8801
8802 // Second finger down.
8803 processSlot(mapper, SECOND_SLOT);
8804 processId(mapper, SECOND_TRACKING_ID);
8805 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008806 processSync(mapper);
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008808 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8810
arthurhungcc7f9802020-04-30 17:55:40 +08008811 // If the tool type of the first finger changes to MT_TOOL_PALM,
8812 // we expect to receive ACTION_POINTER_UP with cancel flag.
8813 processSlot(mapper, FIRST_SLOT);
8814 processId(mapper, FIRST_TRACKING_ID);
8815 processToolType(mapper, MT_TOOL_PALM);
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008818 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008819 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8820
8821 // Second finger keeps moving.
8822 processSlot(mapper, SECOND_SLOT);
8823 processId(mapper, SECOND_TRACKING_ID);
8824 processPosition(mapper, x2 + 1, y2 + 1);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8828
8829 // second finger becomes palm, receive cancel due to only 1 finger is active.
8830 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008831 processToolType(mapper, MT_TOOL_PALM);
8832 processSync(mapper);
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8834 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8835
arthurhungcc7f9802020-04-30 17:55:40 +08008836 // third finger down.
8837 processSlot(mapper, THIRD_SLOT);
8838 processId(mapper, THIRD_TRACKING_ID);
8839 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008840 processPosition(mapper, x3, y3);
8841 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8844 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008845 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8846
8847 // third finger move
8848 processId(mapper, THIRD_TRACKING_ID);
8849 processPosition(mapper, x3 + 1, y3 + 1);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8853
8854 // first finger up, third finger receive move.
8855 processSlot(mapper, FIRST_SLOT);
8856 processId(mapper, INVALID_TRACKING_ID);
8857 processSync(mapper);
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8860 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8861
8862 // second finger up, third finger receive move.
8863 processSlot(mapper, SECOND_SLOT);
8864 processId(mapper, INVALID_TRACKING_ID);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8868 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8869
8870 // third finger up.
8871 processSlot(mapper, THIRD_SLOT);
8872 processId(mapper, INVALID_TRACKING_ID);
8873 processSync(mapper);
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8876 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8877}
8878
8879/**
8880 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8881 * and the active finger could still be allowed to receive the events
8882 */
8883TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8884 addConfigurationProperty("touch.deviceType", "touchScreen");
8885 prepareDisplay(DISPLAY_ORIENTATION_0);
8886 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8887 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8888
8889 NotifyMotionArgs motionArgs;
8890
8891 // default tool type is finger
8892 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8893 processId(mapper, FIRST_TRACKING_ID);
8894 processPosition(mapper, x1, y1);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8899
8900 // Second finger down.
8901 processSlot(mapper, SECOND_SLOT);
8902 processId(mapper, SECOND_TRACKING_ID);
8903 processPosition(mapper, x2, y2);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008906 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008907 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8908
8909 // If the tool type of the second finger changes to MT_TOOL_PALM,
8910 // we expect to receive ACTION_POINTER_UP with cancel flag.
8911 processId(mapper, SECOND_TRACKING_ID);
8912 processToolType(mapper, MT_TOOL_PALM);
8913 processSync(mapper);
8914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008915 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008916 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8917
8918 // The following MOVE event should be processed.
8919 processSlot(mapper, FIRST_SLOT);
8920 processId(mapper, FIRST_TRACKING_ID);
8921 processPosition(mapper, x1 + 1, y1 + 1);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8925 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8926
8927 // second finger up.
8928 processSlot(mapper, SECOND_SLOT);
8929 processId(mapper, INVALID_TRACKING_ID);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8933
8934 // first finger keep moving
8935 processSlot(mapper, FIRST_SLOT);
8936 processId(mapper, FIRST_TRACKING_ID);
8937 processPosition(mapper, x1 + 2, y1 + 2);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8941
8942 // first finger up.
8943 processId(mapper, INVALID_TRACKING_ID);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8947 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008948}
8949
Arthur Hung9ad18942021-06-19 02:04:46 +00008950/**
8951 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8952 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8953 * cause slot be valid again.
8954 */
8955TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8956 addConfigurationProperty("touch.deviceType", "touchScreen");
8957 prepareDisplay(DISPLAY_ORIENTATION_0);
8958 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8960
8961 NotifyMotionArgs motionArgs;
8962
8963 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8964 // First finger down.
8965 processId(mapper, FIRST_TRACKING_ID);
8966 processPosition(mapper, x1, y1);
8967 processPressure(mapper, RAW_PRESSURE_MAX);
8968 processSync(mapper);
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8971 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8972
8973 // First finger move.
8974 processId(mapper, FIRST_TRACKING_ID);
8975 processPosition(mapper, x1 + 1, y1 + 1);
8976 processPressure(mapper, RAW_PRESSURE_MAX);
8977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8980 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8981
8982 // Second finger down.
8983 processSlot(mapper, SECOND_SLOT);
8984 processId(mapper, SECOND_TRACKING_ID);
8985 processPosition(mapper, x2, y2);
8986 processPressure(mapper, RAW_PRESSURE_MAX);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008989 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008990 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8991
8992 // second finger up with some unexpected data.
8993 processSlot(mapper, SECOND_SLOT);
8994 processId(mapper, INVALID_TRACKING_ID);
8995 processPosition(mapper, x2, y2);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008998 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008999 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9000
9001 // first finger up with some unexpected data.
9002 processSlot(mapper, FIRST_SLOT);
9003 processId(mapper, INVALID_TRACKING_ID);
9004 processPosition(mapper, x2, y2);
9005 processPressure(mapper, RAW_PRESSURE_MAX);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9008 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9009 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9010}
9011
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009012// --- MultiTouchInputMapperTest_ExternalDevice ---
9013
9014class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9015protected:
Chris Yea52ade12020-08-27 16:49:20 -07009016 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009017};
9018
9019/**
9020 * Expect fallback to internal viewport if device is external and external viewport is not present.
9021 */
9022TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9023 prepareAxes(POSITION);
9024 addConfigurationProperty("touch.deviceType", "touchScreen");
9025 prepareDisplay(DISPLAY_ORIENTATION_0);
9026 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9027
9028 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9029
9030 NotifyMotionArgs motionArgs;
9031
9032 // Expect the event to be sent to the internal viewport,
9033 // because an external viewport is not present.
9034 processPosition(mapper, 100, 100);
9035 processSync(mapper);
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9038
9039 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009040 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009041 processPosition(mapper, 100, 100);
9042 processSync(mapper);
9043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9044 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9045}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009046
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009047TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9048 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9049 std::shared_ptr<FakePointerController> fakePointerController =
9050 std::make_shared<FakePointerController>();
9051 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9052 fakePointerController->setPosition(0, 0);
9053 fakePointerController->setButtonState(0);
9054
9055 // prepare device and capture
9056 prepareDisplay(DISPLAY_ORIENTATION_0);
9057 prepareAxes(POSITION | ID | SLOT);
9058 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9059 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9060 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009061 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009062 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9063
9064 // captured touchpad should be a touchpad source
9065 NotifyDeviceResetArgs resetArgs;
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9067 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9068
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009069 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009070
9071 const InputDeviceInfo::MotionRange* relRangeX =
9072 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9073 ASSERT_NE(relRangeX, nullptr);
9074 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9075 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9076 const InputDeviceInfo::MotionRange* relRangeY =
9077 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9078 ASSERT_NE(relRangeY, nullptr);
9079 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9080 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9081
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009082 // run captured pointer tests - note that this is unscaled, so input listener events should be
9083 // identical to what the hardware sends (accounting for any
9084 // calibration).
9085 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009086 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009087 processId(mapper, 1);
9088 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9089 processKey(mapper, BTN_TOUCH, 1);
9090 processSync(mapper);
9091
9092 // expect coord[0] to contain initial location of touch 0
9093 NotifyMotionArgs args;
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9096 ASSERT_EQ(1U, args.pointerCount);
9097 ASSERT_EQ(0, args.pointerProperties[0].id);
9098 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9099 ASSERT_NO_FATAL_FAILURE(
9100 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9101
9102 // FINGER 1 DOWN
9103 processSlot(mapper, 1);
9104 processId(mapper, 2);
9105 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9106 processSync(mapper);
9107
9108 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009110 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009111 ASSERT_EQ(2U, args.pointerCount);
9112 ASSERT_EQ(0, args.pointerProperties[0].id);
9113 ASSERT_EQ(1, args.pointerProperties[1].id);
9114 ASSERT_NO_FATAL_FAILURE(
9115 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9116 ASSERT_NO_FATAL_FAILURE(
9117 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9118
9119 // FINGER 1 MOVE
9120 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9121 processSync(mapper);
9122
9123 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9124 // from move
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9127 ASSERT_NO_FATAL_FAILURE(
9128 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9129 ASSERT_NO_FATAL_FAILURE(
9130 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9131
9132 // FINGER 0 MOVE
9133 processSlot(mapper, 0);
9134 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9135 processSync(mapper);
9136
9137 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9140 ASSERT_NO_FATAL_FAILURE(
9141 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9142 ASSERT_NO_FATAL_FAILURE(
9143 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9144
9145 // BUTTON DOWN
9146 processKey(mapper, BTN_LEFT, 1);
9147 processSync(mapper);
9148
9149 // touchinputmapper design sends a move before button press
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9154
9155 // BUTTON UP
9156 processKey(mapper, BTN_LEFT, 0);
9157 processSync(mapper);
9158
9159 // touchinputmapper design sends a move after button release
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9164
9165 // FINGER 0 UP
9166 processId(mapper, -1);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9170
9171 // FINGER 1 MOVE
9172 processSlot(mapper, 1);
9173 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9174 processSync(mapper);
9175
9176 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9179 ASSERT_EQ(1U, args.pointerCount);
9180 ASSERT_EQ(1, args.pointerProperties[0].id);
9181 ASSERT_NO_FATAL_FAILURE(
9182 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9183
9184 // FINGER 1 UP
9185 processId(mapper, -1);
9186 processKey(mapper, BTN_TOUCH, 0);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9190
9191 // non captured touchpad should be a mouse source
9192 mFakePolicy->setPointerCapture(false);
9193 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9195 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9196}
9197
9198TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9199 std::shared_ptr<FakePointerController> fakePointerController =
9200 std::make_shared<FakePointerController>();
9201 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9202 fakePointerController->setPosition(0, 0);
9203 fakePointerController->setButtonState(0);
9204
9205 // prepare device and capture
9206 prepareDisplay(DISPLAY_ORIENTATION_0);
9207 prepareAxes(POSITION | ID | SLOT);
9208 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9209 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009210 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009211 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9212 // run uncaptured pointer tests - pushes out generic events
9213 // FINGER 0 DOWN
9214 processId(mapper, 3);
9215 processPosition(mapper, 100, 100);
9216 processKey(mapper, BTN_TOUCH, 1);
9217 processSync(mapper);
9218
9219 // start at (100,100), cursor should be at (0,0) * scale
9220 NotifyMotionArgs args;
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9223 ASSERT_NO_FATAL_FAILURE(
9224 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9225
9226 // FINGER 0 MOVE
9227 processPosition(mapper, 200, 200);
9228 processSync(mapper);
9229
9230 // compute scaling to help with touch position checking
9231 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9232 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9233 float scale =
9234 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9235
9236 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9240 0, 0, 0, 0, 0, 0, 0));
9241}
9242
9243TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9244 std::shared_ptr<FakePointerController> fakePointerController =
9245 std::make_shared<FakePointerController>();
9246
9247 prepareDisplay(DISPLAY_ORIENTATION_0);
9248 prepareAxes(POSITION | ID | SLOT);
9249 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009250 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009251 mFakePolicy->setPointerCapture(false);
9252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9253
9254 // uncaptured touchpad should be a pointer device
9255 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9256
9257 // captured touchpad should be a touchpad device
9258 mFakePolicy->setPointerCapture(true);
9259 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9260 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9261}
9262
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009263// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009264
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009265class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009266protected:
9267 static const char* DEVICE_NAME;
9268 static const char* DEVICE_LOCATION;
9269 static const int32_t DEVICE_ID;
9270 static const int32_t DEVICE_GENERATION;
9271 static const int32_t DEVICE_CONTROLLER_NUMBER;
9272 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9273 static const int32_t EVENTHUB_ID;
9274
9275 std::shared_ptr<FakeEventHub> mFakeEventHub;
9276 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009277 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009278 std::unique_ptr<InstrumentedInputReader> mReader;
9279 std::shared_ptr<InputDevice> mDevice;
9280
9281 virtual void SetUp(Flags<InputDeviceClass> classes) {
9282 mFakeEventHub = std::make_unique<FakeEventHub>();
9283 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009284 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009285 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009286 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009287 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9288 }
9289
9290 void SetUp() override { SetUp(DEVICE_CLASSES); }
9291
9292 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009293 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009294 mFakePolicy.clear();
9295 }
9296
9297 void configureDevice(uint32_t changes) {
9298 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9299 mReader->requestRefreshConfiguration(changes);
9300 mReader->loopOnce();
9301 }
9302 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9303 }
9304
9305 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9306 const std::string& location, int32_t eventHubId,
9307 Flags<InputDeviceClass> classes) {
9308 InputDeviceIdentifier identifier;
9309 identifier.name = name;
9310 identifier.location = location;
9311 std::shared_ptr<InputDevice> device =
9312 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9313 identifier);
9314 mReader->pushNextDevice(device);
9315 mFakeEventHub->addDevice(eventHubId, name, classes);
9316 mReader->loopOnce();
9317 return device;
9318 }
9319
9320 template <class T, typename... Args>
9321 T& addControllerAndConfigure(Args... args) {
9322 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9323
9324 return controller;
9325 }
9326};
9327
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009328const char* PeripheralControllerTest::DEVICE_NAME = "device";
9329const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9330const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9331const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9332const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9333const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009334 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009335const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009336
9337// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009338class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009339protected:
9340 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009341 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009342 }
9343};
9344
9345TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009346 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009347
9348 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9349 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9350}
9351
9352TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009353 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009354
9355 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9356 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9357}
9358
9359// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009360class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009361protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009362 void SetUp() override {
9363 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9364 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009365};
9366
Chris Ye85758332021-05-16 23:05:17 -07009367TEST_F(LightControllerTest, MonoLight) {
9368 RawLightInfo infoMono = {.id = 1,
9369 .name = "Mono",
9370 .maxBrightness = 255,
9371 .flags = InputLightClass::BRIGHTNESS,
9372 .path = ""};
9373 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009374
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009375 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009376 InputDeviceInfo info;
9377 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009378 std::vector<InputDeviceLightInfo> lights = info.getLights();
9379 ASSERT_EQ(1U, lights.size());
9380 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009381
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009382 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9383 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009384}
9385
9386TEST_F(LightControllerTest, RGBLight) {
9387 RawLightInfo infoRed = {.id = 1,
9388 .name = "red",
9389 .maxBrightness = 255,
9390 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9391 .path = ""};
9392 RawLightInfo infoGreen = {.id = 2,
9393 .name = "green",
9394 .maxBrightness = 255,
9395 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9396 .path = ""};
9397 RawLightInfo infoBlue = {.id = 3,
9398 .name = "blue",
9399 .maxBrightness = 255,
9400 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9401 .path = ""};
9402 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9403 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9404 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9405
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009406 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009407 InputDeviceInfo info;
9408 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009409 std::vector<InputDeviceLightInfo> lights = info.getLights();
9410 ASSERT_EQ(1U, lights.size());
9411 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009412
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009413 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9414 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009415}
9416
9417TEST_F(LightControllerTest, MultiColorRGBLight) {
9418 RawLightInfo infoColor = {.id = 1,
9419 .name = "red",
9420 .maxBrightness = 255,
9421 .flags = InputLightClass::BRIGHTNESS |
9422 InputLightClass::MULTI_INTENSITY |
9423 InputLightClass::MULTI_INDEX,
9424 .path = ""};
9425
9426 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9427
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009428 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009429 InputDeviceInfo info;
9430 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009431 std::vector<InputDeviceLightInfo> lights = info.getLights();
9432 ASSERT_EQ(1U, lights.size());
9433 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009434
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009435 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9436 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009437}
9438
9439TEST_F(LightControllerTest, PlayerIdLight) {
9440 RawLightInfo info1 = {.id = 1,
9441 .name = "player1",
9442 .maxBrightness = 255,
9443 .flags = InputLightClass::BRIGHTNESS,
9444 .path = ""};
9445 RawLightInfo info2 = {.id = 2,
9446 .name = "player2",
9447 .maxBrightness = 255,
9448 .flags = InputLightClass::BRIGHTNESS,
9449 .path = ""};
9450 RawLightInfo info3 = {.id = 3,
9451 .name = "player3",
9452 .maxBrightness = 255,
9453 .flags = InputLightClass::BRIGHTNESS,
9454 .path = ""};
9455 RawLightInfo info4 = {.id = 4,
9456 .name = "player4",
9457 .maxBrightness = 255,
9458 .flags = InputLightClass::BRIGHTNESS,
9459 .path = ""};
9460 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9461 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9462 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9463 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9464
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009465 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009466 InputDeviceInfo info;
9467 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009468 std::vector<InputDeviceLightInfo> lights = info.getLights();
9469 ASSERT_EQ(1U, lights.size());
9470 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009471
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009472 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9473 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9474 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009475}
9476
Michael Wrightd02c5b62014-02-10 15:10:22 -08009477} // namespace android